I am building a flutter app where there is a syncfusion calendar and you can add events to it. However, when I try to add an event, it does not get added and thus, is not displayed on the calendar despite the "Add Event" page popping after supposedly adding the event.
Code to access the class "InsertEvent" where you add event:
class EditEventsPage extends StatefulWidget {
final Events? event;
const EditEventsPage({Key? key, this.event}) : super(key: key);
@override
_EditEventsPageState createState() => _EditEventsPageState();
}
class _EditEventsPageState extends State<EditEventsPage> {
var insertEventInstance = InsertEvent();
final _formKey = GlobalKey<FormState>();
final titleController = TextEditingController();
late DateTime fromDate;
late DateTime toDate;
@override
void initState() {
super.initState();
if (widget.event == null) {
fromDate = DateTime.now();
toDate = DateTime.now().add(Duration(hours: 4));
}
}
@override
void dispose() {
titleController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Color(0xFF1c3f77),
elevation: 0,
leading: CloseButton(),
actions: buildEditingActions(),
),
body: SingleChildScrollView(
padding: EdgeInsets.all(12),
child: Form(
key: _formKey,
child: Column(mainAxisSize: MainAxisSize.min, children: <Widget>[
buildTitle(),
SizedBox(height: 25),
buildDateTime(),
])),
),
);
}
List<Widget> buildEditingActions() => [
ElevatedButton.icon(
style: ElevatedButton.styleFrom(
primary: Colors.transparent, shadowColor: Colors.transparent),
icon: Icon(Icons.done),
label: Text(
'Add',
style: TextStyle(color: Colors.white, fontSize: 15),
),
onPressed: saveEvent,
)
];
Widget buildTitle() => TextFormField(
style: TextStyle(fontSize: 15),
decoration:
InputDecoration(border: UnderlineInputBorder(), hintText: 'Title'),
onFieldSubmitted: (_) => saveEvent,
validator: (title) =>
title != null && title.isEmpty ? 'Title cannot be empty' : null,
controller: titleController,
);
saveEvent function:
Future saveEvent() async {
final isValid = _formKey.currentState!.validate();
if (isValid) {
final event = Events(
title: titleController.text,
details: 'testing 1 2 3',
from: fromDate,
to: toDate,
isAllday: false,
);
insertEventInstance.addEvent(event);
Navigator.of(context).pop();
}
}
InsertEvent class:
class InsertEvent {
final List<Events> _events = [];
List<Events> get events => _events;
void addEvent(Events event) {
_events.add(event);
}
}
I used "var insertEventInstance = InsertEvent();" to get access to the class and tried to add an event. Although the saveEvent function does pop after I click add (as written in the code), the event and its details do not appear at all.
Calendar Datasource :
class CalendarWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
var eventInstance = InsertEvent();
final events = eventInstance.events;
return SfCalendar(
dataSource: EventDataSource(events),
);
}
Is there an error somewhere in calling events in datasource for the calendar? How can I display the added events?