我正在从另一个底表打开一个modalbootmsheet,问题是这个小部件已被卸载,所以状态不再有上下文

I am opening a modalbootmsheet from another bottomsheet, the problem is This widget has been unmounted, so the State no longer has a context

提问人:HASSAN ZAHEER 提问时间:11/14/2023 更新时间:11/14/2023 访问量:10

问:

我有一个 showthirdlevelmenusheet 和 countermenusheet,在 showthirdlevelmenusheet 中,当我选择一个单选按钮然后按下下一个按钮时,countermenusheet 显示错误:此小部件已卸载,因此状态不再具有上下文(应被视为已失效)。 考虑在“处置”期间取消任何正在进行的工作或使用 “mounted” getter 来确定状态是否仍处于活动状态。

但是,如果我在不检查任何单选按钮的情况下按下一步,则 countermenusheet 会出现而不会出错。
_showThirdLevelMenuSheet的代码是:

void _showThirdLevelMenuSheet() {
    List recipeTypeIdList = widget.submenu.level3menu!.split(',');
    Get.bottomSheet(
        isScrollControlled: true,
        GetBuilder(
          init: HomeController(),
          builder: (controller) {
            return Container(
              height: MediaQuery.of(context).size.height * 0.85,
              color: const Color(0xff757575),
              child: Container(
                decoration: const BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(20),
                    topRight: Radius.circular(20),
                  ),
                ),
                padding: const EdgeInsets.all(16),
                child: SingleChildScrollView(
                  child: Column(
                    // shrinkWrap: true,
                    // physics: NeverScrollableScrollPhysics(),
                    children: [
                      const Text(
                        'Add Ingredients to this item',
                        style: TextStyle(
                          color: Colors.pink,
                          fontWeight: FontWeight.bold,
                          fontSize: 20,
                        ),
                      ),
              
                      const SizedBox(
                        height: 10,
                      ),
                      Padding(
                        padding: const EdgeInsets.only(left: 15),
                        child: Align(
                          alignment: Alignment.topLeft,
                          child: RichText(
                            text: TextSpan(
                              style: DefaultTextStyle.of(context).style,
                              children: <TextSpan>[
                                TextSpan(
                                  text: 'Select ',
                                  style: TextStyle(
                                      fontSize: 15,
                                      fontWeight: FontWeight.bold,
                                      color: Colors.black.withOpacity(0.8)),
                                ),
                                TextSpan(
                                  text: widget.itemName,
                                  style: TextStyle(
                                    fontWeight: FontWeight.bold,
                                    color: Colors.black.withOpacity(0.8),
                                    fontSize: 15,
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ),
                      ),
                      const SizedBox(height: 5),
                      ListView.builder(
                          shrinkWrap: true,
                          physics: const NeverScrollableScrollPhysics(),
                          itemCount: recipeTypeIdList.length,
                          itemBuilder: (context, index) {
                            var recipeType = controller.recipeTypeList
                                .firstWhere((element) =>
                                    recipeTypeIdList.contains(element.id));
                            var createRecipeDetailList = controller
                                .recipeDetailList
                                .where((element) =>
                                    element.recipeTypeId == recipeType.id)
                                .toList();
                            return Column(
                              children: [
                                Row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceBetween,
                                  children: [
                                    Padding(
                                      padding: const EdgeInsets.only(left: 15),
                                      child: Align(
                                        alignment: Alignment.topLeft,
                                        child: SizedBox(
                                          width: Get.size.width / 1.2,
                                          child: Row(
                                            children: [
                                              Flexible(
                                                child: Text(
                                                  recipeType.name!,
                                                  style: const TextStyle(
                                                      fontSize: 20,
                                                      color:
                                                          Colors.orangeAccent),
                                                ),
                                              ),
                                            ],
                                          ),
                                        ),
                                      ),
                                    ),
                                   
                                  ],
                                ),
                                const SizedBox(
                                  height: 5,
                                ),
                                const Divider(
                                  height: 1,
                                ),
                                const SizedBox(
                                  height: 5,
                                ),
                                ListView.builder(
                                    itemCount: createRecipeDetailList.length,
                                    physics:
                                        const NeverScrollableScrollPhysics(),
                                    shrinkWrap: true,
                                    itemBuilder: (context, index) {
                                      var recipe =
                                          createRecipeDetailList[index];
                                      return ReusableCheckboxRow(
                                        text: recipe.name!,
                                        selectedValue:
                                            controller.selectedOption,
                                        onChanged: controller.updateSelected,
                                      );
                                    }),
                              ],
                            );
                          }),
                      Row(
                        children: [
                          Expanded(
                            flex: 2,
                            child: Container(
                              padding: const EdgeInsets.symmetric(vertical: 50),
                              child: ElevatedButton(
                                onPressed: () {
                                  Get.back();
                                  controller.updateSelected(null);
                                  controller.serves = 1.obs;
                                },
                                style: ElevatedButton.styleFrom(
                                  backgroundColor: AppColor.secondary,
                                  shape: RoundedRectangleBorder(
                                    borderRadius: BorderRadius.circular(10.0),
                                  ),
                                ),
                                child: const Text(
                                  'Cancel',
                                  style: TextStyle(
                                    color: Colors.white,
                                    fontSize: 14,
                                    fontWeight: FontWeight.bold,
                                  ),
                                ),
                              ),
                            ),
                          ),
                          const SizedBox(
                            width: 20,
                          ),
                          Expanded(
                            flex: 2,
                            child: Container(
                              padding: const EdgeInsets.symmetric(vertical: 50),
                              child: ElevatedButton(
                                onPressed: () {
                                  // controller.addToCart(widget.submenu);
                                  // _countMenuSheet();

                                  Get.back(result: true);
                                  Get.bottomSheet(_countMenuSheet());

                                  // controller.updateSelected(null);
                                  // controller.serves = 1.obs;
                                },
                                style: ElevatedButton.styleFrom(
                                  backgroundColor: AppColor.secondary,
                                  shape: RoundedRectangleBorder(
                                    borderRadius: BorderRadius.circular(10.0),
                                  ),
                                ),
                                child: const Text(
                                  'Next',
                                  style: TextStyle(
                                    color: Colors.white,
                                    fontSize: 14,
                                    fontWeight: FontWeight.bold,
                                  ),
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ],
                  ),
                ),
              ),
            );
          },
        ));
  }  

_countMenuSheet的代码是:

 Widget _countMenuSheet() {
    return GetBuilder(
      init: HomeController(),
      builder: (controller) {
        // var recipeType = controller.recipeTypeList.first;
        return Container(
          height: MediaQuery.of(context).size.height / 1.7,
          color: const Color(0xff757575),
          child: Container(
            decoration: const BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.only(
                topLeft: Radius.circular(20),
                topRight: Radius.circular(20),
              ),
            ),
            padding: const EdgeInsets.all(16),
            child: SingleChildScrollView(
              child: Column(
                // shrinkWrap: true,
                // physics: NeverScrollableScrollPhysics(),
                children: [
                  const Text(
                    'Choose Item Quantity',
                    style: TextStyle(
                      color: Colors.pink,
                      fontWeight: FontWeight.bold,
                      fontSize: 20,
                    ),
                  ),
                  const SizedBox(
                    height: 10,
                  ),
                  Padding(
                    padding: const EdgeInsets.only(left: 0),
                    child: Align(
                      alignment: Alignment.topLeft,
                      child: RichText(
                        text: TextSpan(
                          style: DefaultTextStyle.of(context).style,
                          children: <TextSpan>[
                            TextSpan(
                              text: 'Select ',
                              style: TextStyle(
                                  fontSize: 15,
                                  fontWeight: FontWeight.bold,
                                  color: Colors.black.withOpacity(0.8)),
                            ),
                            const TextSpan(
                              text:
                                  ' ', // Add a space here to create space between text spans
                            ),
                            TextSpan(
                              text: widget.itemName,
                              style: const TextStyle(
                                fontWeight: FontWeight.bold,
                                color: Colors.orangeAccent,
                                fontSize: 15,
                              ),
                            ),
                            const TextSpan(
                              text:
                                  ' ', // Add a space here to create space between text spans
                            ),
                            TextSpan(
                              text: 'Quantity',
                              style: TextStyle(
                                fontWeight: FontWeight.bold,
                                color: Colors.black.withOpacity(0.8),
                                fontSize: 15,
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),
                  const SizedBox(height: 5),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        widget.itemName,
                        style: const TextStyle(color: Colors.black),
                      ),
                      Row(
                        children: [
                          SizedBox(
                            height: 28,
                            width: 28,
                            child: FloatingActionButton(
                              backgroundColor: Colors.orange,
                              onPressed: () {
                                controller.setServesValues(false);
                              },
                              child: const Icon(
                                Icons.remove,
                                color: Colors.white,
                                size: 13,
                              ),
                            ),
                          ),
                          const SizedBox(
                            width: 8,
                          ),
                          Obx(() {
                            return Text(
                              controller.serves.toString(),
                              style: const TextStyle(fontSize: 16),
                            );
                          }),
                          const SizedBox(
                            width: 8,
                          ),
                          SizedBox(
                            height: 28,
                            width: 28,
                            child: FloatingActionButton(
                              // backgroundColor: Colors.pink,
                              backgroundColor: AppColor.myGreen,
                              onPressed: () {
                                controller.setServesValues(true);
                              },
                              child: const Icon(
                                Icons.add,
                                color: Colors.white,
                                size: 13,
                              ),
                            ),
                          ),
                        ],
                      ),
                    ],
                  ),
                  
                  Row(
                    children: [
                      Expanded(
                        flex: 2,
                        child: Container(
                          padding: const EdgeInsets.symmetric(vertical: 50),
                          child: ElevatedButton(
                            onPressed: () {
                              Get.back();
                              controller.updateSelected(null);
                              controller.serves = 1.obs;
                            },
                            style: ElevatedButton.styleFrom(
                              backgroundColor: AppColor.secondary,
                              shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(10.0),
                              ),
                            ),
                            child: const Text(
                              'Cancel',
                              style: TextStyle(
                                color: Colors.white,
                                fontSize: 14,
                                fontWeight: FontWeight.bold,
                              ),
                            ),
                          ),
                        ),
                      ),
                      const SizedBox(
                        width: 20,
                      ),
                      Expanded(
                        flex: 2,
                        child: Container(
                          padding: const EdgeInsets.symmetric(vertical: 50),
                          child: ElevatedButton(
                            onPressed: () {
                              controller.addToCart(widget.submenu);
                              Get.back();
                              controller.updateSelected(null);
                              controller.serves = 1.obs;
                            },
                            style: ElevatedButton.styleFrom(
                              backgroundColor: AppColor.secondary,
                              shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(10.0),
                              ),
                            ),
                            child: const Text(
                              'Add',
                              style: TextStyle(
                                color: Colors.white,
                                fontSize: 14,
                                fontWeight: FontWeight.bold,
                              ),
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ),
        );
      },
    );
  }  
Flutter Dart 移动应用 状态管理

评论


答: 暂无答案