Flutter开发 MaterrialApp基本属性介绍

这是目录

home属性

dart 复制代码
class MyApp extends StatelessWidget { //无状态的

  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo', //任务管理窗口中显示的应用程序标题
      theme: ThemeData(
        //主题
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.red),
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page222'), //应用程序默认显示的控件
    );
  }
}

home属性用于指定进入应用程序后显示的第一个画面。

routes

用于应用程序中页面跳转的路由。

示例

点击"第一个页面"跳转到另一个页面。

dart 复制代码
void main() {
  //入口函数
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  //无状态的

  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo', //任务管理窗口中显示的应用程序标题
      theme: ThemeData(
        //主题
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.red),
      ),
      home: SecondPage(),
      routes: {
        '/main': (BuildContext context) => SecondPage(),
        '/thrid': (BuildContext context) => ThridPage(),
      },
    );
  }
}

class SecondPage extends StatelessWidget {
  const SecondPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: GestureDetector(
          onTap: () {
            print("这是主页面");
            Navigator.pushNamed(context, "/thrid");//路由
          },
          child: Text("第一个页面"),
        ),
      ),
    );
  }
}

class ThridPage extends StatelessWidget {
  const ThridPage({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: GestureDetector(
            onTap: () {
              print("这是主页面");
            },
            child: Text("第二个页面"),
          ),
        ),
      ),
    );
  }
}

initialRoute

用于 指定应用程序启动时的初始路由,即应用程序启动后 跳转的第一个页面,应用程序中即使设置了home属性,启动后的第一个页面也是initialRoute路由指定的页面。

dart 复制代码
  Widget build(BuildContext context) {
    return MaterialApp(
      ........
      home: SecondPage(),
      routes: {
        '/main': (BuildContext context) => SecondPage(),
        '/thrid': (BuildContext context) => ThridPage(),
      },
      initialRoute: "/thrid",
      onGenerateRoute: (settings) {//home或者initialRoute错误时,会调用

        return PageRouteBuilder(pageBuilder: (context, animation, secondaryAnimation) {

          return ThridPage();
        },);
      },
    );
  }

theme

用于指定应用程序的主题

dart 复制代码
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      //任务管理窗口中显示的应用程序标题
      theme: ThemeData(
        //主题
        primaryColor: Colors.green,
        primarySwatch: Colors.blue,
      ),
      .......
    );
  }
相关推荐
阿杰m6 分钟前
Android源码第三十章:Android Automotive、TV 与桌面模式
android
晚霞的不甘9 小时前
Flutter/OpenHarmony 三方库适配实战:google_maps_flutter 地图
flutter·macos·华为·cocoa·harmonyos·1024程序员节
意念回复9 小时前
Transformer 系列(五):从 Block 到模型——三大架构、因果掩码与自回归生成
android·回归·transformer
松仔log11 小时前
Kotlin中级——KMP
android·开发语言·kotlin
GY-9311 小时前
【Android】SQL数据库
android
GY-9312 小时前
【Android】适配不同的屏幕尺寸
android
Chenpip113 小时前
安卓 14/15 上装不上:四层原因与三行检查
android·app分发·应用分发·海哈游分发·内测
TT哇13 小时前
【Android 之旅】Android 通俗易懂记
android·gitee
雨白13 小时前
工厂设计模式实战:从简单工厂到抽象工厂的演进之路
android
Digitally14 小时前
7 种主流安卓设备备份与恢复方法
android