Flutter 动态路由添加

  • Post author:
  • Post category:其他


import 'package:flutter/material.dart';
import 'dart:io';
import 'package:flutter/services.dart';
import 'package:demo_app/pages/reload.dart';
import 'package:demo_app/pages/bottom_nav_bar.dart';
import 'package:oktoast/oktoast.dart';
void main(){
  // 强制竖屏显示
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]).then((_) {
    runApp(MyApp());
    if (Platform.isAndroid) {
    // 以下两行 设置android状态栏为透明的沉浸。写在组件渲染之后,是为了在渲染后进行set赋值,覆盖状态栏,写在渲染之前MaterialApp组件会覆盖掉这个值。
    SystemUiOverlayStyle systemUiOverlayStyle =
        SystemUiOverlayStyle(statusBarColor: Colors.transparent);
      SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
    }
  })
}

class MyApp extends StatelessWidget {
  // 路由定义
  final routes = {
    // 预登陆页面
    "reload": (context, {arguments}) => ReloadPage(),
    // 跳转到底部导航页面,默认索引为0(首页)
    "bottom_nav_bar":(context,{arguments})=>BottomNavBarPage(arguments:arguments??0),
  };

  @override
  Widget build(BuildContext context) {
    return  OKToast(
        // 隐藏已出现的 toast
        dismissOtherOnShow: true,
        child:MaterialApp(
        title: '应用程序标题',
        theme: ThemeData(
            appBarTheme: AppBarTheme(
                color: Colors.white,
                textTheme: TextTheme(
                    title: TextStyle(color: Color(0xFF000F41), fontSize: 18.0)),
                iconTheme:
                    IconThemeData(color: Color(0xFF000F41), size: 10.0))),
        // 去掉debug标签
        debugShowCheckedModeBanner: false,
        home: ReloadPage(),
        onGenerateRoute: (RouteSettings settings) {
          // 统一处理
          final String routeName = settings.name;
          final Function pageContainerBuilder = this.routes[routeName];
          if (pageContainerBuilder != null) {
            if (settings.arguments != null) {
              final route = MaterialPageRoute(
                  builder: (context) => pageContainerBuilder(context,
                      arguments: settings.arguments));
              return route;
            } else {
              final route = MaterialPageRoute(
                  builder: (context) => pageContainerBuilder(context));
              return route;
            }
          }
        })
      );
  }
}

欢迎加群讨论更多flutter相关问题(7天有效)如果失效,可加个人微信拉群




版权声明:本文为isusjjyy原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。