Flutter-路由配置

配置

final routes = {
  '/': (context, {arguments}) => Tabs(),
  ///需要传值路由
  '/form': (context, {arguments}) => FormValuePage(
        arguments: arguments,
      ),
  '/productinfo': (context, {arguments}) => ProductInfoPage(
        arguments: arguments,
      ),
  ///不需要传值路由
  '/search': (context) => SearchPage(),
};

// ignore: prefer_function_declarations_over_variables
var jyOnGenerateRote = (RouteSettings settings) {
  final String? name = settings.name;

  final Function pageContentBuilder = routes[name] as Function;

  // ignore: unnecessary_null_comparison
  if (pageContentBuilder != null) {
    if (settings.arguments != null) {
      final Route route = MaterialPageRoute(
        builder: (context) =>
            pageContentBuilder(context, arguments: settings.arguments),
      );
      return route;
    } else {
      final Route route = MaterialPageRoute(
        builder: (context) => pageContentBuilder(context),
      );
      return route;
    }
  }
  return null;
};

初始路由

return MaterialApp(
      onGenerateRoute: jyOnGenerateRote,
      //初始化路由
      initialRoute: '/',
    );

 

posted @ 2022-03-25 11:24  macroK  阅读(158)  评论(0编辑  收藏  举报