create snackbar flutter code example
Example 1: 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 2: snackbar in flutter
final snackBar = SnackBar(content: Text('Yay! A SnackBar!'));
ScaffoldMessenger.of(context).showSnackBar(snackBar);
Example 3: flutter snackbar
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
@override
void initState() {
super.initState();
showInSnackBar("Some text");
}
@override
Widget build(BuildContext context) {
return new Padding(
key: _scaffoldKey,
padding: const EdgeInsets.all(16.0),
child: new Text("Simple Text")
);
}
void showInSnackBar(String value) {
_scaffoldKey.currentState.showSnackBar(new SnackBar(
content: new Text(value)
));
}
}
Example 4: snackbar in flutter
final snackBar = SnackBar(
content: Text('Yay! A SnackBar!'),
action: SnackBarAction(
label: 'Undo',
onPressed: () {
},
),
);
Example 5: 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);
},
),