美文网首页
flutter 分享代码(四)

flutter 分享代码(四)

作者: Victory_886 | 来源:发表于2019-08-05 18:57 被阅读0次
    • 一、首先在MaterialApp中设置一下路由。
      代码如下:
    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.red,
          ),
          home: MyHomePage(),
          onGenerateRoute: onGenerateRoute,  /// 这里是配置路由
        );
      }
    }
    
    • 二、创建一个路由管理类,具体代码如下:
    
    final routesMap = {
      "/root1": (context) => new MusicQQPage(),
      "/root2": (context) => new MusicXMPage(),
      "/root3": (context) => new MusicMainPage(),
    };
    
    var onGenerateRoute = (RouteSettings settings) {
      final String name = settings.name;
      final Function pageContentBuilder = routesMap[name];
      if (pageContentBuilder != null) {
        Route route;
        if (settings.arguments != null) {
          route = MaterialPageRoute(
              builder: (context) =>
                  pageContentBuilder(context, arguments: settings.arguments));
        } else {
          route =
              MaterialPageRoute(builder: (context) => pageContentBuilder(context));
        }
        return route;
      }
    };
    
    
    • 三、使用方法如下:
    Navigator.pushNamed(context, "/root1");
    

    相关文章

      网友评论

          本文标题:flutter 分享代码(四)

          本文链接:https://www.haomeiwen.com/subject/gahvdctx.html