Widgets 18 | SnackBar
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 our SnackBar. I will use a button to click, if you click it, it will show you a snack bar.
1
import "package:flutter/material.dart";
2
import 'package:flutter_widgets/const/_const.dart';
3
4
class SnackPage extends StatefulWidget {
5
@override
6
_SnackState createState() => _SnackState();
7
}
8
9
class _SnackState extends State<SnackPage> {
10
GlobalKey<ScaffoldState> _key = GlobalKey();
11
@override
12
Widget build(BuildContext context) {
13
return Scaffold(
14
key: _key,
15
appBar: AppBar(
16
title: Text(PageName.SNACK_BAR),
17
),
18
body: Center(
19
child: RaisedButton(
20
onPressed: () {
21
// our code.
22
},
23
child: Text("Show the snack bar"),
24
color: RED,
25
),
26
));
27
}
28
}
Copied!
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 a SnackBar.
1
Widget _snackSample() => SnackBar(
2
content: Text(
3
"You have a message!",
4
style: TextStyle(
5
color: TEXT_BLACK,
6
fontSize: 20,
7
),
8
textAlign: TextAlign.center,
9
),
10
backgroundColor: BLUE_LIGHT,
11
);
Copied!
Then, we use them in the onPressed function.
1
final bar = _snackSample();
2
_key.currentState.showSnackBar(bar);
Copied!
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.
1
SnackBar({
2
Key key,
3
@required this.content,
4
this.backgroundColor,
5
this.action,
6
this.duration = _kSnackBarDisplayDuration,
7
this.animation,
8
})
Copied!
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.
1
final SnackBarAction action;
Copied!
This class is also a widget, let's look at its constructor.
1
SnackBarAction({
2
Key key,
3
this.textColor,
4
this.disabledTextColor,
5
@required this.label,
6
@required this.onPressed,
7
})
Copied!
The label is the String, the onPressed is a function. The other two are the Color. Let's look at an example.
1
Widget _snackAction() => SnackBar(
2
content: Text(
3
"Test the action in the SnackBar.",
4
style: TextStyle(
5
color: TEXT_BLACK,
6
fontSize: 20,
7
),
8
textAlign: TextAlign.center,
9
),
10
action: SnackBarAction(
11
label: "I Know!",
12
textColor: Colors.white,
13
disabledTextColor: TEXT_BLACK_LIGHT,
14
onPressed: () {
15
print("I know you are testing the action in the SnackBar!");
16
},
17
),
18
backgroundColor: BLUE_LIGHT,
19
);
Copied!
You can use it in the button.
1
final bar = _snackAction();
2
_key.currentState.showSnackBar(bar);
Copied!
It will show like this.
When you click the I Know!, the console will print some strings.
1
I/flutter ( 4202): I know you are testing the action in the SnackBar!
Copied!

duration

When you want to change the show time of the SnackBar, you should use the parameter.duration. Let's see an example.
1
Widget _snackDuration() => SnackBar(
2
content: Text(
3
"You have a message!",
4
style: TextStyle(
5
color: TEXT_BLACK,
6
fontSize: 20,
7
),
8
textAlign: TextAlign.center,
9
),
10
backgroundColor: BLUE_LIGHT,
11
duration: Duration(milliseconds: 100),
12
);
Copied!
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 your reading!
The end.

Whole code in GitHub,star to support.

Facebook Page
Twitter
GitHub
Medium
NieBin
NieBin
Last modified 2yr ago