1

我正在构建一个颤振应用程序,其中有一个同步融合日历,您可以向其中添加事件。但是,当我尝试添加事件时,它不会被添加,因此尽管在添加事件后弹出“添加事件”页面,但它不会显示在日历上。

访问添加事件的类“InsertEvent”的代码:

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,
      );

保存事件函数:

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();
    }
  }

插入事件类:

class InsertEvent {
  final List<Events> _events = [];
  List<Events> get events => _events;

  void addEvent(Events event) {
    _events.add(event);
  }
}

我使用“var insertEventInstance = InsertEvent();” 访问课程并尝试添加事件。虽然 saveEvent 函数在我单击添加后弹出(如代码中所写),但事件及其详细信息根本不会出现。

日历数据源:

class CalendarWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var eventInstance = InsertEvent();
    final events = eventInstance.events;

    return SfCalendar(
        dataSource: EventDataSource(events),
  );
}

在日历的数据源中调用事件时是否有错误?如何显示添加的事件?

4

1 回答 1

0

通过使用CalendarDataSource的notifyListeners,您可以在添加、删除、编辑约会时将更改反映在 UI 上。我们有针对此要求的 KB 文档。请从以下链接中找到 KB,

知识库链接: https ://www.syncfusion.com/kb/11204/how-to-design-and-configure-your-appointment-editor-in-flutter-event-calendar-sfcalendar

此外,我们还有一个 KB 文档,用于从日历中删除约会并将约会添加到日历中。请从以下链接中找到 kB。

知识库链接: https ://www.syncfusion.com/kb/11522/how-to-delete-an-appointment-in-the-flutter-event-calendar-sfcalendar

https://www.syncfusion.com/kb/12300/how-to-add-the-appointments-using-the-ontap-callback-in-the-flutter-calendar

于 2021-10-25T11:07:40.103 回答