So I have a requirement, where I need to show the Snackbar
just when the application starts, but when I try to do it, it gives me context error. How can I know that my Widget tree has been built and use the context to display the Snackbar
.
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
Scaffold.of(context).showSnackBar(SnackBar(content: Text('Welcome User')));
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[],
),
),
);
}
}
showSnackBar( SnackBar( content: Text("Hello This is FlutterCampus"), action: SnackBarAction( onPressed: (){ //if action button is pressed }, label: "OPEN WEBSITE", ), ) ); You can show Snackbar with the action using the above code.
Create a GlobalKey of type ScaffoldState to make sure you target the right Scaffold to show a SnackBar on.
Set your target Scaffold's key to the key you have created.
In your initState() function use WidgetsBinding class and addPostFrameCallback to execute your code when the layout is built. Show your SnackBar in the call back.
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
var _scaffoldKey = GlobalKey<ScaffoldState>();
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) => _scaffoldKey.currentState.showSnackBar(SnackBar(content: Text('Welcome User'))));
}
@override
Widget build(BuildContext context) {
return new Scaffold(
key: _scaffoldKey,
appBar: new AppBar(
title: new Text(widget.title),
),
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[],
),
),
);
}
}
You don't need GlobalKey
, Builder
or any typical stuff like that. Simply use ScaffoldMessenger.of(context).showSnackBar()
.
Full code:
void main() => runApp(MaterialApp(home: HomePage()));
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
void initState() {
super.initState();
final snackBar = SnackBar(content: Text('Hi'));
Future(() => ScaffoldMessenger.of(context).showSnackBar(snackBar));
}
@override
Widget build(BuildContext context) => Scaffold();
}
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