因此,我使用 Table Calendar 包在 Flutter 中构建了一个日历应用程序,但一旦我将事件实现到日历中,它就无法工作。我很快就遇到了 multidex 错误,但我不知道为什么。 作为参考,我正在 Mac 上工作。
这是我的日历课程:
import 'package:flutter/material.dart';
import 'package:table_calendar/table_calendar.dart';
import 'event.dart';
class Calendar extends StatefulWidget {
@override
_CalendarState createState() => _CalendarState();
}
class _CalendarState extends State<Calendar> {
late Map<DateTime, List<Event>> selectedEvents;
CalendarFormat format = CalendarFormat.month;
DateTime selectedDay = DateTime.now();
DateTime focusedDay = DateTime.now();
TextEditingController _eventController = TextEditingController();
@override
void initState() {
selectedEvents = {};
super.initState();
}
List<Event> _getEventsfromDay(DateTime date) {
return selectedEvents[date] ?? [];
}
@override
void dispose() {
_eventController.dispose();
super.dispose();
}
@override
Widget build (BuildContext context) {
return Scaffold (
appBar: AppBar(
title: Text('calendar'),
centerTitle: true,
),
body: Column(
children: [
TableCalendar(
focusedDay: selectedDay,
firstDay: DateTime(2000),
lastDay: DateTime(2050),
calendarFormat: format,
onFormatChanged: (CalendarFormat _format) {
setState(() {
format = _format;
});
},
startingDayOfWeek: StartingDayOfWeek.monday,
daysOfWeekVisible: true,
//Day Changed
onDaySelected: (DateTime selectDay, DateTime focusDay) {
setState(() {
selectedDay = selectDay;
focusedDay = focusDay;
});
print(focusedDay);
},
selectedDayPredicate: (DateTime date) {
return isSameDay(selectedDay, date);
},
eventLoader: _getEventsfromDay,
//Styling
calendarStyle: CalendarStyle(
isTodayHighlighted: true,
selectedDecoration: BoxDecoration(
color: Color(0xff56E2E1),
borderRadius: BorderRadius.circular(5.0),
),
selectedTextStyle: TextStyle(color: Colors.white),
todayDecoration: BoxDecoration(
color: Colors.blueAccent,
shape: BoxShape.rectangle,
borderRadius: BorderRadius.circular(5.0),
),
defaultDecoration: BoxDecoration(
shape: BoxShape.rectangle,
borderRadius: BorderRadius.circular(5.0),
),
weekendDecoration: BoxDecoration(
shape: BoxShape.rectangle,
borderRadius: BorderRadius.circular(5.0),
),
),
headerStyle: HeaderStyle(
formatButtonVisible: false,
titleCentered: false,
formatButtonShowsNext: false,
),
),
..._getEventsfromDay(selectedDay).map(
(Event event) => ListTile(
title: Text(
event.title,
),
),
),
],
),
floatingActionButton: FloatingActionButton.extended(
onPressed: () => showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text ('Add Event'),
content: TextFormField(
controller: _eventController,
),
actions: [
TextButton(
child: Text('cancel'),
onPressed: () => Navigator.pop(context),
),
TextButton(
child: Text('ok'),
onPressed: () {
if (_eventController.text.isEmpty) {
} else {
if (selectedEvents[selectedDay] != null) {
selectedEvents[selectedDay]?.add(
Event(title: _eventController.text)
);
} else {
selectedEvents[selectedDay] = [
Event(title: _eventController.text)
];
}
}
Navigator.pop(context);
_eventController.clear();
setState(() {});
return;
},
),
],
),
),
label: Text('add event'),
icon: Icon(Icons.add),
),
);
}
}
这是事件类:
class Event {
final String title;
Event({required this.title});
String toString() => this.title;
}
我已经尝试重新编写整个应用程序,但没有成功。 我对 Flutter 和编码相当陌生,对在这里做什么完全一无所知,我只是想添加功能,然后稍后将所有事件添加到 sqlite 数据库中。
默认情况下,Android 应用程序具有
SingleDex
支持。这将您的应用程序限制为仅 65536 个方法,Multidex
意味着您现在可以在应用程序中编写超过 65536 个方法。
要解决此问题,请转到:
android
-> app
-> build.gradle
在
multiDexEnabled true
中添加
default config {}
defaultConfig {
applicationId "com.example.myapp"
minSdkVersion flutter.minSdkVersion
targetSdkVersion flutter.targetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
multiDexEnabled true // <- Add This Line
}
我从这里获取了参考。