【问题标题】:Table Calendar stacking events for specific date - Flutter特定日期的表日历堆叠事件 - Flutter
【发布时间】:2021-01-08 02:36:09
【问题描述】:

我正在使用 Flutter、带有表格日历的 Firestore。

这是我的代码,目前正在运行,但有一个小故障

              //Initialize Events
              List<Event> eventList = List<Event>();
              //Get events and add them to the list.
              eventsSnapshot.data.documents.forEach((doc) {
                Timestamp _eventDateStart = doc.data['eventDateStart'];
                Timestamp _eventDateFinish = doc.data['eventDateFinish'];
                Event _thisEvent = Event('test',
                      doc.data['eventName'],
                      doc.data['eventPrice'],
                      doc.data['eventDescription'],
                      _eventDateStart.toDate(),
                      _eventDateFinish.toDate());
                print('Event added : ${_thisEvent.eventName.toString()}');
                eventList.add(_thisEvent);
               });
               
              _events = convertToMap(eventList);

这是我的 converToMap

class Event {
  final String id;
  final String eventName;
  final double eventPrice;
  final String eventDescription;
  final DateTime eventDateStart;
  final DateTime eventDateFinish;

  Event(this.id, this.eventName, this.eventPrice,this.eventDescription, this.eventDateStart, this.eventDateFinish);
}

//method to change calendar item to Map<DateTime,List>
Map<DateTime, List<Event>> convertToMap(List<Event> item) {
  Map<DateTime, List<Event>> result;

  for (int i = 0; i < item.length; i++) {
    Event data = item[i];
    //get the date and convert it to a DateTime variable
    DateTime currentDate = data.eventDateStart;
    List<Event> events = [];
    //add the event name to the the eventNames list for the current date.
    //search for another event with the same date and populate the eventNames List.
    for (int j = 0; j < item.length; j++) {
      //create temp calendarItemData object.
      Event temp = item[j];
      //establish that the temp date is equal to the current date
      if (data.eventDateStart == temp.eventDateStart) {
        //add the event name to the event List.
        events.add(temp);
      } //else continue
    }

    //add the date and the event to the map if the date is not contained in the map
    if (result == null) {
      result = {currentDate: events};
    } else {

      result[currentDate] = events;
    }
  }
  print(result);
  return result;
}

打印出来的效果就是这个。

I/flutter (1655):添加事件:deuxio I/flutter(1655):添加事件:PremierVraiTest I/flutter(1655):添加事件:测试 我/颤振(1655):{2020-09-17 13:00:00.000:[“事件”实例],2020-09-17 12:00:00.000:[“事件”实例],2020-09- 18 12:00:00.000:[“事件”实例]}

现在的问题: 当我检查我的日历时,我看到 17 的 1 个事件和 18 的 1 个事件。 17 事件是具有 13:00 的事件。我没有看到第二个事件。

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您可以在下面复制粘贴运行完整代码
    原因 2020-09-17 13:00:00 != 2020-09-17 12:00:00,当使用 Map result[currentDate] 你不会得到 2 个事件
    您只能使用DateTime(year, month, day)
    代码sn-p

      DateTime currentDate = DateTime(data.eventDateStart.year,
          data.eventDateStart.month, data.eventDateStart.day);
    
      ...
      for (int j = 0; j < item.length; j++) {
        ...
        if (DateTime(data.eventDateStart.year, data.eventDateStart.month,
                data.eventDateStart.day) ==
            DateTime(temp.eventDateStart.year, temp.eventDateStart.month,
                temp.eventDateStart.day)) {
    

    输出

    I/flutter (21975): {2020-09-17 00:00:00.000: [Instance of 'Event', Instance of 'Event']}
    I/flutter (21975): 2
    

    完整代码

    import 'package:flutter/material.dart';
    
    class Event {
      final String id;
      final String eventName;
      final double eventPrice;
      final String eventDescription;
      final DateTime eventDateStart;
      final DateTime eventDateFinish;
    
      Event(this.id, this.eventName, this.eventPrice, this.eventDescription,
          this.eventDateStart, this.eventDateFinish);
    }
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
            visualDensity: VisualDensity.adaptivePlatformDensity,
          ),
          home: MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key, this.title}) : super(key: key);
    
      final String title;
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      int _counter = 0;
      List<Event> eventList = [
        Event("1", "a", 123.0, "a desc", DateTime(2020, 9, 17, 13, 0, 0),
            DateTime(2020, 9, 17, 13, 0, 0)),
        Event("2", "b", 456.0, "b desc", DateTime(2020, 9, 17, 12, 0, 0),
            DateTime(2020, 9, 17, 13, 0, 0))
      ];
    
      Map<DateTime, List<Event>> convertToMap(List<Event> item) {
        Map<DateTime, List<Event>> result;
        for (int i = 0; i < item.length; i++) {
          Event data = item[i];
          //get the date and convert it to a DateTime variable
          //DateTime currentDate = data.eventDateStart;
    
          DateTime currentDate = DateTime(data.eventDateStart.year,
              data.eventDateStart.month, data.eventDateStart.day);
    
          List<Event> events = [];
          //add the event name to the the eventNames list for the current date.
          //search for another event with the same date and populate the eventNames List.
          for (int j = 0; j < item.length; j++) {
            //create temp calendarItemData object.
            Event temp = item[j];
            //establish that the temp date is equal to the current date
            if (DateTime(data.eventDateStart.year, data.eventDateStart.month,
                    data.eventDateStart.day) ==
                DateTime(temp.eventDateStart.year, temp.eventDateStart.month,
                    temp.eventDateStart.day)) {
              //add the event name to the event List.
              events.add(temp);
            } //else continue
          }
    
          //add the date and the event to the map if the date is not contained in the map
          if (result == null) {
            result = {currentDate: events};
          } else {
            result[currentDate] = events;
          }
        }
        print(result);
        print(result[DateTime(2020, 9, 17, 0, 0, 0)].length);
        return result;
      }
    
      void _incrementCounter() {
        convertToMap(eventList);
        setState(() {
          _counter++;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text(
                  'You have pushed the button this many times:',
                ),
                Text(
                  '$_counter',
                  style: Theme.of(context).textTheme.headline4,
                ),
              ],
            ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: 'Increment',
            child: Icon(Icons.add),
          ),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-13
      • 2020-06-02
      • 2014-02-05
      • 2022-11-18
      • 1970-01-01
      • 2013-03-06
      • 1970-01-01
      相关资源
      最近更新 更多