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,
      ),
      .......
    );
  }
相关推荐
木易士心9 分钟前
Android Handler 机制原理详解
android·app
用户20187928316715 分钟前
CoroutineDispatcher的"自由精灵" - Dispatchers.Unconfined
android
用户20187928316716 分钟前
用 “奶茶连锁店的部门分工” 理解各种 CoroutineScope
android
勤劳打代码1 小时前
触类旁通 —— Flutter 与 React 对比解析
前端·flutter·react native
黄额很兰寿1 小时前
深入源码理解LiveData的实现原理
android
黄额很兰寿1 小时前
flow 的冷流和热流 是设么有什么区别?
android
Digitally1 小时前
如何将 Android 联系人备份到 Mac 的 4 种简单
android·macos
2501_915918412 小时前
iOS 混淆与 IPA 加固一页式行动手册(多工具组合实战 源码成品运维闭环)
android·运维·ios·小程序·uni-app·iphone·webview
不吃凉粉10 小时前
Android Studio USB串口通信
android·ide·android studio
zhangphil10 小时前
android studio设置大内存,提升编译速度
android·android studio