Sub topic 2
MaterialApp: the root of a Material app
This chapter assumes you know basic Dart classes, named parameters, StatelessWidget, and the build method. You also need a working Flutter SDK and a project that can run on a device or emulator.
MaterialApp is usually the root widget of a Flutter application that follows Material Design. It creates the application-level environment that widgets below it use. This environment includes theme data, navigation, localization support, and services such as ScaffoldMessenger.
A typical widget tree begins like this:
MaterialApp does not normally draw the visible page content itself. Instead, it selects a screen, such as HomePage, and provides shared configuration to that screen.
The smallest useful app
The home property tells MaterialApp which widget to display first. That widget often returns a Scaffold, which supplies the visible structure of the screen.
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Inbox',
home: const HomePage(),
);
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Inbox'),
),
body: const Center(
child: Text('No messages'),
),
);
}
}The call to runApp attaches MyApp to Flutter's rendering system. MyApp returns MaterialApp, and MaterialApp displays HomePage through its home property.
The title property is application metadata. It can be used by the operating system or accessibility tools. It is not the same as the visible AppBar title.
What MaterialApp provides
| Property or service | Purpose |
|---|---|
| `home` | The first screen shown by the app |
| `theme` | Visual defaults for light mode |
| `darkTheme` | Visual defaults for dark mode |
| `themeMode` | Chooses light, dark, or system mode |
| `routes` | Maps route names to screens |
| `scaffoldMessengerKey` | Provides app-level access to SnackBars |
Widgets below MaterialApp can read inherited configuration through BuildContext. For example, Theme.of(context) reads the active theme. A ScaffoldMessenger can display a SnackBar for the current Scaffold.
Configure a Material 3 theme
A theme is a set of shared visual rules. ColorScheme.fromSeed generates a coordinated color scheme from one seed color. This is preferable to assigning unrelated colors to every widget.
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Inbox',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.teal,
),
useMaterial3: true,
),
darkTheme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.teal,
brightness: Brightness.dark,
),
useMaterial3: true,
),
themeMode: ThemeMode.system,
home: const HomePage(),
);
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Inbox')),
body: Center(
child: Text(
'Welcome',
style: Theme.of(context).textTheme.headlineMedium,
),
),
);
}
}themeMode: ThemeMode.system follows the device's light or dark setting. ThemeMode.light always uses theme, while ThemeMode.dark always uses darkTheme.
Common errors
A frequent mistake is placing MaterialApp inside every screen. This creates multiple application environments and can cause confusing theme or navigation behavior. Place one primary MaterialApp near the root, then return Scaffold widgets from individual screens.
Another mistake is calling Theme.of(context) from a context that is above MaterialApp. That context cannot see the theme provided by MaterialApp. Call it from a descendant widget, such as HomePage.
When an app needs multiple screens, MaterialApp can also define named routes:
MaterialApp(
initialRoute: '/',
routes: {
'/': (context) => const HomePage(),
'/settings': (context) => const SettingsPage(),
},
)The route table belongs at the application level because navigation connects screens across the whole app.
Exercise
Why should an app normally have one primary MaterialApp instead of one MaterialApp per screen?
Show answer
MaterialApp provides shared theme, navigation, and application services. One root keeps those settings consistent for all screens.