如何确定何时筛选简历或离开时使用Getx?例:产品屏幕->购物车屏幕。在购物车屏幕上,我删除了1个产品并返回到产品屏幕,我想更新UI上的购物车数量徽章,但我不知道怎么做?有什么方法可以像onResume()那样被检测到吗?对不起我的英语不好。
发布于 2022-08-18 04:55:04
就像GetX包的作者制作了一个示例计数器应用程序一样,您可以创建自己的应用程序逻辑。
创建一个扩展CartController的GetxController,并在里面放置'cartProductQuantity‘整数变量,并使变量可以被观察到,将.obs放置在它的末尾。
class CartController extends GetxController{
var cartProductQuantity = 0.obs;
increment() => cartProductQuantity++;
decrement() => cartProductQuantity--;
}
class Home extends StatelessWidget {
@override
Widget build(context) {
// Instantiate your class using Get.put() to make it available for all "child" routes there.
final CartController c = Get.put(CartController());
return Scaffold(
// Use Obx(()=> to update Text() whenever quantity badge is changed.
appBar: AppBar(title: Obx(() => Text("Items: ${c.count}"))),
body: Center(child: ElevatedButton(
child: Text("Go to Other"), onPressed: () => Get.to(Other()))),
floatingActionButton:
FloatingActionButton(child: Icon(Icons.add), onPressed: c.increment));
}
}
class Other extends StatelessWidget {
// You can ask Get to find a Controller that is being used by another page and redirect you to it.
final Controller c = Get.find();
@override
Widget build(context){
// Access the updated count variable
return Scaffold(body: Center(child: Text("${c.count}")));
}
}https://stackoverflow.com/questions/73397554
复制相似问题