📜  flutter materialApp (1)

📅  最后修改于: 2023-12-03 15:30:48.454000             🧑  作者: Mango

Flutter MaterialApp

MaterialApp is a pre-built widget in Flutter that provides a basic layout structure for building a material design app. With MaterialApp, it's easy to create an app that follows material design guidelines, by enabling the use of material widgets such as AppBar, BottomNavigationBar, Drawer, FloatingActionButton, and many more.

How to use MaterialApp

To use MaterialApp, simply create a new MaterialApp widget, and pass it the required configuration. For example, the following code creates a simple MaterialApp with a Scaffold as its home page:

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    home: Scaffold(
      appBar: AppBar(
        title: Text('My App'),
      ),
      body: Center(
        child: Text('Hello, world!'),
      ),
    ),
  ));
}

The above example creates an app with a simple Scaffold that includes an AppBar and a Text widget. This is the most basic structure of an app, but you can add more widgets to create more complex designs.

MaterialApp Configuration

MaterialApp provides many properties that you can configure to customize the app's appearance and behavior. Some of the most common properties are:

  • title - The title of the app, displayed in the app bar.
  • theme - The color scheme and font style used throughout the app.
  • routes - A map of named routes that can be used to navigate to different pages in the app.
  • initialRoute - The initial route to open when the app is launched.
  • home - The widget to display as the app's home page.

Here's an example of how to use some of these properties:

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    title: 'My App',
    theme: ThemeData(
      primarySwatch: Colors.blue,
    ),
    initialRoute: '/',
    routes: {
      '/': (context) => MyHomePage(),
      '/second': (context) => SecondPage(),
    },
  ));
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('My App'),
      ),
      body: Center(
        child: Text('Home Page'),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {},
        child: Icon(Icons.add),
      ),
    );
  }
}

class SecondPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Second Page'),
      ),
      body: Center(
        child: Text('Second Page'),
      ),
    );
  }
}

In this example, we've set the app's title to "My App", and the primary color to blue. We've also created two named routes - / and /second - and defined two corresponding pages using the MyHomePage and SecondPage widgets.

Conclusion

Using MaterialApp is a great way to quickly build a material design app using Flutter. With its built-in widgets, properties, and configuration options, it's easy to create an app that looks and feels great on both Android and iOS devices.