我的期望是,在实例化控制器时,由控制器公开的值应该重置为其初始值。 然而,小部件继续显示来自控制器的最后一个值。
我正在使用Flutter和GetX的最新版本:分别为2.2.3和4.3.8
感谢您的帮助。
代码:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return GetMaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.purple,
),
home: LandingScreen(),
);
}
}
class LandingScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
color: Colors.blue[800],
child: Center(
child: ElevatedButton(
onPressed: () => {
Get.to(MainScreen())
},
child: const Text('Navigate to Second Screen'),
),
),
);
}
}
class MainScreen extends StatelessWidget {
final MyController controller = Get.put(MyController());
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Container(
color: Colors.blueAccent,
child: Center(
child: Column(
children: [
Obx(() => Text('Clicked ${controller.count}')),
FloatingActionButton(
onPressed: controller.increment,
child: Text('+'),
),
ElevatedButton(
onPressed: ()=>{Navigator.of(context).pop()},
child: Text('Go Back'),
)
],
),
),
),
),
);
}
}
class MyController extends GetxController {
var count = 0.obs;
void increment() => count++;
}