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();
},
),));
}
You can hide the current SnackBar before the end of its duration by using: ScaffoldMessenger. of(ctx). hideCurrentSnackBar();
of() is deprecated for showing SnackBars, use ScaffoldMessenger instead.
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.
This lib is deprecated in favor of Google's Design Support Library which includes a Snackbar and is no longer being developed.
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();
Try using hideCurrentSnackBar
method
onPressed: () {
homeScaffoldKey.currentState.hideCurrentSnackBar();
},
More info here: https://docs.flutter.io/flutter/material/ScaffoldState/hideCurrentSnackBar.html
ScaffoldMessenger.of(context).hideCurrentSnackBar();
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);
}
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With