dart Flutter showModalBottomSheet关闭后保存数据

nnsrf1az  于 2023-06-19  发布在  Flutter
关注(0)|答案(1)|浏览(177)

我有showModalBottomSheet来显示应用中的过滤器,但是当我关闭showModalBottomSheet并重新打开它时,我的过滤器值 是否有任何选项来保持我的过滤器关闭showModalBottomSheet而不使用像SharedPref这样的包?我有showModalBottomSheet来显示应用中的过滤器,但是当我关闭showModalBottomSheet并重新打开它时,我的过滤器值 are reset是否有任何选项可以在不使用SharedPref等软件包的情况下将我的过滤器保持在close showModalBottomSheet上?

showFilter(
        {required BuildContext context,
        required FilterModel filterData,
        Function(String)? onSubmitted
        }) =>
    showModalBottomSheet(
      context: context,
      backgroundColor: Colors.white,
      isScrollControlled: true,
      builder: (context) {
        final shipmentItems = filterData.shipmentType!.childData
            .map((e) => DropdownMenuItem(
                  value: e.value,
                  child: Text(e.text),
                ))
            .toList();
        var shipmentInitialVal = filterData.shipmentType!.initialValue;
        ////////////
        final saleChannelItems = filterData.saleChannel!.childData
            .map((e) => DropdownMenuItem(
                  value: e.value,
                  child: Text(e.text),
                ))
            .toList();
        var saleChannelInitialVal = filterData.saleChannel!.initialValue;
        ////////////
        final shipmentPointItems = filterData.shipmentPoint!.childData
            .map((e) => DropdownMenuItem(
                  value: e.value,
                  child: Text(e.text),
                ))
            .toList();
        var shipmentPointInitialVal = filterData.shipmentPoint!.initialValue;
        //////////////
        return StatefulBuilder(builder: (context, innerSetState) {
          return Container(
            padding: REdgeInsets.symmetric(horizontal: 8, vertical: 12),
            height: MediaQuery.of(context).size.height * 0.8,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  children: [
                    Text(
                      'Фильтр',
                      style: TextStyles.bodyStyle,
                    ),
                    const Spacer(),
                    IconButton(
                        onPressed: () {
                          Navigator.pop(context);
                        },
                        icon: const Icon(Icons.close)),
                  ],
                ),
                SizedBox(height: 10.h),
                Text(
                  filterData.saleChannel!.name.toUpperCase(),
                  style: TextStyles.editStyle,
                ),
                SizedBox(height: 10.h),
                if (filterData.saleChannel!.type == 'select')
                  DropdownButtonHideUnderline(
                      child: DropdownButton2(
                    isExpanded: true,
                    buttonStyleData: ButtonStyleData(
                        height: 50,
                        decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(8),
                            color: Theme.of(context).scaffoldBackgroundColor),
                        padding: REdgeInsets.all(8)),
                    value: saleChannelInitialVal,
                    items: saleChannelItems,
                    hint: const Text('Выберите статус'),
                    onChanged: (value) {
                      innerSetState(() {
                        saleChannelInitialVal = int.parse(value.toString());
                      });
                    },
                  )),
                SizedBox(height: 10.h),
                Text(
                  filterData.shipmentType!.name.toUpperCase(),
                  style: TextStyles.editStyle,
                ),
                SizedBox(height: 10.h),
                if (filterData.shipmentType!.type == 'select')
                  DropdownButtonHideUnderline(
                      child: DropdownButton2(
                    isExpanded: true,
                    buttonStyleData: ButtonStyleData(
                        height: 50,
                        decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(8),
                            color: Theme.of(context).scaffoldBackgroundColor),
                        padding: REdgeInsets.all(8)),
                    value: shipmentInitialVal,
                    items: shipmentItems,
                    hint: const Text('Выберите статус'),
                    onChanged: (value) {
                      innerSetState(() {
                        shipmentInitialVal = int.parse(value.toString());
                      });
                    },
                  )),
                SizedBox(height: 10.h),
                Text(
                  filterData.shipmentPoint!.name.toUpperCase(),
                  style: TextStyles.editStyle,
                ),
                SizedBox(height: 10.h),
                if (filterData.shipmentPoint!.type == 'select')
                  DropdownButtonHideUnderline(
                      child: DropdownButton2(
                    isExpanded: true,
                    buttonStyleData: ButtonStyleData(
                        height: 50,
                        decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(8),
                            color: Theme.of(context).scaffoldBackgroundColor),
                        padding: REdgeInsets.all(8)),
                    value: shipmentPointInitialVal,
                    items: shipmentPointItems,
                    hint: const Text('Выберите статус'),
                    onChanged: (value) {
                      innerSetState(() {
                        shipmentPointInitialVal = int.parse(value.toString());
                      });
                    },
                  )),
                const Spacer(),
                ElevatedButton(
                    onPressed: () {
                      innerSetState(() {
                        saleChannelInitialVal =
                            filterData.saleChannel!.initialValue;
                        shipmentInitialVal =
                            filterData.shipmentType!.initialValue;
                        shipmentPointInitialVal =
                            filterData.shipmentPoint!.initialValue;
                      });
                    },
                    style: ElevatedButton.styleFrom(
                        backgroundColor: Colors.red,
                        minimumSize: const Size(double.infinity, 40)),
                    child: const Text('Сбросить')),
                ElevatedButton(
                    onPressed: () {
                      final filter = {
                        filterData.saleChannel!.value: saleChannelInitialVal,
                        filterData.shipmentType!.value: shipmentInitialVal,
                        filterData.shipmentPoint!.value:
                            shipmentPointInitialVal,
                      };
                      final apiFilter = filter.entries
                          .map((e) => '${e.key}=${e.value}')
                          .join('&');
                      onSubmitted!(apiFilter);
                      Navigator.pop(context);
                    },
                    style: ElevatedButton.styleFrom(
                        minimumSize: const Size(double.infinity, 40)),
                    child: const Text('Применить')),
                SizedBox(height: 10.h),
              ],
            ),
          );
        });
      },
    );
sr4lhrrt

sr4lhrrt1#

如果你想在关闭showModalBottomshet时保留选择选项,你可以使用一个回调函数。示例

showFilter(
        {required BuildContext context,
        required FilterModel filterData,
        Function(String)? onSubmitted,
        Function(Filter filter) onSelected,
        Filter initialFilter
        })

你可以在你使用view的地方给onSelected参数。当选择一个过滤器选项时,您可以在视图中看到。您可以指定一个变量,当再次单击showModalBottomSheet时,您可以使用initailFilter发送选定的过滤器。并且必须在bottomSheet小部件中进行控制。

相关问题