How to get appointments between start and end date in Flutter Calendar?
In the Flutter Event Calendar, you can retrieve the total appointments in the visible dates by using the getVisibleAppointments method.
In initState(), set the default values for the calendar.
List<Meeting>? _appointments;
List<Appointment>? _visibleAppointments = <Appointment>[];
List<String> _subjectCollection = <String>[];
late _MeetingDataSource _events;
List<Color> _colorCollection = <Color>[];
@override
void initState() {
_appointments = _addAppointment();
_events = _MeetingDataSource(_appointments!);
super.initState();
}
Use the getVisibleAppointments method in the onViewChanged callback.
void viewChanged(ViewChangedDetails viewChangedDetails) {
List<DateTime> _date = viewChangedDetails.visibleDates;
_visibleAppointments =
_events!.getVisibleAppointments(_date[0], '', _date[_date.length - 1]);
}
Use the onPressed callback of the button to display the appointment details in the alert window.
RaisedButton(
child: Text('Get visible appointments'),
onPressed: _showDialog,
),
_showDialog() async {
await showDialog(
builder: (context) => new AlertDialog(
title: Container(
child: Text("Visible dates contains " +
_visibleAppointments!.length.toString() +
" appointments"),
),
contentPadding: const EdgeInsets.all(16.0),
content: ListView.builder(
itemCount: _visibleAppointments!.length,
itemBuilder: (BuildContext context, int index) {
return Container(
color: _visibleAppointments![index].color,
child: Text(_visibleAppointments![index].subject));
}),
actions: <Widget>[
new FlatButton(
child: const Text('OK'),
onPressed: () {
Navigator.pop(context);
})
],
),
context: context,
);
}
|
