Snackbar Variants
Loading preview
Starting the Flutter app
Simple, action, icon, error, and persistent snackbar styles
import 'package:flutter/material.dart';
class SnackbarVariants extends StatelessWidget {
const SnackbarVariants({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Snackbar Variants')),
body: ListView(
padding: const EdgeInsets.all(16),
children: [
FilledButton(
onPressed: () => ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Simple snackbar')),
),
child: const Text('Simple'),
),
const SizedBox(height: 8),
FilledButton(
onPressed: () => ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: const Text('Action snackbar'),
action: SnackBarAction(
label: 'Undo',
onPressed: () => _showDemo(context, 'Undone (demo)'),
),
),
),
child: const Text('With Action'),
),
const SizedBox(height: 8),
FilledButton(
onPressed: () => ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Row(
children: [
Icon(Icons.check_circle, color: Colors.white),
SizedBox(width: 12),
Expanded(child: Text('Success with icon')),
],
),
backgroundColor: Colors.green,
),
),
child: const Text('With Icon'),
),
const SizedBox(height: 8),
FilledButton(
onPressed: () => ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: const Text('Error message'),
backgroundColor: Theme.of(context).colorScheme.error,
),
),
child: const Text('Error Style'),
),
],
),
);
}
}
void _showDemo(BuildContext context, String message) {
ScaffoldMessenger.of(context)
..hideCurrentSnackBar()
..showSnackBar(SnackBar(content: Text(message)));
}
Add it to your project
Copies the Dart source into your app, so you own it. Flutter SDK only, no extra packages.
First time? Run dart pub global activate fluttertemplates_dev, or read the CLI guide.
How it works
Four FilledButtons each call ScaffoldMessenger.of(context).showSnackBar with a different configuration: plain, with a SnackBarAction, with a green icon row, and error-colored. Repeated taps queue up rather than replacing the current bar, since nothing hides it first. The Undo callback only confirms with another SnackBar, and the success color is hardcoded while the error one comes from the theme.
Adapting it
- Call
hideCurrentSnackBarbeforeshowSnackBarif newer messages should win. - Put your restore logic in
onPressedonSnackBarAction. - Wrap these in one helper taking the message and type, and replace
Colors.greenwith a theme extension color.