how to create a snackbar in flutter code example
Example 1: flutter snackbar showing error
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:
GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
Scaffold(
key: _scaffoldKey,
body: ...
)
_scaffoldKey.showSnackBar(SnackBar(
content: Text("New Notification"),
));
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:
GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
Scaffold(
key: _scaffoldKey,
body: ...
)
_scaffoldKey.showSnackBar(SnackBar(
content: Text("New Notification"),
));
Example 3: show snackbar without scaffold flutter
void _showToast(BuildContext context) {
final scaffold = Scaffold.of(context);
scaffold.showSnackBar(
SnackBar(
content: const Text('Updating..'),
),
);
this._showToast(context);