Reputation: 308
I am following this tutorial to make a calendar in my flutter app. For some reason, the events are not showing when you click on the date with an event:
As you can see, when I click on 9, which has an event, it is not showing as it does in the video. I am not too sure why this isn't happening as I have followed the tutorial exactly and just changed the colours.
This is the code:
class SecondRoute extends StatefulWidget {
@override
_SecondRoute createState() => _SecondRoute();
}
class _SecondRoute extends State<SecondRoute> {
CalendarController _controller;
Map<DateTime, List<dynamic>> _events;
List<dynamic> _selectedEvents;
@override
void initState() {
super.initState();
_controller = CalendarController();
_events = {};
_selectedEvents = [];
}
Map<DateTime, List<dynamic>> _groupEvents(List<EventModel> events) {
Map<DateTime, List<dynamic>> data = {};
events.forEach((event) {
DateTime date = DateTime(
event.eventDate.year, event.eventDate.month, event.eventDate.day, 12);
if (data[date] == null) data[date] = [];
data[date].add(event);
});
return data;
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0xFF030164),
appBar: AppBar(
title: Text('Calendar'),
),
body: StreamBuilder<List<EventModel>>(
stream: eventDBS.streamList(),
builder: (context, snapshot) {
if (snapshot.hasData) {
List<EventModel> allEvents = snapshot.data;
if (allEvents.isNotEmpty) {
_events = _groupEvents(allEvents);
}
}
return SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
TableCalendar(
events: _events,
initialCalendarFormat: CalendarFormat.month,
calendarStyle: CalendarStyle(
canEventMarkersOverflow: true,
todayColor: Color(0xFF974FF9),
selectedColor: Theme.of(context).primaryColor,
todayStyle: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18.0,
color: Colors.white)),
headerStyle: HeaderStyle(
centerHeaderTitle: true,
formatButtonDecoration: BoxDecoration(
color: Color(0xFF974FF9),
borderRadius: BorderRadius.circular(20.0),
),
formatButtonTextStyle: TextStyle(color: Colors.white),
formatButtonShowsNext: false,
),
startingDayOfWeek: StartingDayOfWeek.monday,
onDaySelected: (context, date, events) {
setState(() {
_selectedEvents = events;
});
},
builders: CalendarBuilders(
selectedDayBuilder: (context, date, events) =>
Container(
margin: const EdgeInsets.all(4.0),
alignment: Alignment.center,
decoration: BoxDecoration(
color: Theme.of(context).primaryColor,
borderRadius: BorderRadius.circular(10.0)),
child: Text(
date.day.toString(),
style: TextStyle(color: Colors.white),
)),
todayDayBuilder: (context, date, events) => Container(
margin: const EdgeInsets.all(4.0),
alignment: Alignment.center,
decoration: BoxDecoration(
color: Color(0xFF974FF9),
borderRadius: BorderRadius.circular(10.0)),
child: Text(
date.day.toString(),
style: TextStyle(color: Colors.white),
)),
),
calendarController: _controller,
),
..._selectedEvents.map((event) => ListTile(
title: Text(event.title),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (_) => EventDetailsPage(
event: event,
)));
},
)),
],
),
);
}),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
backgroundColor: Color(0xff17F0FC),
onPressed: () => Navigator.pushNamed(context, 'add_event'),
));
}
}
Any help would be appreciated!
Upvotes: 0
Views: 136
Reputation: 308
I figured out the issue, in onDaySelected
the (context, date, events)
should be ordered like (date, events, context)
instead:
onDaySelected: (date, events, context) {
setState(() {
_selectedEvents = events;
});
},
Upvotes: 1