我有一个流是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'),
);
},
),
);
}
}