The Scaffold widget is the skeleton of a Flutter screen. It implements the basic Material Design visual layout structure. Without it, your app is just a black screen or lacks the standard "page" look and feel.
Why do we need Scaffold?
When you start building a screen, you need a canvas to draw on. The Scaffold provides this canvas.
If you do not use a Scaffold:
- You might see a black screen because there is no default background color.
- You miss out on the standard layout structure (like where the top bar or bottom bar goes).
- Text and other elements might look broken or unstyled (e.g., ugly red/yellow default text styles) because the Material foundation is missing.
Scaffold handles the "plumbing" of a screen so you can focus on the content.
Basic Structure
Scaffold fills the available space and provides a structure for common UI elements. It is usually the parent widget of your screen.
Simple Scaffold Example
1 2import 'package:flutter/material.dart';3 4void main() {5 runApp(MyApp());6}7 8class MyApp extends StatelessWidget {9 @override10 Widget build(BuildContext context) {11 return MaterialApp(12 home: Scaffold(13 appBar: AppBar(14 title: Text('My First App'),15 ),16 body: Center(17 child: Text('Hello, World!'),18 ),19 floatingActionButton: FloatingActionButton(20 onPressed: () {},21 child: Icon(Icons.add),22 ),23 ),24 );25 }26}27 HintThis code creates a basic app structure with an AppBar, a centered "Hello, World!" text in the body, and a Floating Action Button.
Key Attributes
The Scaffold has several named arguments that let you slot in different standard UI components:
- appBar: The tool bar at the top of the screen. It usually displays the page title and action buttons (like search or settings).
- body: The primary content area. This takes up the remaining space in the middle. This is where your main lists, text, or images go.
- bottomNavigationBar: A bar at the bottom of the screen, typically used to navigate between primary destinations in your app (like Home, Profile, Settings).
- floatingActionButton: A circular button that hovers over the content, usually in the bottom-right corner, used for a primary action (like "Compose Email" or "Add Item").
- drawer: A hidden panel that slides out from the side (usually the left) for navigation links.
Loot collected
Key takeaways- Scaffold is the root widget for most screens in a Flutter app.
- It prevents the "black screen" issue by providing a standard material design layout.
- It makes it easy to add an AppBar, Body, and BottomNavigationBar.