FlutterBasic 2 min read

Flutter Scaffold, AppBar and contextual feedback

Build a Material page and show a SnackBar from a context that belongs below the Scaffold.

On this page

Scaffold supplies page-level structure such as an app bar, body, drawer and floating action button. It is a useful boundary for a screen, not a widget that needs to wrap every card.

Use a descendant context

Feedback often depends on an ancestor such as ScaffoldMessenger. A Builder creates a context at its place in the tree, below the surrounding widgets. This avoids looking for a newly created ancestor from a context above it.

Worked example

This is a complete example. Open it with Run example, then use Run inside DartPad.

main.dart
import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(
  home: Scaffold(
    appBar: AppBar(title: const Text('Page-level feedback')),
    body: Builder(builder: (context) => Center(child: FilledButton(
      onPressed: () => ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('Draft saved for this session.'))),
      child: const Text('Save draft'),
    ))),
  ),
));

Read the result

The action displays feedback without changing the page route. Use SnackBars for brief, non-blocking messages. A failure requiring a user decision may need an inline error or dialog instead.

Follow the lookup from the button’s context

The callback uses the context supplied by Builder. That context is a descendant of the Scaffold, so related ancestor lookups happen from the intended place. A context captured above the newly constructed page is not made into a descendant simply because the callback is written nearby in the same function.

A SnackBar is temporary feedback, not durable application state. If saving can fail, the state model should record the result and expose a meaningful retry path; a briefly visible message alone cannot represent a persistent error.

Use BuildContext to understand the lookup and async states to distinguish the request result from the surface used to announce it. Reserve a dialog for a decision that genuinely needs focused interaction.

Practice and verification

Add an Undo action to the SnackBar and connect it to a real state change. Check that a screen-reader user can discover the message and the alternative action.

How this topic connects

Understand first

Apply it next

Lesson progressMark complete after checking the example and exercise.

Find your next idea

Search lessons, code examples and projects.

Type a few words to start exploring.