我们可以在我们的蜂巢箱内分组或过滤数据吗

Can we group or filter data inside our hive boxes

我使用配置单元将我的数据本地存储在费用跟踪器应用程序中。我已将所有费用保存在一个名为 expenseBox 的盒子中。我已经为每个费用项目指定了一个日期,我希望将具有相同日期的项目组合在一起并单独显示。有没有办法做到这一点? 谢谢你的帮助!

顺便说一句,这是我显示项目的主屏幕代码。

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:flutter_locales/flutter_locales.dart';

import '/models/expense.dart';
import '/widgets/expense_list_tile.dart';
import '/screens/add_expense_screen.dart';

class HomeScreen extends StatefulWidget {
  const HomeScreen({Key? key}) : super(key: key);

  static const routeName = '/home_page';

  @override
  _HomeScreenState createState() => _HomeScreenState();
}

enum SlidableAction {
  edit,
  delete,
}

class _HomeScreenState extends State<HomeScreen> {
  late Box expenseBox;
  late Box pocketBox;

  bool isFabVisible = true;

  @override
  void initState() {
    expenseBox = Hive.box<Expense>('expenses');
    pocketBox = Hive.box<int>('pocket');
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return ValueListenableBuilder(
      valueListenable: Hive.box<int>('pocket').listenable(),
      builder: (context, Box<int> pocketBox, child) => Scaffold(
        appBar: AppBar(
          title: const LocaleText('appName'),
          elevation: 0,
          centerTitle: true,
        ),
        body: NestedScrollView(
          headerSliverBuilder: (context, _) => [
            SliverAppBar(
              expandedHeight: MediaQuery.of(context).size.height * 0.15,
              flexibleSpace: FlexibleSpaceBar(
                background: Container(
                  padding: const EdgeInsets.symmetric(horizontal: 10),
                  child: Row(
                    children: [
                      Expanded(
                        child: Column(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                const LocaleText(
                                  'appName',
                                  style: TextStyle(
                                      fontSize: 14, color: Colors.white),
                                ),
                                Text(
                                  '${pocketBox.get('budget') ?? 0}',
                                  style: const TextStyle(
                                      fontSize: 18,
                                      color: Colors.white,
                                      fontWeight: FontWeight.bold),
                                ),
                              ],
                            ),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                const LocaleText(
                                  'income',
                                  style: TextStyle(
                                      fontSize: 14, color: Colors.white),
                                ),
                                Text(
                                  '${pocketBox.get('totalIncome') ?? 0}',
                                  style: const TextStyle(
                                      fontSize: 18,
                                      color: Colors.white,
                                      fontWeight: FontWeight.bold),
                                ),
                              ],
                            ),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                const LocaleText(
                                  'expense',
                                  style: TextStyle(
                                      fontSize: 14, color: Colors.white),
                                ),
                                Text(
                                  '${pocketBox.get('totalExpense') ?? 0}',
                                  style: const TextStyle(
                                      fontSize: 18,
                                      color: Colors.white,
                                      fontWeight: FontWeight.bold),
                                ),
                              ],
                            ),
                          ],
                        ),
                      ),
                      Expanded(child: Container()),
                    ],
                  ),
                ),
              ),
            ),
          ],
          body: ValueListenableBuilder(
            valueListenable: Hive.box<Expense>('expenses').listenable(),
            builder: (context, Box<Expense> expensesBox, child) {
              return NotificationListener<UserScrollNotification>(
                onNotification: (notification) {
                  if (notification.direction == ScrollDirection.forward) {
                    if (!isFabVisible) setState(() => isFabVisible = true);
                  } else if (notification.direction ==
                      ScrollDirection.reverse) {
                    if (isFabVisible) setState(() => isFabVisible = false);
                  }
                  return true;
                },
                child: Scrollbar(
                  thickness: 5,
                  interactive: true,
                  child: ListView.separated(
                    separatorBuilder: (context, index) {
                      return const SizedBox();
                    },
                    itemCount: expenseBox.length,
                    itemBuilder: (context, index) {
                      final expense = expenseBox.getAt(index);
                      return ExpenseListTile(index: index, expense: expense);
                    },
                  ),
                ),
              );
            },
          ),
        ),
        floatingActionButton: isFabVisible
            ? FloatingActionButton(
                onPressed: () {
                  Navigator.of(context).push(MaterialPageRoute(
                    builder: (_) => const AddExpenseScreen(
                      index: -1,
                    ),
                  ));
                },
                child: const Icon(Icons.add),
              )
            : null,
      ),
    );
  }
}

