Я пытаюсь отобразить список задач на основе текущей выбранной даты. Я использую шаблон flutter blo c и построил blo c для своего календаря (который зависит от предка blo c - 'FilteredTasksBlo c'), но я не смог получить задачи на каждый день. В нем постоянно отображаются задачи на сегодня. Как я могу это исправить?
Вот мой код:
Состояние календаря:
abstract class CalendarState extends Equatable {
const CalendarState();
@override
List<Object> get props => [];
}
class CalendarLoading extends CalendarState {}
class CalendarLoaded extends CalendarState {
final List<Task> tasks;
final DateTime selectedDate;
CalendarLoaded(this.tasks, this.selectedDate);
@override
List<Object> get props => [tasks, selectedDate];
@override
String toString() {
return 'CalendarLoaded { tasks: $tasks, selectedDate: $selectedDate }';
}
}
class CalendarNotLoaded extends CalendarState {}
Событие календаря:
abstract class CalendarEvent extends Equatable {
const CalendarEvent();
}
class UpdateCalendar extends CalendarEvent {
const UpdateCalendar(this.selectedDate);
final DateTime selectedDate;
@override
List<Object> get props => [selectedDate];
@override
String toString() => 'UpdateCalendar { selectedDate: $selectedDate }';
}
class UpdateTasks extends CalendarEvent {
const UpdateTasks(this.tasks);
final List<Task> tasks;
@override
List<Object> get props => [tasks];
@override
String toString() => 'UpdateTasks { tasks: $tasks }';
}
Calendar Blo c:
class CalendarBloc extends Bloc<CalendarEvent, CalendarState> {
final FilteredTasksBloc filteredTasksBloc;
StreamSubscription filteredTasksSubscription;
CalendarBloc({@required this.filteredTasksBloc}) {
filteredTasksSubscription = filteredTasksBloc.listen((state) {
if (state is FilteredTasksLoaded) {
add(UpdateTasks(
(filteredTasksBloc.state as FilteredTasksLoaded).filteredTasks,
));
}
});
}
@override
CalendarState get initialState {
return filteredTasksBloc.state is FilteredTasksLoaded
? CalendarLoaded(
(filteredTasksBloc.state as FilteredTasksLoaded).filteredTasks, sss)
: CalendarLoading();
}
@override
Stream<CalendarState> mapEventToState(
CalendarEvent event,
) async* {
if (event is UpdateCalendar) {
yield* _mapUpdateCalendarToState(event);
} else if (event is UpdateTasks) {
yield* _mapUpdateTasksToState(event);
}
}
bool isSameDay(DateTime a, DateTime b) {
return a.year == b.year && a.month == b.month && a.day == b.day;
}
List<Task> _mapTasksToCalender(List<Task> tasks, DateTime selectedDate) {
return tasks
.where((task) => isSameDay(task.dateAdded, selectedDate))
.toList();
}
DateTime get sss {
DateTime date;
for (int i = 0; i < (filteredTasksBloc.state as FilteredTasksLoaded).filteredTasks.length; i++) {
date = (filteredTasksBloc.state as FilteredTasksLoaded).filteredTasks.elementAt(i).dateAdded;
}
return date;
}
Stream<CalendarState> _mapUpdateCalendarToState(UpdateCalendar event) async* {
if (filteredTasksBloc.state is FilteredTasksLoaded) {
yield CalendarLoaded(
_mapTasksToCalender(
(filteredTasksBloc.state as FilteredTasksLoaded).filteredTasks,
event.selectedDate,
),
event.selectedDate,
);
}
}
Stream<CalendarState> _mapUpdateTasksToState(UpdateTasks event) async* {
final selectedDate =
state is CalendarLoaded ? (state as CalendarLoaded).selectedDate : sss;
yield CalendarLoaded(
_mapTasksToCalender(
(filteredTasksBloc.state as FilteredTasksLoaded).filteredTasks,
selectedDate,
),
selectedDate,
);
}
@override
Future<void> close() {
filteredTasksSubscription.cancel();
return super.close();
}
}
Главный экран:
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
CalendarController _calendarController;
Map<DateTime, List> _events;
List _selectedEvents;
List<Task> tasks;
@override
void initState() {
super.initState();
_calendarController = CalendarController();
}
@override
void dispose() {
_calendarController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: BlocBuilder<CalendarBloc, CalendarState>(
bloc: context.bloc<CalendarBloc>(),
builder: (context, state) {
if (state is CalendarLoading) {
return Center(
child: CircularProgressIndicator(),
);
} else if (state is CalendarLoaded) {
_events = {state.selectedDate: state.tasks};
_selectedEvents = _events[state.selectedDate] ?? [];
_onDaySelected(DateTime dateTime, List children) {
setState(() {
_selectedEvents = children;
});
}
return Stack(
children: <Widget>[
Column(
children: <Widget>[
TunAppBar(),
TunCalendar(
calendarController: _calendarController,
initialSelectedDay: state.selectedDate,
onDaySelected: _onDaySelected,
),
TopContainer(),
Expanded(
child: ListView(
children: _selectedEvents
.map((task) => TaskItem(task: task))
.toList(),
),
),
],
)
],
);
} else {
return Center(
child: Text('Not Loaded'),
);
}
},
),
);
}
}
Спасибо.