Flutter Widgets 18 | SnackBar

NieBin
flutteropen
Published in
3 min readFeb 15, 2019

In this tutorial, you will learn how to use the SnackBar.

Before Start

When we start our tutorial, we should create a page to show the SnackBar. I will use a button to click, if you click it, it will show you a snack bar.

It will show you like this. If we click the button, I want that the SnackBar will show on the bottom of the screen.

Simple Use

I will give you a simple to show the SnackBar. First, we should create SnackBar.

Then, we use them in the onPressed function.

When you click the button, it will show you as follow.

Constructor

When we start to learn the widgets, we should have a custom that we should look at its constructor first.

The content is a widget, the backgroundColor is the Color, the duration is a Duration, the animation is an `Animation`. So let's talk about them one by one.

action

This parameter is very special because we do not see it before. So we should look at its definition.

This class is also a widget, let’s look at its constructor.

The label is the String, the onPressed is a function. The other two are the Color. Let's look at an example.

You can use it in the button.

It will show like this.

When you click the I Know!, the console will print some strings.

duration

When you want to change the show time of the SnackBar, you should use the parameter.duration. Let's see an example.

When you click the button, you can see that the show time become short than before. The effect is below.

Conclusion

I have talked about the parameter action,duration. I just use the content and backgroundColor, because they are very easy and we have used its type before, so I don't talk about its detail. Also, I don't plan to talk about the animation, I will talk about them in the Animation Widget.

Thanks for reading.

The End.

The whole code in Github: https://github.com/FlutterOpen/ebook

--

--