Flutter GetX routing in web

Viewed 1040

I want to know if it is possible to push my initial route screen before accessing another screen. It is my first time using flutter web and GetX and I don't know if it's intended behavior or not.

As for now, when I try to access TransactionDetailPage directly from the URL by typing http://localhost:64289/transaction/1, it doesn't push my MainHomePage screen. There's no back button in TransactionDetailPage AppBar when I access it this way.

What I want to do is to push MainHomePage on the first stack of the screen whenever the user tries to access some other page directly from URL, so there's always a back button to pop the current screen to my home page. How can I achieve this?

main.dart

Widget build(BuildContext context) {
    return GetMaterialApp(
      title: 'Flutter test',
      debugShowCheckedModeBanner: false,
      initialRoute: '/',
      getPages: [
        GetPage(
           name: '/',
           page: () => MainHomePage(),
        ),
        GetPage(
           name: '/transaction/:id',
           page: () => TransactionDetailPage(),
        ),
      ],
    );
  }
2 Answers

Give MainHomePage a name for routing, like this:

GetPage(
          name: '/home',
          page: () => MainHomePage(),
       ),

Also change initial route: initialRoute: '/home'

Use Get.toNamed('/transaction'); to navigate to transaction page.

Also use Get.back(); to back to home page.

class MyApp extends StatelessWidget {
  var _isLoged = GetStorage();
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      initialBinding: HomeBinding(),
      debugShowCheckedModeBanner: false,
      title: 'Your project name',
      getPages: AppPages.pages,
      initialRoute: '/home',
    );
  }
}

// Create a roots folder and add app_routs.dart

class AppRoutes {
  static const HOME = '/home';
}

// the most important thing is to do the following steps // create roots> app_page.dart and add this code

final _binding = HomeBinding();

class AppPages {
  static final home = GetPage(
    name: AppRoutes.HOME,
    page: () => Home(),
    binding: _binding,
  );
 static final List<GetPage> pages = [
    home,
  ];
}
Related