1 controller
class NestedController extends GetxController {
static NestedController get to => Get.find();
var currentIndex = 0.obs;
final pages = ['/list', '/detail', '/login'];
void changePage(int index) {
currentIndex.value = index;
Get.toNamed(pages[index], id: 1);
}
Route? onGenerateRoute(RouteSettings settings) {
if (settings.name == "/login") {
return GetPageRoute(
settings: settings,
page: () => LoginView(),
);
} else if (settings.name == "/list") {
return GetPageRoute(
settings: settings,
page: () => MyListView(),
);
} else if (settings.name == "/detail") {
return GetPageRoute(
settings: settings,
page: () => DetailView(),
);
}
return null;
}
}
2 bindings
class NestedBinding extends Bindings {
@override
void dependencies() {
Get.lazyPut(() => NestedController());
}
}
3 view
class NestedNavView extends GetView<NestedController> {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("嵌套路由")),
body: Container(
color: Colors.amber,
child: Column(
children: [
Container(
child: Text("占位条"),
height: 100,
),
SizedBox(
height: 300,
child: Navigator(
key: Get.nestedKey(1),
initialRoute: '/list',
onGenerateRoute: controller.onGenerateRoute,
),
)
],
)),
bottomNavigationBar: Obx(() => BottomNavigationBar(items: [
BottomNavigationBarItem(
icon: Icon(Icons.list),
label: "列表",
),
BottomNavigationBarItem(
icon: Icon(Icons.details),
label: '详情',
),
BottomNavigationBarItem(
icon: Icon(Icons.login),
label: '登录',
),
],
currentIndex: controller.currentIndex.value,
selectedItemColor: Colors.pink,
onTap: controller.changePage,
)),
);
}
}