dartfluttersnackbar

Flutter snackbar dismiss on SnackBarAction onPressed


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();
    },
  ),));
}

Solution

  • Try using hideCurrentSnackBar method

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

    Update

    Use ScaffoldMessenger instead, heee you have the guide:

    https://docs.flutter.dev/release/breaking-changes/scaffold-messenger

    More info here: https://api.flutter.dev/flutter/material/ScaffoldMessengerState/hideCurrentSnackBar.html