In this post, I'm going to share you the code to create a Snackbar in Flutter.
Once you run the provided code, this is how a Snackbar will popup on a button click.
; background-color: rgb(30, 30, 30); font-family: Consolas, "Courier New", monospace; line-height: 19px; white-space: pre-wrap;](
color: #569cd6;](import color: #ce9178;]('package:flutter/material.dart';
color: #569cd6;](void color: #dcdcaa;](main() => color: #dcdcaa;](runApp(color: #c586c0;](new color: #4ec9b0;](MyApp());
color: #569cd6;](class color: #4ec9b0;](MyApp color: #569cd6;](extends color: #4ec9b0;](StatelessWidget {
color: #6a9955;](// This widget is the root of your application.
color: #569cd6;](@override
color: #4ec9b0;](Widget color: #dcdcaa;](build(color: #4ec9b0;](BuildContext context) {
color: #c586c0;](return color: #c586c0;](new color: #4ec9b0;](MaterialApp(
title: color: #ce9178;]('Flutter Demo',
theme: color: #c586c0;](new color: #4ec9b0;](ThemeData(
primarySwatch: color: #4ec9b0;](Colors.blue,
),
home: color: #c586c0;](new color: #4ec9b0;](MyHomePage(title: color: #ce9178;]('Flutter Demo Home Page'),
);
}
}
color: #569cd6;](class color: #4ec9b0;](MyHomePage color: #569cd6;](extends color: #4ec9b0;](StatefulWidget {
color: #4ec9b0;](MyHomePage({color: #4ec9b0;](Key key, color: #569cd6;](this.title}) : color: #569cd6;](super(key: key);
color: #569cd6;](final color: #4ec9b0;](String title;
color: #569cd6;](@override
color: #4ec9b0;](_MyHomePageState color: #dcdcaa;](createState() => color: #c586c0;](new color: #4ec9b0;](_MyHomePageState();
}
color: #569cd6;](class color: #4ec9b0;](_MyHomePageState color: #569cd6;](extends color: #4ec9b0;](State {
color: #569cd6;](@override
color: #4ec9b0;](Widget color: #dcdcaa;](build(color: #4ec9b0;](BuildContext context) {
color: #c586c0;](return color: #c586c0;](new color: #4ec9b0;](Scaffold(
appBar: color: #c586c0;](new color: #4ec9b0;](AppBar(
title: color: #c586c0;](new color: #4ec9b0;](Text(widget.title),
),
body:
color: #c586c0;](new color: #4ec9b0;](Builder(
builder: (color: #4ec9b0;](BuildContext context) {
color: #c586c0;](return color: #c586c0;](new color: #4ec9b0;](Center(
child: color: #c586c0;](new color: #4ec9b0;](RaisedButton(
onPressed: () {
color: #4ec9b0;](Scaffold.color: #dcdcaa;](of(context).color: #dcdcaa;](showSnackBar(color: #4ec9b0;](SnackBar(
content: color: #c586c0;](new color: #4ec9b0;](Text(color: #ce9178;]('Hello from Snakbar!'),
action: color: #4ec9b0;](SnackBarAction(
label: color: #ce9178;]("Undo",
onPressed: () {
},
),
));
},
child: color: #c586c0;](new color: #4ec9b0;](Text(color: #ce9178;]("Show Snakbar"),
)
);
},
)
);
}
}
Hope this is useful.
Thanks,
Srikanth