Meet MaterialApp
MaterialApp is the root widget of every Flutter app. It sets up themes, navigation, and the overall look and feel. Think of it as the foundation of your house — everything else is built on top of it. Every Flutter app starts with MaterialApp in the main function.
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
title: 'My Flutter App',
theme: ThemeData(
primarySwatch: Colors.blue,
brightness: Brightness.light,
),
home: MyHomePage(),
),
);
}
The title is what appears in the task switcher. The theme defines your app's color scheme and visual style. And home is the first screen your users see. Simple, but powerful.
Try it Yourself →Understanding Scaffold
Scaffold provides the basic visual layout structure. It's like the rooms inside your house — it gives you the walls, the ceiling, and the floor to work with. Scaffold has properties for an AppBar at the top, a body for your main content, a FloatingActionButton, drawers, and bottom navigation.
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('My App'),
actions: [
IconButton(
icon: Icon(Icons.search),
onPressed: () {},
),
IconButton(
icon: Icon(Icons.settings),
onPressed: () {},
),
],
),
body: Center(
child: Text('Hello, Scaffold!'),
),
floatingActionButton: FloatingActionButton(
onPressed: () {},
child: Icon(Icons.add),
),
);
}
}
Try it Yourself →
Adding Drawers and Bottom Navigation
Scaffold makes it easy to add complex layout elements. A Drawer slides in from the left side — great for navigation menus. BottomNavigationBar lets users switch between different sections of your app. Both are built right into Scaffold.
class MainScreen extends StatefulWidget {
@override
_MainScreenState createState() => _MainScreenState();
}
class _MainScreenState extends State<MainScreen> {
int _currentIndex = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('My App')),
drawer: Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: [
DrawerHeader(
decoration: BoxDecoration(color: Colors.blue),
child: Text('Menu', style: TextStyle(color: Colors.white, fontSize: 24)),
),
ListTile(
leading: Icon(Icons.home),
title: Text('Home'),
onTap: () => Navigator.pop(context),
),
ListTile(
leading: Icon(Icons.person),
title: Text('Profile'),
onTap: () {},
),
ListTile(
leading: Icon(Icons.settings),
title: Text('Settings'),
onTap: () {},
),
],
),
),
body: Center(
child: Text('Current tab: $_currentIndex'),
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: _currentIndex,
onTap: (index) => setState(() => _currentIndex = index),
items: [
BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
BottomNavigationBarItem(icon: Icon(Icons.search), label: 'Search'),
BottomNavigationBarItem(icon: Icon(Icons.person), label: 'Profile'),
],
),
);
}
}
Try it Yourself →
Themes and Styling
MaterialApp's theme property controls your app's entire visual appearance. You define primary colors, text styles, button shapes, and more in one place. This ensures consistency across your entire app without repeating style code everywhere.
MaterialApp(
title: 'Styled App',
theme: ThemeData(
primarySwatch: Colors.indigo,
scaffoldBackgroundColor: Colors.grey[50],
appBarTheme: AppBarTheme(
elevation: 0,
centerTitle: true,
),
cardTheme: CardTheme(
elevation: 2,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
elevatedButtonTheme: ElevatedButtonThemeData(
style: ElevatedButton.styleFrom(
padding: EdgeInsets.symmetric(horizontal: 24, vertical: 12),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
),
home: MyHomePage(),
)
This structure gives you a professional-looking app with minimal code. MaterialApp handles the heavy lifting of layout and theming, so you can focus on building your features. That's the beauty of Flutter — it does the hard work so you don't have to.
Try it Yourself →