将路由提取到单独的 dart 文件中

Extract routes into separate dart files

我有一个超过 50 行的路线列表,我想简化它,因为它又长又乱。以下是当前路线的示例。无论如何我可以将它提取到单独的文件中吗?例如,将 //2 放入 routes2.dart 文件,将 //3 放入 routes3.dart 文件?我如何在 MyApp 中给他们回电话?

class MyApp extends StatelessWidget {

    @override
    Widget build(BuildContext context) {


        return MultiProvider(
          providers: [
            ChangeNotifierProvider( create: (_) => SettingsNotifier()),
            ChangeNotifierProvider(create: (_) => InterstitialTimerNotifier()),
            ChangeNotifierProvider(create: (_) => InterstitialReadyNotifier()),
            ChangeNotifierProvider(create: (_) => PolicyNotifier()),
          ],
          child: MaterialApp(

            themeMode:ThemeMode.system,
            theme: ThemeClass.lightTheme,
            darkTheme: ThemeClass.darkTheme,

            initialRoute: HomeScreen.id,
            routes: {


              //1. Home
              HomeScreen.id: (context) =>  HomeScreen(),

              //2. Puasa
              PuasaScreen.id: (context) => PuasaScreen(),
              DoaBerbuka.id: (context) => DoaBerbuka(),
              NiatPuasa.id: (context) => NiatPuasa(),
              WaktuMustajabDoa.id: (context) => WaktuMustajabDoa(),
              PuasaPengenalanScreen.id: (context) => PuasaPengenalanScreen(),
              PuasaReference.id: (context) => PuasaReference(),


              //3. Solat Tarawih
              TarawihScreen.id: (context) => TarawihScreen(),
              TarawihPengenalanScreen.id: (context) => TarawihPengenalanScreen(),
              TarawihNiat.id:(context) => TarawihNiat(),
              TarawihTakrif.id: (context) => TarawihTakrif(),
              TarawihDalilScreen.id: (context) => TarawihDalilScreen(),
              TarawihHukum.id: (context) => TarawihHukum(),
              TarawihReference.id: (context) => TarawihReference(),
              TarawihWaktuSolat.id: (context) => TarawihWaktuSolat(),
              TarawihKelebihanScreen.id: (context) => TarawihKelebihanScreen(),
              TarawihBilanganRakaat.id: (context) => TarawihBilanganRakaat(),
              Tarawih8Screen.id: (context) => Tarawih8Screen(),
              Tarawih8TatacaraScreen.id: (context) => Tarawih8TatacaraScreen(),

              //4. Solat Witir
              Tarawih8WitirScreen.id: (context) => Tarawih8WitirScreen(),
              Tarawih8WitirRakaat1.id: (context) => Tarawih8WitirRakaat1(),
              Tarawih8WitirRakaat2.id: (context) => Tarawih8WitirRakaat2(),
              Tarawih8WitirRakaat3.id: (context) => Tarawih8WitirRakaat3(),
              TarawihTahlil.id: (context) => TarawihTahlil(),

             //There're more routes after this

              }),
          );
      }
}

routes:需要Map<String, WidgetBuilder>,你可以创建

一个飞镖文件并创建一个地图,

final Map<String, WidgetBuilder> route2 = {
  PuasaScreen.id: (context) => PuasaScreen(),
  .....
};

routes3.dart也是如此。

并且您可以在 MaterialApp 上添加这些路线,例如

return MaterialApp(
  routes: {
    HomeScreen.id: (context) => HomeScreen(),
    ...route2, /// variable name
    ...route3,
  },
);

这是应用@OMiShah 的建议后更新的代码。

  1. 我为路线创建了新的飞镖文件。例如,route_puasa.dart:
import ...

Map routes_puasa = {
  PuasaScreen.id: (context) => PuasaScreen(),
  DoaBerbuka.id: (context) => DoaBerbuka(),
  NiatPuasa.id: (context) => NiatPuasa(),
  WaktuMustajabDoa.id: (context) => WaktuMustajabDoa(),
  PuasaPengenalanScreen.id: (context) => PuasaPengenalanScreen(),
  PuasaReference.id: (context) => PuasaReference(),
};
  1. 然后使用 ... Spread Operator
  2. MyApp 中调用它
routes: {
          ...routes_puasa,
          ...route_tarawih,
          ...routes_tarawih20,
          ...routes_zakat,
          
          }

完美运行!