If you use Getx for routing, it becomes much easier to manage routing. Just create a new dart class and name it RouteHelper.dart
This class will contain a routes list of List<GetPage> type. The variable would be static type.
Let’s see the class
class RouteHelper {
static const String initial = '/';
static const String splash = '/splash';
static const String language='/language';
static String getSplashRoute() => '$splash';
static String getInitialRoute()=>'$initial';
static String getLanguageRoute()=>'$language';
static List<GetPage> routes = [
GetPage(name: splash, page: () {
return SplashScreen();
}),
GetPage(name: initial, page:(){
return HomePage();
}),
GetPage(name:language, page:(){
return LanguagePage();
})
];
}
The above RouteHelper class static properties and methods. You may call both from the UI. But in general we call the methods from the UI.
Each of those methods refers to GetPage() class. The constructor takes name and page. Name should refer to the static members and page should refer to a widget.
From the UI, inside main.dart, you should do
GetMaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
initialRoute: RouteHelper.getSplashPage(),
getPages: RouteHelper.routes,
theme: ThemeData(
primaryColor: AppColors.mainColor,
fontFamily: "Lato",
)
Set initialRoute and getPages and you are good to go.
And then from UI classes just call like Get.to(RouteHelper.getInitialRoute()) or Get.to(RouteHelper.getLanguage()).
Instead of Get.to(), you may use other Getx routing methods