Flutter snackbar dismiss on SnackBarAction onPressed

Ammy Kang picture Ammy Kang · Mar 2, 2019 · Viewed 14.3k times · Source

I want to dismiss SnackBar on SnackBarAction's onPressed method. I tried with Navigator.of(context).pop(); but SnackBar is not dismissing my screen get black instead.

Here is code:

 void showInSnackBar(String value) {
homeScaffoldKey.currentState.showSnackBar(new SnackBar(content: new Text(value),
  action: SnackBarAction(
    label: 'Dissmiss',
    textColor: Colors.yellow,
    onPressed: () {
    //  Navigator.of(context).pop();
    },
  ),));
}

Answer

diegoveloper picture diegoveloper · Mar 2, 2019

Try using hideCurrentSnackBar method

onPressed: () {
    homeScaffoldKey.currentState.hideCurrentSnackBar();
},

More info here: https://docs.flutter.io/flutter/material/ScaffoldState/hideCurrentSnackBar.html