1

当我使用单选按钮时,我无法在 modalBottomSheet 中更新我的状态。当我更改单选按钮时,我正在从服务器检索数据并且我想更新状态。

这是我的代码

我在这里调用modalBottomSheet:

GestureDetector(
      onTap: () {
         showModalBottomSheet(
         context: context,
         builder:
         buildBottomSheet);
         },
         child: ListTile(
         leading: Icon(
         Icons.shopping_basket),
         title:
         Text("choose bag type "),
         ),
   ),

这是我的带有包含单选按钮的容器的 modalbottomSheet。

  Widget buildBottomSheet(BuildContext context) {
    return StatefulBuilder(builder: (BuildContext context, StateSetter state) {
      return Container(
        color: Color(0xff757575),
        child: Container(
          height: MediaQuery.of(context).size.height / 4,
          decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(25), topRight: Radius.circular(25))),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: <Widget>[
              SizedBox(height: 25),
              RadioListTile(
                groupValue: _con.cart.bagType,
                title: Row(
                  children: <Widget>[
                    Expanded(
                      flex: 3,
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Text(
                            S.of(context).plastic_bag +
                                " (" +
                                _con.cart.plasticBagCount.toString() +
                                " X " +
                                _con.cart.plasticBagUnitPrice.toString() +
                                ")",
                            style: Theme.of(context).textTheme.body1,
                            overflow: TextOverflow.ellipsis,
                            maxLines: 1,
                          ),
                        ],
                      ),
                    ),
                    Expanded(
                      flex: 1,
                      child: Image.asset("assets/img/business.png",
                          height: 25, color: Colors.grey),
                    )
                  ],
                ),
                value: 1,
                onChanged: (val) {
                  state(() {
                    selectedBagType = val;
                  });

                  _con.cart.bagType = val;
                  print("BBBBBBBBBBBBBBBBBBBBBBBBBB" + val.toString());
                  _con.listenForBagTypeChange();
                },
              ),
              RadioListTile(
                groupValue: _con.cart.bagType,
                title: Row(
                  children: <Widget>[
                    Expanded(
                      flex: 3,
                      child: Text(
                        S.of(context).reusable_bag +
                            " (" +
                            _con.cart.reusableBagCount.toString() +
                            " X " +
                            _con.cart.reusableBagUnitPrice.toString() +
                            ")",
                        style: Theme.of(context).textTheme.body1,
                        overflow: TextOverflow.ellipsis,
                        maxLines: 1,
                      ),
                    ),
                    Expanded(
                      flex: 1,
                      child: InkWell(
                        child: Icon(
                          Icons.shopping_basket,
                          color: Colors.grey,
                          size: 30.0,
                        ),
                      ),
                    )
                  ],
                ),
                value: 2,
                onChanged: (val) {
                  state(() {
                    selectedBagType = val;
                  });

                  _con.cart.bagType = val;
                  print("AAAAAAAAAAAAAAAAAAAA" + val.toString());
                  _con.listenForBagTypeChange();
                },
              ),
            ],
          ),
        ),
      );
    });
  }

请问有人可以帮忙吗?

4

0 回答 0