Crafting SnackBar Widgets in Flutter

24 Jun 2023 Balmiki Mandal 0 Andriod

Crafting SnackBar Widgets in Flutter

SnackBar widgets are a great way to give feedback to users in your Flutter applications. They are simple yet effective in displaying short messages to users when an action is performed. In this article, I will go over how to craft your own custom SnackBar widgets in Flutter.

Creating a Simple SnackBar Widget

The first step in creating a SnackBar widget is to create a Scaffold widget which is a basic widget for a Flutter application. After creating the Scaffold widget, you can add a SnackBar child widget and customize the content inside of it. Inside of the SnackBar widget, you can define an action which consists of a label and an icon.

Next, you can specify the duration of the SnackBar, which is the amount of time it will be displayed on the user’s screen. In this example, we are setting the duration to 5 seconds. The last step is to call the showSnackBar() method with the SnackBar widget as a parameter. This will display the snackbar on the user’s screen for the given duration.

Below is an example of a simple SnackBar widget:

Scaffold( 
  body: Center(
    child: RaisedButton(
      onPressed: () {
        // Create a SnackBar
        final snackBar = SnackBar(
          content: Text('You pressed the button!'),
          action: SnackBarAction(
            label: 'Undo',
            onPressed: () {
              // Some code to undo the change.
            },
          ),
          duration: Duration(seconds: 5),
        );

        // Show SnackBar
        Scaffold.of(context).showSnackBar(snackBar);
      },
    ),
  ),
);

Creating a Custom SnackBar Widget

In addition to creating a simple SnackBar widget, you can also create a custom SnackBar widget with additional features. For example, you can add an extra action button or set the background color. You can also set the duration of the SnackBar, and even add a callback function which will be triggered when the SnackBar is dismissed.

Below is an example of a custom SnackBar widget:

Scaffold( 
  body: Center(
    child: RaisedButton(
      onPressed: () {
        // Create a custom SnackBar
        final snackBar = SnackBar(
          content: Text('You pressed the button!'),
          action: SnackBarAction(
            label: 'Undo',
            onPressed: () {
              // Some code to undo the change.
            },
          ),
          backgroundColor: Colors.black,
          duration: Duration(seconds: 10),
          onDismissed: () {
            // Do something when the SnackBar is dismissed.
          },
        );

        // Show SnackBar
        Scaffold.of(context).showSnackBar(snackBar);
      },
    ),
  ),
);

As you can see, you can easily create custom SnackBar widgets which can be used to display messages to your users in your Flutter applications.

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.