In Flutter, the MaterialApp widget is the top-level widget that defines the basic material design structure of your application. It sets up the default configurations, themes, and navigation for your app. Here are some key attributes and properties of the MaterialApp widget:
Attributes and Properties:
home: The widget that is the home of the application, typically a Scaffold.
title: The title that appears on the device's system bar.
theme: The overall theme of the application, defining colors, typography, and other visual aspects.
routes: A map of named routes used for navigation within the app.
initialRoute: The initial route the app should navigate to when it starts.
onGenerateRoute: A callback function that allows dynamic generation of routes.
navigatorKey: A key to use when accessing the Navigator from the BuildContext.
navigatorObservers: A list of observers for the Navigator.
debugShowMaterialGrid: A property that shows a grid overlay for material design visual debugging.
Example:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'My App',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home Page'),
),
body: Center(
child: Text(
'Welcome to My App!',
style: TextStyle(fontSize: 20),
),
),
);
}
}
In this example, the MaterialApp widget is used to define the title and theme of the application. The home property is set to an instance of MyHomePage, which is a simple Scaffold with an AppBar and a Center containing a welcome message.
Example:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Material App Example',
theme: ThemeData(
primarySwatch: Colors.green,
accentColor: Colors.orange,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
routes: {
'/second': (context) => SecondPage(),
},
);
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home Page'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Welcome to Material App Example!',
style: TextStyle(fontSize: 20),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
Navigator.pushNamed(context, '/second');
},
child: Text('Go to Second Page'),
),
],
),
),
);
}
}
class SecondPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Second Page'),
),
body: Center(
child: Text(
'This is the Second Page!',
style: TextStyle(fontSize: 20),
),
),
);
}
}
In this full example, the MaterialApp widget is used with additional properties such as routes and theme. Two pages (MyHomePage and SecondPage) are defined, and navigation between them is demonstrated using the Navigator.