Flutter Open
  • Flutter Widgets
  • Structure of code
  • Widgets 01 | Container
  • Widgets 02 | Text
  • Widgets 03 | Image
  • Widgets 04 | Row & Column
  • Widgets 05 | Icon
  • Widgets 06 | RaiseButton
  • Widgets 07 | AppBar
  • Widgets 08 | Scaffold
  • Widgets 09 | FlutterLogo
  • Widgets 10 | Placeholder
  • Widgets 11 | BottomNavigationBar
  • Widgets 12 | TabBarView&TabBar
  • Widgets 13 | DropdownButton
  • Widgets 14 | PopupMenuButton
  • Widgets 15 | Stack
  • Widgets 16 | Stepper
  • Widgets 17 | SimpleDialog
  • Widgets 18 | SnackBar
  • Animation
    • Animation 01 | Base Animation
    • Animation 02 | Flare Animation
  • Canvas
    • Canvas 01 | Custom Painting
    • Canvas 02 | Round Angle Polygon
    • Canvas 03 | Regular Polygon
    • Canvas 04 | Pie Chart With Round Angle
    • Canvas 05 | Gesture With Canvas
Powered by GitBook
On this page
  • Before Start
  • Simple Use
  • Constructor
  • action
  • duration
  • Conclusion

Was this helpful?

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.

import "package:flutter/material.dart";
import 'package:flutter_widgets/const/_const.dart';

class SnackPage extends StatefulWidget {
  @override
  _SnackState createState() => _SnackState();
}

class _SnackState extends State<SnackPage> {
  GlobalKey<ScaffoldState> _key = GlobalKey();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        key: _key,
        appBar: AppBar(
          title: Text(PageName.SNACK_BAR),
        ),
        body: Center(
          child: RaisedButton(
            onPressed: () {
             // our code.
            },
            child: Text("Show the snack bar"),
            color: RED,
          ),
        ));
  }
}

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.

  Widget _snackSample() => SnackBar(
        content: Text(
          "You have a message!",
          style: TextStyle(
            color: TEXT_BLACK,
            fontSize: 20,
          ),
          textAlign: TextAlign.center,
        ),
        backgroundColor: BLUE_LIGHT,
      );

Then, we use them in the onPressed function.

final bar = _snackSample();
_key.currentState.showSnackBar(bar);

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.

SnackBar({
    Key key,
    @required this.content,
    this.backgroundColor,
    this.action,
    this.duration = _kSnackBarDisplayDuration,
    this.animation,
  })

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.

  final SnackBarAction action;

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

SnackBarAction({
    Key key,
    this.textColor,
    this.disabledTextColor,
    @required this.label,
    @required this.onPressed,
  })

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

  Widget _snackAction() => SnackBar(
        content: Text(
          "Test the action in the SnackBar.",
          style: TextStyle(
            color: TEXT_BLACK,
            fontSize: 20,
          ),
          textAlign: TextAlign.center,
        ),
        action: SnackBarAction(
          label: "I Know!",
          textColor: Colors.white,
          disabledTextColor: TEXT_BLACK_LIGHT,
          onPressed: () {
            print("I know you are testing the action in the SnackBar!");
          },
        ),
        backgroundColor: BLUE_LIGHT,
      );

You can use it in the button.

final bar = _snackAction();
_key.currentState.showSnackBar(bar);

It will show like this.

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

I/flutter ( 4202): I know you are testing the action in the SnackBar!

duration

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

 Widget _snackDuration() => SnackBar(
        content: Text(
          "You have a message!",
          style: TextStyle(
            color: TEXT_BLACK,
            fontSize: 20,
          ),
          textAlign: TextAlign.center,
        ),
        backgroundColor: BLUE_LIGHT,
        duration: Duration(milliseconds: 100),
      );

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.

Facebook Page

Twitter

GitHub

Medium

PreviousWidgets 17 | SimpleDialog

Last updated 6 years ago

Was this helpful?

Whole code in ,star to support.

GitHub
Flutter Open
NieBin
NieBin
FlutterOpen