How to Display a Snackbar in Flutter?

Learn via video courses
Topics Covered

Overview

Snackbars are a crucial UI component in Flutter, which are lightweight and are used to show a temporary message or notification at the bottom of the screen. We can customize the snackbar and can show alerts and messages to the users. Snackbar is a valuable and fancy widget for developers and makes the user experience great. Snackbars in Flutter are used in a variety of apps for showing some information or feedback, notifying the users of specific actions, or allowing the user to undo an action related to the message displayed.

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

What is the SnackBar Class?

The SnackBar class extends the StatefulWidget class, which means it can be updated dynamically to reflect changes in its content or behavior. Snackbar in Flutter is part of the material library and provides a convenient way to display feedback or alerts to the user. The snackbar class has properties like action, content, duration, behavior, width, padding, etc. which we can use and design the snackbar. Snackbars in Flutter pop up on the screen for a very short period and when the time is completed it disappears on its own.

SnackBar Properties

These are the properties of the snackbar in Flutter-

PropertiesDescription
contentThis returns a widget and shows the main data or message like a text in the snackbar.
durationThis shows how much time our snackbar should be on the screen. The default is 4.0 seconds.
shapeIt is used to customize the shape of the snackbar like a rectangle(can change width, color, border radius), or stadium border.
actionAn optional SnackBarAction widget that represents an action button displayed alongside the content. It allows the user to perform an action related to the message.
elevationIt is z-coordinate where the snack bar is placed. It is used to control the shadow size below the snack bar.
behaviorIt shows the behavior and location of the snack bar like it should be adjusted with FAB or fixed.
backgroundcolorThis is used to change the background color of the snackbar
widthIt gives the width to the snackbar
dismissDirectionThis defines the direction in which the snackbar should dismiss like up, down, horizontal, or vertical.
animationIt defines the exit and entrance of the snack bar.
Sharpen Your Fundamentals with Free Learning

Example Snackbar App in Futter

We will learn to create and use snackbar by following these steps-

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

1. Create a Project

Create a Flutter project in the IDE say Android Studio. Go to the lib folder and open the main.dart file. Call a class from here that will have a Snackbar demo.

2. Create a Scaffold Widget

Now create a Scaffold widget responsible for the visual structure and add the other properties to see. Add a button, by clicking it, you will see the snackbar at the bottom of the app.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

3. Display a SnackBar Widget

Now to display a snackbar, just click on the elevated button and you will see the snackbar. This uses the ScaffoldMessenger and showSnackBar methods to show the snackbar.

If you use FAB, see how we see the snackbar and we can change the location of the snackbar with the behavior property.

If the property is fixed, then it will be below the FAB and if it is floationg, it will be above the FAB.

displaying a snackBar widget with fixed behavior

displaying a snackBar widget with floating behavior

4. Provide an Optional Action

We can provide an option for the user to perform some action if a user deletes a message or clicks on something accidentally and wants to undo the action. For this, we use the action property of the snackbar.

Below is the final code of the snackbar.dart class, which has all the code in this file.

This is how it will look on the screen.

example snackbar app in flutter

For full code and download this, check my repository link

Note:

If you have to use a snackbar throughout your Flutter app, then you can make a method for this in a class. Call this method any time at any place to show the snackbar.

Make an AppUtils class and make a method inside it to show the snackbar. Make content dynamic, as you may have to pass different messages on different screens like below.

Remember to add the scaffoldMessengerKey property and pass the messangerKey to this, in the MaterialApp in the root class shown below.

Use the method of this class as shown below.

Conclusion

So, we have learned a lot about snackbar, practically and theoretically in our Flutter application. Below is the sum up of all the discussed points.

  1. Snackbar is a valuable widget and is widely used in many Flutter applications.
  2. We use snackbars if we want to show some alerts, notifications, or messages to the user.
  3. We can customize the appearance of snackbars like width, background color, duration time, etc.
  4. We can define the entrance and exit animations for the snackbar using the animation property.
  5. You might need to change the location of the snackbar if you are using the Floating Action button. Just pass SnackBarBehavior.floating to the behavior property of snackbar.
  6. All other properties of the snackbar are discussed above with a sample app.