- Customizing Fonts in Flutter with ExampleFlutter Skeleton Text with ExampleFlutter Themes with ExampleFlutter Lazy Loader with ExampleFlutter UI Orientation with ExampleFlutter Animation in Route Transition with ExampleFlutter Physics Simulation in Animation with ExampleFlutter Radial Hero Animation with ExampleFlutter Hinge Animation with ExampleFlutter Lottie Animation with Example
- URLs in Flutter with ExampleRoutes and Navigator in FlutterFlutter WebSockets with ExampleFlutter Named Routes with ExampleFlutter Arguments in Named RoutesMulti Page Applications in FlutterFlutter Updating Data on the InternetFlutter Fetching Data From the InternetFlutter Deleting Data On The InternetFlutter Sending Data To The InternetFlutter Send Data to Screen with Example
MaterialApp class in Flutter with Example
In Flutter, the MaterialApp
class is a top-level widget that provides a material design-themed application. It is a container for the app's UI and manages the app's navigation stack, theme, and localization. Here are some of the commonly used properties of the MaterialApp
class:
title
: A string that sets the title of the app.theme
: AThemeData
object that sets the theme for the app.home
: A widget that sets the app's home screen.routes
: A map of named routes that define the app's navigation stack.initialRoute
: A string that sets the initial route for the app.onGenerateRoute
: A callback function that generates a route based on a given name.
Here's an example of how to use the MaterialApp
class
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,
),
home: MyHomePage(),
routes: {
'/second': (context) => SecondScreen(),
},
);
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Welcome to my app!',
style: Theme.of(context).textTheme.headline4,
),
RaisedButton(
child: Text('Go to second screen'),
onPressed: () {
Navigator.pushNamed(context, '/second');
},
),
],
),
),
);
}
}
class SecondScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Second Screen'),
),
body: Center(
child: Text(
'This is the second screen.',
style: Theme.of(context).textTheme.headline4,
),
),
);
}
}
ā
In this example, we create a simple app with a home screen and a second screen. We set the app's title to "My App" and its theme to a blue color swatch. We define a named route for the second screen using the routes
property.
In the home screen, we use a Scaffold
widget to create a basic UI with an app bar and a centered column of widgets. We also define a RaisedButton
that, when pressed, navigates to the second screen using the Navigator.pushNamed
method.
In the second screen, we use another Scaffold
widget to create a UI with an app bar and a centered text widget.
By using the MaterialApp
class and its associated properties and widgets, we can easily create a material design-themed app with multiple screens and navigation.