What is Scaffold in Flutter: Explanation and Example
Scaffold is a layout structure that provides a basic visual framework for your app, including app bars, drawers, and floating action buttons. It helps you build a consistent and standard app screen easily by handling common UI elements.How It Works
Think of Scaffold as the skeleton of your app's screen. It holds the main parts like the top bar, the main content area, and buttons floating above the content. Just like a house frame supports walls, windows, and doors, Scaffold supports these UI pieces so you don't have to build them from scratch.
When you use Scaffold, you get a ready-made structure that manages layout and behavior for common app features. For example, it automatically handles the space for the app bar and the floating button, so your content doesn't get hidden behind them. This makes your app look neat and organized without extra work.
Example
This example shows a simple app screen using Scaffold with an app bar, a centered text, and a floating action button.
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: Text('Scaffold Example'), ), body: Center( child: Text('Hello, Scaffold!'), ), floatingActionButton: FloatingActionButton( onPressed: () {}, child: Icon(Icons.add), ), ), ); } }
When to Use
Use Scaffold whenever you want to create a new screen in your Flutter app that needs a standard layout with an app bar, body content, and optional floating buttons or drawers. It saves time by providing these common parts out of the box.
For example, if you are building a chat app, each chat screen can use Scaffold to show the chat messages in the body, a top bar with the chat name, and a floating button to add new messages. It helps keep your app consistent and easy to navigate.
Key Points
- Scaffold provides a basic visual layout structure for Flutter apps.
- It manages app bars, body content, floating action buttons, drawers, and more.
- Using Scaffold ensures consistent and standard UI across different screens.
- It handles layout details so your content is not hidden behind UI elements.