image.png

1 controller

  1. class NestedController extends GetxController {
  2. static NestedController get to => Get.find();
  3. var currentIndex = 0.obs;
  4. final pages = ['/list', '/detail', '/login'];
  5. void changePage(int index) {
  6. currentIndex.value = index;
  7. Get.toNamed(pages[index], id: 1);
  8. }
  9. Route? onGenerateRoute(RouteSettings settings) {
  10. if (settings.name == "/login") {
  11. return GetPageRoute(
  12. settings: settings,
  13. page: () => LoginView(),
  14. );
  15. } else if (settings.name == "/list") {
  16. return GetPageRoute(
  17. settings: settings,
  18. page: () => MyListView(),
  19. );
  20. } else if (settings.name == "/detail") {
  21. return GetPageRoute(
  22. settings: settings,
  23. page: () => DetailView(),
  24. );
  25. }
  26. return null;
  27. }
  28. }

2 bindings

  1. class NestedBinding extends Bindings {
  2. @override
  3. void dependencies() {
  4. Get.lazyPut(() => NestedController());
  5. }
  6. }

3 view

  1. class NestedNavView extends GetView<NestedController> {
  2. Widget build(BuildContext context) {
  3. return Scaffold(
  4. appBar: AppBar(title: Text("嵌套路由")),
  5. body: Container(
  6. color: Colors.amber,
  7. child: Column(
  8. children: [
  9. Container(
  10. child: Text("占位条"),
  11. height: 100,
  12. ),
  13. SizedBox(
  14. height: 300,
  15. child: Navigator(
  16. key: Get.nestedKey(1),
  17. initialRoute: '/list',
  18. onGenerateRoute: controller.onGenerateRoute,
  19. ),
  20. )
  21. ],
  22. )),
  23. bottomNavigationBar: Obx(() => BottomNavigationBar(items: [
  24. BottomNavigationBarItem(
  25. icon: Icon(Icons.list),
  26. label: "列表",
  27. ),
  28. BottomNavigationBarItem(
  29. icon: Icon(Icons.details),
  30. label: '详情',
  31. ),
  32. BottomNavigationBarItem(
  33. icon: Icon(Icons.login),
  34. label: '登录',
  35. ),
  36. ],
  37. currentIndex: controller.currentIndex.value,
  38. selectedItemColor: Colors.pink,
  39. onTap: controller.changePage,
  40. )),
  41. );
  42. }
  43. }