我正在使用此plugin。我的目标是向日历添加标记。这已实现,但也会在同一天(上一个月或下个月)添加间歇性日期。因此,如果地图包含一个DateTime
,则日历应显示该日期,并用一个图标表示。发生的事情是:如果添加4月30日... 5月30日也是。该月的早些日子填充上个月,而后一天则填充下个月。这仅在前几天/后几天发生。从代码中您可以看到markedDateMap
映射用于填充月份。并且initState()
中仅添加了一个日期...那么为什么日历中要填充其他日期呢?
代码:
import 'package:flutter/material.dart';
import 'package:flutter_calendar_carousel/flutter_calendar_carousel.dart';
import 'package:flutter_calendar_carousel/classes/event.dart';
import 'package:flutter_calendar_carousel/classes/event_list.dart';
import 'auth_provider.dart';
class CalendarPage2 extends StatefulWidget {
@override
_CalendarPage2State createState() => new _CalendarPage2State();
}
List<DateTime> presentDates;
class _CalendarPage2State extends State<CalendarPage2> {
static Widget _presentIcon(String day) => Container(
decoration: BoxDecoration(
color: Colors.green,
borderRadius: BorderRadius.all(
Radius.circular(1000),
),
),
child: Center(
child: Text(
day,
style: TextStyle(
color: Colors.black,
),
),
),
);
EventList<Event> _markedDateMap = new EventList<Event>(
events: {},
);
CalendarCarousel _calendarCarousel;
double cHeight;
@override
Widget build(BuildContext context) {
cHeight = MediaQuery.of(context).size.height;
_calendarCarousel = CalendarCarousel<Event>(
height: cHeight * 0.70,
weekendTextStyle: TextStyle(
color: Colors.red,
),
todayButtonColor: Colors.blue[200],
markedDatesMap: _markedDateMap,
markedDateShowIcon: true,
//markedDateIconMaxShown: 1,
markedDateMoreShowTotal:
null, // null for not showing hidden events indicator
markedDateIconBuilder: (event) {
return event.icon;
},
);
return new Scaffold(
appBar: new AppBar(
title: new Text("Calender"),
),
body: _calendarCarousel,
);
}
Widget markerRepresent(Color color, String data) {
return new ListTile(
leading: new CircleAvatar(
backgroundColor: color,
radius: cHeight * 0.022,
),
title: new Text(
data,
),
);
}
@override
void initState() {
super.initState();
presentDates = [
DateTime(2019, 9, 1
)];
for (int i = 0; i < presentDates.length; i++) {
print('££££££££££££££££££££££££££££££££££££££££££££££££££££££££££££££££££££££££££££££££');
_markedDateMap.add(
presentDates[i],
new Event(
date: presentDates[i],
title: 'Event 5',
icon: _presentIcon(
presentDates[i].day.toString(),
),
),
);
print(presentDates);
}
}
}