我有一个流是Bookings的StreamBuilder,我有一个TableCalendar,当点击它时,将使用仅在该日期进行的新的预订过滤列表来更新Listview,问题是当用户广告新的预订时,流会更新,但由于我们依赖于TableCalendar中的按钮单击来更新Listforcew,当添加新的预订时,Listview不会自动更新,有什么方法可以解决这个问题吗?

class _AdminHomePageState extends ConsumerState<AdminHomePage> {
  CalendarFormat _calendarFormat = CalendarFormat.month;
  DateTime _focusedDay = DateTime.now();
  DateTime _selectedDay = DateTime.now();
  DBHandler dbhandler = DBHandler();
  List<Booking> allBookings = [];
  List<Booking> filteredBooking = [];

//now i neeed a selected events where it returns events for a certain day when clciked
  Map<DateTime, List<Booking>> events = {};

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

  List<Booking> filterBookingsByDate(
      DateTime selectedDate, List<Booking> unfiltered) {
    List<Booking> filteredBookings = [];

    for (var booking in unfiltered) {
      if (isSameDay(booking.bookingDate, selectedDate)) {
        filteredBookings.add(booking);
      }
    }

    return filteredBookings;
  }

  //need a method here to get bookings for a particular Date
  //I can interate thourhg the Bookings list trying to match the bookingDate to selectedDay

  @override
  Widget build(BuildContext context) {
    final bookingsAsyncValue = ref.watch(bookingsProvider);

    return Scaffold(
      appBar: AppBar(
        title: Text('Bookings'),
      ),
      body: bookingsAsyncValue.when(
        data: (bookings) {
          return Padding(
            padding: const EdgeInsets.all(8.0),
            child: Column(
              children: [
                Expanded(
                  flex: 1,
                  child: Container(
                    height: 300,
                    child: TableCalendar(
                      firstDay: DateTime.utc(2010, 10, 16),
                      lastDay: DateTime.utc(2030, 3, 14),
                      focusedDay: DateTime.now(),
                      calendarFormat: _calendarFormat,
                      calendarStyle:
                          const CalendarStyle(outsideDaysVisible: false),
                      onFormatChanged: (format) {
                        setState(() {
                          _calendarFormat = format;
                        });
                      },
                      selectedDayPredicate: (day) {
                        return isSameDay(_selectedDay, day);
                      },
                      onDaySelected: (selectedDay, focusedDay) {
                        setState(() {
                          _selectedDay = selectedDay;
                          _focusedDay =
                              focusedDay; // Update focused day as well
                          filteredBooking =
                              filterBookingsByDate(selectedDay, bookings);
                          //ChatGpt here you should show the bookings that have a startDate on the selected day

                          //use the Selected Day to get List of bookings below
                        });
                      },
                      onPageChanged: (focusedDay) {
                        _focusedDay = focusedDay;
                      },
                      eventLoader: (day) {
                        // Remove the time information from the day parameter
                        DateTime dateOnly =
                            DateTime(day.year, day.month, day.day);
                        return events[dateOnly] ?? [];
                      },
                    ),
                  ),
                ),
                Container(
                  height: 300,
                  child: ListView.builder(
                    itemCount: filteredBooking.length,
                    itemBuilder: (context, index) {
                      final booking = filteredBooking[index];
                      return ListTile(
                        title: Text(booking.serviceTitle),
                        subtitle: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text('Email: ${booking.userEmail}'),
                            Text(
                                'Date: ${DateFormat('yyyy-MM-dd').format(booking.bookingDate)}'),
                            Text(
                                'StartTime: ${DateFormat('HH:mm').format(booking.startTime)}'),
                          ],
                        ),
                        // Add more widgets to display other booking details
                      );
                    },
                  ),
                ),
              ],
            ),
          );
        },
        loading: () {
          return Center(
            child: CircularProgressIndicator(),
          );
        },
        error: (error, stackTrace) {
          return Center(
            child: Text('Error: $error'),
          );
        },
      ),
    );
  }
}

推荐答案

首先,我建议您不要在同一个小部件中使用setState和riverpod来管理状态.如果可能的话,try 提取窗口小部件.

您可以通过在Build()方法中创建filteredList来实现所需的功能.

移除

List<Booking> filteredBooking = [];

并将其添加到您的Build方法中.

@override
Widget build(BuildContext context) {

final bookingsAsyncValue = ref.watch(bookingsProvider);

final filteredBooking = filterBookingsByDate(_selectedDay, allBookings);

Flutter相关问答推荐

如何在应用栏中将列置于中心

壁炉有序在Flutter

来自FutureProvider的数据只打印两个不同的变量,它们对一个实例只有一次相同的值,为什么?

点击表情符号按钮后,Flutter 文本域文本编辑控制器将文本恢复为原始值

有没有什么方法可以加快在Flutter 中加载Quill HTML编辑器的速度?

如何在抖动中预加载一条路由,或者即使在屏幕外也保持加载小工具?

如何正确处理Flutter 翼中使用火基时发生的碰撞?

升级到Ffltter 3.16后,应用程序栏、背景 colored颜色 、按钮大小和间距都会发生变化

带有附加图像的ListTile

如何在Dart中允许正则表达式中有一个空格,但允许其他字符为1或更多?

构建上下文不能跨异步间隙使用

如何从flutter连接PostgreSQL数据库?

Flutter 使用扩展的 TextField 定位小部件

'type' Icon '不是类型' IconData '的子类型

Flutter使Perspective Page View无限循环

如何从 showModalBottomSheet 中的 topRight 和 topLeft 移除平边

如何在屏幕按钮中排列 row() (我想在按钮中放置屏幕导航图标)

如何将变量翻译成其他语言

http响应在Flutter 中返回307

Flutter Desktop,如何获取windows用户配置文件名称?