I want to display a simple SnackBar inside Flutter\'s stateful widget. My application creates new instance of MaterialApp with a stateful widget ca
In case someone is looking to initialise a Snackbar
on initState()
(in other words when the page loads here is my solution). Also, I assume you already have the _scaffoldKey
in place.
void initState() {
super.initState();
Future.delayed(Duration(seconds: 1)).then(
(_) => _displaySnackbar
);
}
// Display Snackbar
void get _displaySnackbar {
_scaffoldKey.currentState.showSnackBar(SnackBar(
duration: Duration(minutes: 1),
content: Text('Your snackbar message')
));
}
The best way is to create a PageWrapper that you can wrap all of your pages to get the context of the Scaffold widget and avoid experiencing these errors again.
Here is a sample of the page wrapper:
import 'package:flutter/material.dart';
class PageWrapper extends StatelessWidget {
Widget page;
WPage(this.page);
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: page
);
}
}
Then you can wrap all your pages in the main.dart like these:
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(
initialRoute: '/login',
routes: {
'/login': (context) => PageWrapper(Login()),
'/home': (context) => PageWrapper(Home())
}
));
Now you can call Scaffold.of(context)
anywhere in any page just fine.
Reference: https://noobieprogrammer.blogspot.com/2020/06/how-to-create-error-alert-or-popup.html
or watch the video version (5 min. long): https://youtu.be/u9KoFtu0HEY
Scaffold.of(context).showSnackBar(
SnackBar(content: Text("Thanks for using snackbar",
textAlign: TextAlign.center, style: TextStyle(fontSize: 16.0, fontWeight:
FontWeight.bold),), duration: Duration(seconds: 2), backgroundColor: Colors.red,)
);
You can do this in three simple steps.
Make sure you have a key for scaffold. You can create that by writing the below code:
final _scaffoldKey = GlobalKey<ScaffoldState>();
2.Now you have to mention this key inside your Scaffold, by writing the below line inside scaffold:
key:_scaffoldKey,
Now you can show snackbar by writing:
final snackBar=SnackBar(
content: Text('Your password has been changed successfully'),
);
_scaffoldKey.currentState.showSnackBar(snackBar);
There's three problems. The first is that you don't have a Scaffold anywhere, and the Scaffold widget is the one that knows how to show snack bars. The second is that you have a key for getting a hold of the scaffold, but you've put it on a Padding instead (and Paddings don't have any knowledge of snack bars). The third is that you've used the key before the widget that it's associated with has had a chance to be initialised, since initState is called before build.
The simplest solution is to change the home
line in your MyApp widget to:
home: new Scaffold(body: new MyHomePage()),
...and then remove all mention of _scaffoldKey
and instead use Scaffold.of(context)
where you currently have _scaffoldKey.currentState
.
I dit it, work for me :)
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('Demo')
),
body: new Builder(
// Create an inner BuildContext so that the onPressed methods
// can refer to the Scaffold with Scaffold.of().
builder: (BuildContext context) {
return new Center(
child: new RaisedButton(
child: new Text('SHOW A SNACKBAR'),
onPressed: () {
Scaffold.of(context).showSnackBar(new SnackBar(
content: new Text('Hello!'),
));
},
),
);
},
),
);
}