gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 04:36:35 26 4
gpt4 key购买 nike

我正在使用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):添加了事件:测试
I / flutter(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。我看不到第二件事。

最佳答案

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

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

关于flutter - 特定日期的表日历堆叠事件-Flutter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64001757/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com