Flutter Hive 持久化数据

问题描述 投票:0回答:1

enter image description here

我无法在此代码中重置旧过滤器。我正在使用 Hive 和 Flutter。即使我不把它们放在盒子里,过滤器也保留在盒子里

我试过

setState
box.compact
Navigator.pop
reopen
,但没有一个有效。 清理缓存并重新启动它。我的真实设备和模拟器的 iPhone。

我的代码:

class ClientsFilterBottomSheet extends StatefulWidget {
  final TransactionsBloc transactionsBloc;

  const ClientsFilterBottomSheet({Key? key, required this.transactionsBloc}) : super(key: key);

  @override
  State<ClientsFilterBottomSheet> createState() => _ClientsFilterBottomSheetState();
}

class _ClientsFilterBottomSheetState extends State<ClientsFilterBottomSheet> {
  List<dynamic> servers = [];

  @override
  void initState() {
    super.initState();
    servers = box.get('servers', defaultValue: []);
  }

  void _resetFilter() async {
    setState(() {
      servers = box.get('servers', defaultValue: []);
    });
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      physics: const NeverScrollableScrollPhysics(),
      child: SizedBox(
        height: MediaQuery.of(context).size.height * 0.7,
        child: Column(
          children: [
            Container(
              height: 4,
              width: 48,
              margin: const EdgeInsets.only(top: 10),
              decoration: BoxDecoration(
                color: const Color(0xffE0E0E0),
                borderRadius: BorderRadius.circular(10),
              ),
            ),
            const SizedBox(
              height: 10,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                const SizedBox(
                  width: 48,
                ),
                Container(margin: const EdgeInsets.only(top: 13, bottom: 13), child: Text('filter_shops'.tr()).roboto(fontSize: 18, fontWeight: FontWeight.w500)),
                Container(
                  margin: const EdgeInsets.only(right: 24),
                  child: animatedDownloadButton(onUserPressed: () async {
                    await Future.delayed(const Duration(seconds: 5));
                  }),
                )
              ],
            ),
            const SizedBox(
              height: 10,
            ),
            const Divider(
              color: UIColors.greyColor2,
              thickness: 0.1,
              height: 0,
            ),
            SizedBox(
              height: MediaQuery.of(context).size.height * 0.48,
              child: SingleChildScrollView(
                child: ListView.builder(
                  shrinkWrap: true,
                  itemCount: servers.length,
                  physics: const NeverScrollableScrollPhysics(),
                  itemBuilder: (context, index) {
                    List<dynamic> clients = servers[index].clients;
                    return clients.isEmpty
                        ? const SizedBox()
                        : Column(
                            children: [
                              ListTileTheme(
                                dense: true,
                                horizontalTitleGap: 0.0,
                                minLeadingWidth: 0,
                                child: ExpansionTile(
                                  controlAffinity: ListTileControlAffinity.leading,
                                  initiallyExpanded: true,
                                  title: CheckboxListTile(
                                    value: servers[index].isSelected,
                                    tristate: false,
                                    onChanged: (value) {
                                      servers[index].isSelected = value!;
                                      servers[index].clients.forEach((element) {
                                        element.isSelected = value;
                                      });
                                      setState(() {});
                                    },
                                    title: GestureDetector(
                                      onTap: () {},
                                      child: Text(servers[index].name ?? '').capitalize().roboto(fontSize: 16, fontWeight: FontWeight.w500, color: Colors.black87),
                                    ),
                                  ),
                                  children: [
                                    ListView.builder(
                                      shrinkWrap: true,
                                      physics: const NeverScrollableScrollPhysics(),
                                      itemCount: clients.length,
                                      itemBuilder: (context, index2) {
                                        return Padding(
                                          padding: const EdgeInsets.only(right: 14.0, left: 30.0, top: 0, bottom: 0),
                                          child: ListTileTheme(
                                            dense: true,
                                            child: CheckboxListTile(
                                              value: servers[index].isSelected ? (clients[index2].isSelected) : false,
                                              checkColor: UIColors.mainBackgroundColor,
                                              onChanged: (value) {
                                                clients[index2].isSelected = value!;
                                                if (clients.any((element) => element.isSelected)) {
                                                  servers[index].isSelected = true;
                                                } else if (clients.every((element) => !element.isSelected)) {
                                                  servers[index].isSelected = false;
                                                }
                                                setState(() {});
                                              },
                                              title: Padding(
                                                padding: const EdgeInsets.only(left: 8.0),
                                                child: Text(clients[index2].name ?? '').capitalize().roboto(fontSize: 14, fontWeight: FontWeight.w400),
                                              ),
                                            ),
                                          ),
                                        );
                                      },
                                    ),
                                  ],
                                ),
                              ),
                            ],
                          );
                  },
                ),
              ),
            ),
            Expanded(
              child: Container(
                padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 10),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(10),
                  color: Colors.white,
                  boxShadow: [
                    BoxShadow(
                      color: Colors.grey.withOpacity(0.2),
                      spreadRadius: 1,
                      blurRadius: 1,
                      offset: const Offset(0, 1), // changes position of shadow
                    ),
                  ],
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Expanded(
                      child: ElevatedButton(
                        onPressed: _resetFilter,
                        style: ElevatedButton.styleFrom(
                          elevation: 0,
                          backgroundColor: const Color(0xffF0F2F6),
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(10),
                          ),
                          padding: const EdgeInsets.symmetric(vertical: 20),
                        ),
                        child: Text('reset'.tr().toUpperCase()).roboto(fontSize: 16, fontWeight: FontWeight.w500, color: const Color(0xff009C35)),
                      ),
                    ),
                    const SizedBox(
                      width: 8,
                    ),
                    Expanded(
                      child: ElevatedButton(
                        onPressed: () async {
                          //    await box.put('servers', servers);
                          if (mounted) Navigator.pop(context);
                        },
                        style: ElevatedButton.styleFrom(
                          elevation: 0,
                          backgroundColor: const Color(0xff009C35),
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(10),
                          ),
                          padding: const EdgeInsets.symmetric(vertical: 20),
                        ),
                        child: Text('apply'.tr().toUpperCase()).roboto(fontSize: 16, fontWeight: FontWeight.w500, color: Colors.white),
                      ),
                    ),

                    //reset button
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

flutter hive
1个回答
0
投票

我为要编辑或重置而不应用过滤器的框数据创建了新模型。并将数据从新模型保存到盒子中。那奏效了。但是我必须创建一个新模型和两种方法来做到这一点。

© www.soinside.com 2019 - 2024. All rights reserved.