如何在Flutter中从ModalBottomSheet刷新ParentStatefulWidget的状态 [英] How to refresh the states of ParentStatefulWidget from ModalBottomSheet in flutter

查看:62
本文介绍了如何在Flutter中从ModalBottomSheet刷新ParentStatefulWidget的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手,对小部件的状态不了解.我正在创建一个应用程序,要在其中从主屏幕中向购物车中添加一些商品,然后单击购物车"按钮,以打开ModalBottomSheet,用户还可以修改其购物车商品,以及当用户关闭ModalBottomSheet而无需继续操作时想要的商品查看.它也刷新主屏幕上的选定项目.我正在计算每个项目在列表中的加法和减法.一切正常,唯一不起作用的是主屏幕项目不会自动更新,直到我单击该项目,然后它们才能正常工作.

I'm new to flutter and I don't have proper idea about the states of a widget. I'm creating an application where I'm adding some items to the cart from home screen and by clicking on Cartbutton I'm opening ModalBottomSheet where user can also modify their cart items, and what I want when user close the ModalBottomSheet without proceeding for checkout. it refreshes the selected items at home screen as well. I'm calculating addition and subtraction of each item into a list. Everything is working fine, the only thing which is not working is home screen items did not updates automatically untill I click on to that item and then they works fine.

基本上,我想问一下关闭它时如何更新BottomModalSheet的Parent状态.

这是我打开BottomModalSheet的Parent代码的一部分:

Here is a part of code of Parent where I'm opening BottomModalSheet:

  child: Material(
                          color: Colors.transparent,
                          child: InkWell(
                            splashColor: Colors.black12,
                            onTap: () async{

                               await scaffoldKey.currentState
                                  .showBottomSheet((context) =>
                                 StatefulBuilder(
                                   builder: (BuildContext context,StateSetter setState){
                                     return  Container(
                                       color: Colors.white,
                                       height: MediaQuery.of(context).size.height,

                                       child: Column(
                                         mainAxisAlignment: MainAxisAlignment.start,
                                         crossAxisAlignment: CrossAxisAlignment.start,
                                         children: <Widget>[
                                           Container(
                                             height: 200,
                                             color: Colors.black,
                                           ),

这是我要设置状态的BottomModalSheet的一部分:

And This is the part of BottomModalSheet where I'm Setting the state:

                                                        InkWell(
                                                         onTap: (){

                                                                               totalItems.remove(1);
                                                                               totalPrice.remove(int.parse(categoryItemList[index].OurPrice));
                                                                               cart.remove(categoryItemList[index]);
                                                                               setState(() {<----------------------------------------Here I'm calling setState()
                                                                                 if(categoryItemList[index].Counter<2)
                                                                                 {
                                                                                   categoryItemList[index].ShouldVisible = !categoryItemList[index].ShouldVisible;

                                                                                   if(totalItems.length<1)
                                                                                   {
                                                                                     showCart = !showCart;
                                                                                   }
                                                                                 }else{
                                                                                   categoryItemList[index].Counter--;
                                                                                 }

                                                                               });
                                                                               print(categoryItemList[index].Counter);
                                                                               //  print(searchedItemList[index].Counter);
                                                                             }
                                                                             ,child: Container(
                                                                             height: 30,

                                                                             child: Icon(Icons.remove,color: Colors.green,size: 18,))
                                                                         ),

推荐答案

每个Navigator推送方法都是异步的-您可以等到它弹出

Every Navigator push metod is async - you can wait till it poped

引擎盖下的showModalBottomSheet将路线推送到导航器,因此您可以在bottomSheet这样关闭后重建父级

showModalBottomSheet under the hood pushes route to navigator, so you can rebuild your parent after bottomSheet closed this way

// somewhere in statefull parent
onTap:() async {
  await showModalBottomSheet();
  setState((){});
}

这篇关于如何在Flutter中从ModalBottomSheet刷新ParentStatefulWidget的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