Adding a snackbar in Flutter

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

I don't mean the Dutch word snackbar (see image below), but a toast-like message that we can use to feedback to the user.

Using snackbars in mobile apps is very common as they are a perfect way to provide valuable feedback.
For today's article, I will be using my Flutter hello world app as the starting point.
The cool part is that the only requirement for using a snackbar in Flutter is wrapping our application in a scaffold. Something we do most of the time anyway.
We can then use something called the ScaffoldMessenger to send a message on specific actions.
Snackbars themselves can have options in return which we'll also have a look at.
Let's add a button to our application so we can use that to invoke our snackbar.
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: ElevatedButton(
onPressed: () {},
child: const Text('Open the snackbar'),
),
);
}
}
This code will give us an empty button.

Inside this button, we have the onPressed method, which we'll use to bind our action sheet.
onPressed: () {
final snackbar = SnackBar(
content: const Text('Hello world, Snackbar here!'),
);
ScaffoldMessenger.of(context).showSnackBar(snackbar);
},
Now, when we click our button, the snackbar will show at the bottom of our screen.

We can also use the snackbar to give the user action. This might be to undo something they just did.
For instance: User changes a row, the snackbar will show: Successfully modified row x. Then we can have an undo button to revert this change again.
That code will look like this:
onPressed: () {
final snackbar = SnackBar(
content: const Text('Successfully modified row x'),
action: SnackBarAction(
label: 'Undo',
onPressed: () {
// Add your undo code here
},
),
);
ScaffoldMessenger.of(context).showSnackBar(snackbar);
},
You need to implement the code that will undo the action you just did in the onPressed callback.
Note: A snackbar is designed only to have one action. For multiple actions, we should use a dialog.

If you want to see the source code, you can find that on GitHub.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter