TExcelTAD
Home › Flutter › Dialogs & SnackBars

Flutter Dialogs & SnackBars

Sometimes an app needs to tell the user something or ask a quick question without leaving the current screen. Flutter offers three common tools for this:

Widget Use it for Blocks the screen?
SnackBar Short, non-critical feedback ("Message sent") No
AlertDialog Important info or a yes/no decision Yes
Bottom sheet A small menu or extra options sliding up from the bottom Modal ones do

SnackBar

A SnackBar appears briefly at the bottom of the screen and disappears on its own. You show it through the ScaffoldMessenger, which MaterialApp provides.

flutter · try it yourself
import 'package:flutter/material.dart';

void main() => runApp(const MaterialApp(home: Scaffold(body: SaveButton())));

class SaveButton extends StatelessWidget {
  const SaveButton({super.key});

  @override
  Widget build(BuildContext context) {
    return Center(
      child: FilledButton(
        child: const Text('Save'),
        onPressed: () {
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(
              content: const Text('Draft saved'),
              action: SnackBarAction(
                label: 'UNDO',
                onPressed: () => debugPrint('Undo pressed'),
              ),
            ),
          );
        },
      ),
    );
  }
}
Undo pressed

Useful SnackBar options are duration (default 4 seconds), behavior: SnackBarBehavior.floating (a rounded bar that floats above the bottom edge) and showCloseIcon: true.

AlertDialog

showDialog displays a dialog above the current screen and returns a Future that completes when the dialog is closed. Whatever you pass to Navigator.pop(context, value) becomes the result — perfect for confirmations.

flutter · try it yourself
Future<void> _confirmDelete(BuildContext context) async {
  final confirmed = await showDialog<bool>(
    context: context,
    builder: (context) => AlertDialog(
      title: const Text('Delete photo?'),
      content: const Text('This cannot be undone.'),
      actions: [
        TextButton(
          onPressed: () => Navigator.pop(context, false),
          child: const Text('Cancel'),
        ),
        FilledButton(
          onPressed: () => Navigator.pop(context, true),
          child: const Text('Delete'),
        ),
      ],
    ),
  );
  debugPrint('Confirmed: ${confirmed ?? false}');
}
Confirmed: true

If the user taps outside the dialog it closes with null, which is why the code uses confirmed ?? false. Set barrierDismissible: false on showDialog to force a button press.

Tip: showAdaptiveDialog together with AlertDialog.adaptive shows a Cupertino-style dialog on iOS and a Material one on Android.

Bottom sheets

showModalBottomSheet slides a panel up from the bottom. It is great for share menus or a list of actions.

flutter · try it yourself
void _showOptions(BuildContext context) {
  showModalBottomSheet(
    context: context,
    showDragHandle: true,
    builder: (context) => Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        ListTile(
          leading: const Icon(Icons.share),
          title: const Text('Share'),
          onTap: () => Navigator.pop(context),
        ),
        ListTile(
          leading: const Icon(Icons.link),
          title: const Text('Copy link'),
          onTap: () => Navigator.pop(context),
        ),
        const SizedBox(height: 16),
      ],
    ),
  );
}

Which context?

ScaffoldMessenger.of(context) and showDialog(context: …) need a context that is below MaterialApp. If you call them from the same build method that creates MaterialApp, you'll get an error — move the button into its own widget (as in the examples above) or use a Builder.

Note: Dialogs and bottom sheets are pushed onto the Navigator like routes, which is why Navigator.pop closes them.

Test yourself

Which object do you use to show a SnackBar?

  1. Navigator
  2. ScaffoldMessenger
  3. Theme
  4. MediaQuery

What does showDialog return if the user taps outside a dismissible dialog?

  1. true
  2. false
  3. null
  4. It throws an exception

Which is best for a brief, non-blocking confirmation like "Message sent"?

  1. AlertDialog
  2. SnackBar
  3. A new route
  4. showModalBottomSheet