【发布时间】:2021-11-05 08:01:26
【问题描述】:
我正在尝试使用下一个布局创建简单的应用程序:
按钮应该切换 Widget1\Widget2。
我编写了下一个代码(准备好复制过去):
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:get/get.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return GetMaterialApp(
initialBinding: HomeBinding(),
initialRoute: '/widget1',
home: Scaffold(
appBar: AppBar(
title: Text("Demo"),
),
body: SafeArea(
child: IndexedStack(
index: Get.find<NavigationBarController>().tabIndex.value,
children: [
Widget1(),
Widget2()
],
),
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: Get.find<NavigationBarController>().tabIndex.value,
onTap: Get.find<NavigationBarController>().changeTabIndex,
items: [
_bottomNavigationBarItem(
icon: CupertinoIcons.home,
label: 'Button1',
),
_bottomNavigationBarItem(
icon: CupertinoIcons.rocket_fill,
label: 'Button2',
),
])),
getPages: [
GetPage(
name: '/widget1',
page: () => Widget1(),
binding: Widget1_Bindings(),
),
GetPage(
name: '/widget2',
page: () => Widget2(),
binding: Widget2_Bindings(),
),
],
debugShowCheckedModeBanner: false,
themeMode: ThemeMode.system,
);
}
}
class NavigationBarController extends GetxController {
static NavigationBarController get to => Get.find();
var tabIndex = 0.obs;
void changeTabIndex(int index) {
tabIndex.value = index;
update();
}
@override
void onInit() {
super.onInit();
}
@override
void dispose() {
super.dispose();
}
}
class Controller1 extends GetxController {
var _test2 = "test1".obs;
get test1 => this._test2.value;
}
class Controller2 extends GetxController {
var _test2 = "test2".obs;
get test1 => this._test2.value;
}
class Widget1 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
child: Text("Widget1"),
);
}
}
class HomeBinding extends Bindings {
@override
void dependencies() {
Get.lazyPut(() => NavigationBarController());
}
}
class Widget1_Bindings extends Bindings {
@override
void dependencies() {
Get.lazyPut(() => Controller1());
}
}
class Widget2_Bindings extends Bindings {
@override
void dependencies() {
Get.lazyPut(() => Controller2());
}
}
class Widget2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
child: Text("Widget2"),
);
}
}
_bottomNavigationBarItem({required IconData icon, required String label}) {
return BottomNavigationBarItem(
icon: Icon(icon),
label: label,
);
}
第二个是我不知道如何获取 GetX 路由,例如:Get.to 工作。
【问题讨论】:
标签: flutter dart flutter-getx