你可以试试这个代码。

import 'package:flutter/material.dart';
import 'package:sticky_grouped_list/sticky_grouped_list.dart';

class MyMissionList extends StatefulWidget {
  @override
  State<MyMissionList> createState() => _MyMissionListState();
}

class _MyMissionListState extends State<MyMissionList> {
  @override
  Widget build(BuildContext context) {
    return  Scaffold(
      appBar: PreferredSize(
          preferredSize: Size.fromHeight(150.0),
          child: Container(
            decoration: BoxDecoration(
              color: AppColors.baseLightBlueColor,
              // AppColors.blue,
              borderRadius: BorderRadius.only(bottomRight: Radius.circular(30)),
            ),
            child: Material(
              color: AppColors.baseLightBlueColor,
              elevation: 15,
              shape: RoundedRectangleBorder(
                borderRadius:
                BorderRadius.only(bottomRight: Radius.circular(30)),
              ),
              child: Column(
                children: [
                  AppBar(
                    backgroundColor: AppColors.baseLightBlueColor,
                    elevation: 0.0,
                    actions: [
                      Container(
                          padding: EdgeInsets.only(right: 20),
                          child: Image.asset(
                            "assets/Vector-2.png",
                            height: 5,
                            width: 22,
                            color: Colors.white,
                          )),
                    ],
                  ),
                  Container(
                    width: MediaQuery.of(context).size.width,
                    padding: EdgeInsets.only(left: 20),
                    margin: EdgeInsets.only(bottom: 10),
                    decoration: BoxDecoration(
                      color: AppColors.baseLightBlueColor,
                    ),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          "Hello User123,",
                          style:
                          TextStyle(color: AppColors.white, fontSize: 15),
                        ),
                        SizedBox(height: 10),
                        Text(
                          AppStrings.totalAmount,
                          // "Montant total :",
                          style:
                          TextStyle(color: AppColors.white, fontSize: 11),
                        ),
                        Text(
                          "592,30 €",
                          style:
                          TextStyle(color: AppColors.white, fontSize: 25),
                        ),
                      ],
                    ),
                  )
                ],
              ),
            ),
          )),
      body: Column(
        children: [
          Padding(
            padding: const EdgeInsets.only(left:15.0,right: 15,top: 15),
            child: Text("My Mission",style: TextStyle(
                color: Color(0xff243656),
                fontSize: 15,
                fontWeight: FontWeight.w300),
            ),
          ),

          Expanded(
            child: StickyGroupedListView<Element, DateTime>(
              elements: elements,
              order: StickyGroupedListOrder.ASC,
              groupBy: (Element element) =>
                  DateTime(element.date.year, element.date.month, element.date.day),
              groupComparator: (DateTime value1, DateTime value2) =>
                  value2.compareTo(value1),
              itemComparator: (Element element1, Element element2) =>
                  element1.date.compareTo(element2.date),
              floatingHeader: false,
              groupSeparatorBuilder: (Element element) => Container(
                height: 50,

                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    Container(
                      width: 220,
                      padding: EdgeInsets.only(left: 20,top: 20,bottom: 10),
                      child: Text(
                        '${element.heading}',
                        textAlign: TextAlign.start,
                        style: TextStyle(color: Color(0xff919AAA)),
                      ),
                    ),
                  ],
                ),
              ),
              itemBuilder: (_, Element element) {
                return element.type
                    ? GestureDetector(
                  onTap: () {
                    // Navigator.push(context, MaterialPageRoute(builder: (context)=> WaitingForValidation()));
                  },
                  child: Card(
                    elevation: 4,
                    margin: EdgeInsets.only(bottom: 15, right: 15, left: 15),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(15.0),
                    ),
                    child: Container(
                      height: 70,
                      padding: EdgeInsets.all(10),
                      decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(15),
                          color: AppColors.white),
                      child: Row(
                        children: [
                          Container(
                            width: MediaQuery.of(context).size.width * 0.62,
                            height: 50,
                            padding: EdgeInsets.only(top: 10,left: 10),
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.start,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text(
                                  element.subTitle +
                                      '${element.date.day}/${element.date.month}/'
                                          '${element.date.year}',
                                  style: TextStyle(
                                    //color:AppColors.grey
                                      color: Color(0xff919AAA),
                                      fontSize: 12,
                                      fontWeight: FontWeight.w300),
                                ),
                                SizedBox(height: 2,),
                                Text(
                                  element.hotelName,
                                  style: TextStyle(
                                    //color:AppColors.grey
                                      color: Color(0xff243656),
                                      fontSize: 15,
                                      fontWeight: FontWeight.w300),
                                ),

                              ],
                            ),
                          ),
                          Padding(
                            padding: const EdgeInsets.all(5.0),
                            child: Card(
                              elevation: 15.0,
                              shadowColor: Colors.blue[100],
                              shape: const RoundedRectangleBorder(
                                  borderRadius: BorderRadius.all(
                                      Radius.circular(20.0))),
                              child: Container(
                                height: 30,
                                width: 80,
                                decoration: BoxDecoration(
                                  // boxShadow: [BoxShadow(color: AppColors.grey, blurRadius: 20.0)],
                                    borderRadius:
                                    BorderRadius.circular(20.0),
                                    gradient: LinearGradient(
                                      colors: [
                                        Colors.blue,
                                        Colors.blue.shade900,
                                      ],
                                    )),
                                child: MaterialButton(
                                  onPressed: () {},
                                  child: Text(
                                    element.buttonText,
                                    style: TextStyle(
                                        color: Colors.white,
                                        fontSize: 14,
                                        fontWeight: FontWeight.bold),
                                  ),
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                )
                    : Card(
                  elevation: 4,
                  margin: EdgeInsets.only(bottom: 15, right: 15, left: 15),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(15.0),
                  ),
                  child: Container(
                    height: 70,
                    padding: EdgeInsets.all(10),
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(15),
                        color: AppColors.white),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: [
                        Container(
                          width: MediaQuery.of(context).size.width * 0.62,
                          height: 50,
                          padding: EdgeInsets.only(top: 10,left: 10),
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.start,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                element.subTitle +
                                    '${element.date.day}/${element.date.month}/'
                                        '${element.date.year}',
                                style: TextStyle(
                                  //color:AppColors.grey
                                    color: Color(0xff919AAA),
                                    fontSize: 12,
                                    fontWeight: FontWeight.w300),
                              ),
                              SizedBox(height: 2,),
                              Text(
                                element.hotelName,
                                style: TextStyle(
                                  //color:AppColors.grey
                                    color: Color(0xff243656),
                                    fontSize: 15,
                                    fontWeight: FontWeight.w300),
                              ),

                            ],
                          ),
                        ),
                        Padding(
                          padding: const EdgeInsets.only(top:5.0,bottom: 5),
                          child: Container(
                            height: 30,
                            width: 95,
                            child: MaterialButton(
                              onPressed: () {},
                              child: Text(
                                element.buttonText,
                                style: TextStyle(
                                    color: AppColors.green,
                                    fontSize: 14,
                                    fontWeight: FontWeight.bold),
                              ),
                            ),
                          ),
                        )
                      ],
                    ),
                  ),

                );
              },
            ),
          ),
        ],
      ),
    );
  }
}

class Element {
  bool type;
  DateTime date;
  String hotelName;
  String subTitle;
  String heading;
  String buttonText;

  Element(this.date, this.type, this.hotelName, this.subTitle, this.heading,
      this.buttonText);
}
List<Element> elements = <Element>[
  Element(
    DateTime(2020, 7, 22),
    false,
    "Rendez-vous equipe prod",
    "Jeudi",
    "Terminate",
    "Terminate",
  ),
  Element(
    DateTime(2021, 10, 15),
    false,
    "Rendez-vous equipe prod",
    "Jeudi",
    "Terminate",
    "Terminate",
  ),
  Element(
    DateTime(2021, 10, 15),
    false,
    "Rendez-vous equipe prod",
    "Jeudi",
    "Terminate",
    /**/
    "Terminate",
  ),
  Element(
    DateTime(2021, 12, 12),
    true,
    "Visite entrepot Villabe ",
    "Mercredi",
    "To Plan",
    "To Plan",
  ),


];