Displaying SnackBars in Flutter: A Step-by-Step Guide with Examples
Learn how to effectively use SnackBars in Flutter for displaying temporary notifications to the user with easy to follow examples and code snippets
What is the SnackBar class?
SnackBar is a Flutter widget that enables you to temporarily display a pop-up message in your app. It usually appears at the bottom of the app’s screen.
Notes:
- Always keep in mind that the SnackBar shouldn’t distract the end user from the main goal of the app.
- This is one reason why a SnackBar is typically placed at the bottom of the app screen.
- The recommended duration for a SnackBar to display in a Flutter app is 4 to 10 seconds — no longer.
- It’s a good practice to implement some kind of interactive element to accompany your message.
- For example, you might create an action button with a label such as “Dismiss” or “Try again” and attach it to the SnackBar widget.
// Showing a snack bar using ScaffoldMessenger
ScaffoldMessenger.of(_scaffoldCtx).showSnackBar(
SnackBar(
// Setting the elevation to 0 to make the snack bar flat
elevation: 0,
content: Card(
// Rounding the border of the card
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(5)),
// Setting the clipping behavior for the card
clipBehavior: Clip.antiAliasWithSaveLayer,
elevation: 1,
child: Container(
// Adding padding to the container
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
child: Row(
children: [
// Adding space to the left side
const SizedBox(width: 5, height: 0),
Expanded(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
// Displaying the product name with a bold font
TextStyleExample(name : 'Iphone 11',style : textTheme.labelLarge!.copyWith(color: Theme.of(context).colorScheme.primary)),
// Displaying the action that has been taken
TextStyleExample(name : 'Has Been Removed',style : textTheme.labelSmall!),
],
),
),
// Adding a vertical line between the product name and the undo button
Container(color: Colors.grey, height: 35, width: 1, margin: const EdgeInsets.symmetric(horizontal: 5)),
SnackBarAction(
// Displaying the undo button
label: "UNDO",
textColor: Theme.of(context).colorScheme.primary,
// Callback function for when the undo button is pressed
onPressed: (){},
),
],
),
),
),
// Setting the background color to transparent
backgroundColor: Colors.transparent,
// Setting the duration for how long the snack bar should be visible
duration: const Duration(seconds: 3),
),
);
// Showing a Snackbar with a custom design
ScaffoldMessenger.of(_scaffoldCtx).showSnackBar(SnackBar(
// Setting the elevation to 0 to make the Snackbar look flat
elevation: 0,
// Content of the Snackbar
content: Card(
// Adding a rounded rectangle border to the Snackbar content
shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(5),),
// Setting the clip behavior for the Snackbar content
clipBehavior: Clip.antiAliasWithSaveLayer,
// Adding a small elevation to the Snackbar content to give it a raised effect
elevation: 1,
// The main container for the Snackbar content
child: Container(
// Adding padding to the container
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
// A row widget to arrange the content inside the container
child: Row(
children: [
// Adding a spacer with a width of 5
const SizedBox(width: 5, height: 0),
// Adding an image asset of the chat icon
Image.asset(Img.get('chat.png'),
height: 40, width: 40,
),
// Adding a spacer with a width of 10
const SizedBox(width: 10, height: 0),
// A column widget to arrange the text content
Expanded(child: Column(
// Setting the main axis size to minimum
mainAxisSize: MainAxisSize.min,
// Aligning the content to the start of the column
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
// Displaying the name of the item "Black-forest" with a custom style
TextStyleExample(name : 'Iphone 11',style : textTheme.labelLarge!.copyWith(color: Theme.of(context).colorScheme.primary)),
// Show the text "Has Been Removed" with a caption style
TextStyleExample(name : 'Has Been Removed',style : textTheme.labelSmall!.copyWith(color: Theme.of(context).colorScheme.primary)),
],
)),
// Adding a separator line with a color of Colors.grey
Container(color: Colors.grey, height: 35, width: 1, margin: const EdgeInsets.symmetric(horizontal: 5)),
// Adding an undo button
SnackBarAction(
// Setting the label for the undo button
label: "UNDO",
// Setting the text color for the undo button
textColor: Colors.black,
// Setting the callback function for when the undo button is pressed
onPressed: (){},
)
],
),
),
),
// Setting the background color of the Snackbar to be transparent
backgroundColor: Colors.transparent,
// Setting the duration for how long the Snackbar will be displayed
duration: const Duration(seconds: 3),
));
// Create a SnackBar widget
final snackBar = SnackBar(
// The text to display in the SnackBar
content: const Text('Yay! A SnackBar!'),
// The action to display in the SnackBar
action: SnackBarAction(
// The label for the action button
label: 'Undo',
// Callback function to be executed when the action button is pressed
onPressed: () {
// Some code to undo the change.
},
),
);
// Find the ScaffoldMessenger in the widget tree
// and use it to show the SnackBar.
// The _scaffoldCtx variable holds the context of the Scaffold widget
ScaffoldMessenger.of(_scaffoldCtx).showSnackBar(snackBar);
Comments
Post a Comment