flutter show snack bar code example
Example 1: flutter snackbar replacement
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: const Text('snack'),
duration: const Duration(seconds: 1),
action: SnackBarAction(
label: 'ACTION',
onPressed: () { },
),
));
Example 2: how to show snackbar in flutter
You can show material design snackbars using the following code:
Scaffold.of(context).showSnackBar(SnackBar(
content: Text("New Notification"),
));
In some cases, this will throw an error and it can be resolved using a workaround:
//Declare a GlobalKey
GlobalKey _scaffoldKey = GlobalKey();
//Assing this key to the scaffold
Scaffold(
key: _scaffoldKey,
body: ...
)
//finally in the topmost code use this key in the following way
_scaffoldKey.showSnackBar(SnackBar(
content: Text("New Notification"),
));
Example 3: flutter snackbar
final snackBar = SnackBar(
content: Text('Yay! A SnackBar!'),
action: SnackBarAction(
label: 'Undo',
onPressed: () {
// Some code to undo the change.
},
),
);
// Find the Scaffold in the widget tree and use
// it to show a SnackBar.
Scaffold.of(context).showSnackBar(snackBar);
Example 4: Flutter snackbar top
RaisedButton(
child: Text('Show Top Snackbar'),
onPressed: () {
Flushbar(
flushbarPosition: FlushbarPosition.TOP,
)
..title = "Hey Ninja"
..message = "Lorem Ipsum is simply dummy text of the printing and typesetting industry"
..duration = Duration(seconds: 3)
..show(context);
},
),