Flutter 表特定日期的日历堆叠事件-颤振

Flutter 表特定日期的日历堆叠事件-颤振,flutter,dart,Flutter,Dart,我正在使用Flatter、firestore和台历 这是我的代码,目前正在工作,但有一个小故障 //Initialize Events List<Event> eventList = List<Event>(); //Get events and add them to the list. eventsSnapshot.data.documents.forEac

我正在使用Flatter、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);
//初始化事件
List eventList=List();
//获取事件并将其添加到列表中。
eventsSnapshot.data.documents.forEach((doc){
时间戳_eventDateStart=doc.data['eventDateStart'];
时间戳_eventDateFinish=doc.data['eventDateFinish'];
事件_thisEvent=事件(“测试”,
文档数据['eventName'],
单据数据['eventPrice'],
文档数据['eventDescription'],
_eventDateStart.toDate(),
_eventDateFinish.toDate());
打印('添加的事件:${u thisEvent.eventName.toString()}');
事件列表。添加(_thisEvent);
});
_事件=转换映射(事件列表);
这是我的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;
}
类事件{
最终字符串id;
最终字符串eventName;
最终价格;
最终字符串事件描述;
最终日期时间事件日期开始;
最终日期时间事件日期完成;
事件(this.id、this.eventName、this.eventPrice、this.eventDescription、this.eventDateStart、this.eventDateFinish);
}
//方法将日历项更改为映射
映射转换映射(列表项){
地图结果;
对于(int i=0;i
打印的结果是这样的

I/颤振(1655):增加事件:双雄 I/颤振(1655):新增事件:PremierVraiTest I/颤振(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点的比赛。我没有看到第二个事件。

您可以复制下面的粘贴运行完整代码
原因
2020-09-17 13:00:00
!=
2020-09-17 12:00:00
,当使用
Map
result[currentDate]
时,您不会得到2个事件
您只能使用日期时间(年、月、日)

代码片段

  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)) {
完整代码

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),
      ),
    );
  }
}
导入“包装:颤振/材料.省道”;
班级活动{
最终字符串id;
最终字符串eventName;
最终价格;
最终字符串事件描述;
最终日期时间事件日期开始;
最终日期时间事件日期完成;
事件(this.id、this.eventName、this.eventPrice、this.eventDescription、,
this.eventDateStart、this.eventDateFinish);
}
void main(){
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
视觉密度:视觉密度。自适应平台密度,
),
主页:MyHomePage(标题:“颤振演示主页”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
int _计数器=0;
列表事件列表=[
事件(“1”、“a”、123.0、“a描述”、日期时间(2020、9、17、13、0、0),
日期时间(2020,9,17,13,0,0)),
事件(“2”、“b”、456.0、“b描述”、日期时间(2020、9、17、12、0、0),
日期时间(2020,9,17,13,0,0))
];
映射转换映射(列表项){
地图结果;
对于(int i=0;iimport '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),
      ),
    );
  }
}