Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
    },
  ),));
}
like image 753
Ammy Kang Avatar asked Mar 02 '19 05:03

Ammy Kang


People also ask

How do you dismiss SnackBar in Flutter?

You can hide the current SnackBar before the end of its duration by using: ScaffoldMessenger. of(ctx). hideCurrentSnackBar();

What can I use instead of SnackBar in Flutter?

of() is deprecated for showing SnackBars, use ScaffoldMessenger instead.

How do you show SnackBar in Flutter without scaffolding?

you probably have a parent widget that contains Scaffold as well. Create a scaffoldKey for that and pass it to your child widget that have to show the Snakcbar . Note that you can't use Sanckbar without Scaffold widget.

Is SnackBar deprecated?

This lib is deprecated in favor of Google's Design Support Library which includes a Snackbar and is no longer being developed.


5 Answers

You can also use,

Scaffold.of(context).hideCurrentSnackBar();

Be careful when you use context, use the correct context.

NOTE

In the new Flutter Version, this method is deprecated. Therefore use

ScaffoldMessenger.of(context).hideCurrentSnackBar();
like image 55
Achintha Isuru Avatar answered Oct 16 '22 16:10

Achintha Isuru


Try using hideCurrentSnackBar method

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

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

like image 21
diegoveloper Avatar answered Oct 16 '22 16:10

diegoveloper


ScaffoldMessenger.of(context).hideCurrentSnackBar();

like image 14
Rizwan Ansar Avatar answered Oct 16 '22 16:10

Rizwan Ansar


If you want to replace snackbar that show only one time,

  final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
  final snackBar = SnackBar(content: Text("Hello, world"));

And also,

@override
Widget build(BuildContext context) {
return Scaffold(
  key: _scaffoldKey,

And also,

onPressed: () {
    _scaffoldKey.currentState.removeCurrentSnackBar();
    _scaffoldKey.currentState.showSnackBar(snackBar);
     }
like image 6
Hussnain Haidar Avatar answered Oct 16 '22 18:10

Hussnain Haidar


Define your SnackBar:

var snackBar = SnackBar(content: Text('Hello World'));

To show it:

ScaffoldMessenger.of(context).showSnackBar(snackBar);

To hide it:

ScaffoldMessenger.of(context).hideCurrentSnackBar();

To hide the last one and show a new one:

ScaffoldMessenger.of(context) 
  ..hideCurrentSnackBar()
  ..showSnackBar(snackBar);
like image 4
CopsOnRoad Avatar answered Oct 16 '22 17:10

CopsOnRoad