Flutter Snackbar

 

Flutter Snackbar

Snackbar in Flutter is a widget showing the lightweight message that briefly informs the user when certain actions occur. It displays the message for a very short period, and when the specified time completed, it will be disappeared from the screen. By default, snack bar displays at the bottom of the screen. It is an excellent way to give feedback to users. It also contains some actions that allow the user to undo or redo any action. Usually, the snack bar is used with the scaffold widget.

SnackBar Properties

The following are the important properties of the snack bar used in the Flutter:

Attribute NamesDescriptions
contentIt is the main content of the snack bar, which is actually a text widget.
durationIt is used to specify how much time the snack bar should be displayed.
actionIt is used to take action when the user taps on the snack bar. It cannot be dismissed or cancel. We can only undo or redo in the snack bar.
elevationIt is z-coordinate where the snack bar is placed. It is used to control the shadow size below the snack bar.
shapeIt is used to customize the shape of a snack bar.
behaviorIt is used to set the location of the snack bar.
bagroundcolorIt specifies the background of the snack bar.
animationIt defines the exit and entrance of the snack bar.

If we have used the snack bar in the mobile app, it allows the users to get information about their actions in the app. In this article, we are going to learn how to add and show SnackBar in Flutter using the following steps:

  1. Create a project in the IDE you are using.
  2. Create a Scaffold Widget.
  3. Display a SnackBar Widget.
  4. Provide an optional action.

Step 1: Create a Flutter project in the IDE you are using and then open the project in Android Studio. After opening the project, we need to navigate to the lib folder and open the main.dart file.

Step 2: Create a Scaffold widget responsible for the visual structure and ensure the essential widgets do not overlap.

  1. Scaffold(  
  2.   appBar: AppBar(  
  3.     title: Text(' SnackBar Example'),  
  4.   ),  
  5.   body: SnackBarPage(),  
  6. );  

Step 3: Next, we need to display a SnackBar. So, create a snack bar in the Scaffold widget as given below:

  1. final snackBar = SnackBar(content: Text(' Hey! I am a SnackBar message.'));  
  2.   
  3. // Here, we will use the scaffold widget to show a snack bar.  
  4. Scaffold.of(context).showSnackBar(snackBar);  

Step 4: Next, we can add some action to the snack bar. For example, suppose the user accidentally deletes a message or sent a mail, then we can provide an optional action in the snack bar to recover those messages. The below demo code explains it more clearly:

  1. final snackBar = SnackBar(  
  2.   content: Text(' Hey! I am a SnackBar message.'),  
  3.   action: SnackBarAction(  
  4.     label: 'Undo',  
  5.     onPressed: () {  
  6.       // Write your code to undo the change.  
  7.     },  
  8.   ),  
  9. );  

Let us see the complete code of the above steps. Open the main.dart file and replace the following code. This code contains a button, and when the user taps on this, it will display the snack bar message. It also contains actions to undo or redo the changes.

  1. import 'package:flutter/material.dart';  
  2.   
  3. void main() {runApp(MyApp());}  
  4.   
  5. class MyApp extends StatelessWidget {  
  6.   @override  
  7.   Widget build(BuildContext context) {  
  8.     return MaterialApp(  
  9.       theme: new ThemeData(  
  10.         primaryColor: const Color(0xFF43a047),  
  11.         accentColor: const Color(0xFFffcc00),  
  12.         primaryColorBrightness: Brightness.dark,  
  13.       ),  
  14.       home: Scaffold(  
  15.         appBar: AppBar(  
  16.           title: Text('Flutter SnackBar Demo'),  
  17.         ),  
  18.         body: SnackBarPage(),  
  19.       ),  
  20.     );  
  21.   }  
  22. }  
  23.   
  24. class SnackBarPage extends StatelessWidget {  
  25.   @override  
  26.   Widget build(BuildContext context) {  
  27.     return Center(  
  28.       child: RaisedButton(  
  29.         child: Text('Show SnackBar', style: TextStyle(fontSize: 25.0),),  
  30.         textColor: Colors.white,  
  31.         color: Colors.redAccent,  
  32.         padding: EdgeInsets.all(8.0),  
  33.         splashColor: Colors.grey,  
  34.         onPressed: () {  
  35.           final snackBar = SnackBar(  
  36.             content: Text('Hey! This is a SnackBar message.'),  
  37.             duration: Duration(seconds: 5),  
  38.             action: SnackBarAction(  
  39.               label: 'Undo',  
  40.               onPressed: () {  
  41.                 // Some code to undo the change.  
  42.               },  
  43.             ),  
  44.           );  
  45.           Scaffold.of(context).showSnackBar(snackBar);  
  46.         },  
  47.       ),  
  48.     );  
  49.   }  
  50. }  

Output:

When we run this app, it will show the UI of the screen as below screenshot.

Flutter Snackbar

If we click the "Show SnackBar" button, we will see the message at the bottom of the screen. This message will be deleted automatically after completing the specified time. See the below screenshot:

Flutter Snackbar


Post a Comment

Previous Post Next Post