I am trying to change the backgroundColor of an event based on the subject String Value of the event. I am currently mapping the color of the event with a Get Color at the end of the program, but I am having trouble getting any color other than the default Blue for the events dots on the month view and in the Agenda View.
I tried many different ways (Making an If Statement based on the subject, etc...), but nothing seems to give me any other color than the default Blue. Even my current code which uses a greenish FromARGB Color gives me nothing but the default color.
Here is my current code (Which connects to a FireStore database).
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:syncfusion_flutter_calendar/calendar.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:syncfusion_localizations/syncfusion_localizations.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
SfGlobalLocalizations.delegate,
],
// ignore: prefer_const_literals_to_create_immutables
supportedLocales: const [
Locale('en'),
Locale('fr'),
],
locale: const Locale('fr'),
title: 'Calendrier MDL Énergie',
theme: ThemeData(primarySwatch: Colors.green),
themeMode: ThemeMode.system,
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
MeetingDataSource? events;
final databaseReference = FirebaseFirestore.instance;
@override
void initState() {
getDataFromFireStore().then((results) {
SchedulerBinding.instance.addPostFrameCallback((timeStamp) {
setState(() {});
});
});
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Calendrier MDL Énergie"),
backgroundColor: const Color.fromARGB(255, 39, 80, 81)),
body: SfCalendar(
view: CalendarView.month,
monthViewSettings: const MonthViewSettings(
showAgenda: true,
agendaViewHeight: 350,
agendaItemHeight: 65,
),
dataSource: events,
todayHighlightColor: Colors.green),
);
}
Future<void> getDataFromFireStore() async {
var snapShotsValue =
await databaseReference.collection("CalendarEventsCollection").get();
List<Meeting> list = snapShotsValue.docs
.map((e) => Meeting(
eventName: e.data()['Subject'],
from: formatTimestamp(e.data()['StartTime']),
to: formatTimestamp(e.data()['EndTime']),
background: Color.fromARGB(255, 103, 130, 157),
isAllDay: true))
.toList();
events = MeetingDataSource(list);
}
DateTime formatTimestamp(Timestamp timestamp) {
return timestamp.toDate();
}
}
class MeetingDataSource extends CalendarDataSource {
MeetingDataSource(List<Meeting> source) {
appointments = source;
}
@override
DateTime getStartTime(int index) {
return appointments![index].from;
}
@override
DateTime getEndTime(int index) {
return appointments![index].to;
}
@override
bool isAllDay(int index) {
return appointments![index].isAllDay;
}
@override
String getSubject(int index) {
return appointments![index].eventName;
}
@override
Color GetColor(int index) {
return appointments![index].background();
}
}
class Meeting {
String? eventName;
DateTime? from;
DateTime? to;
Color? background;
bool? isAllDay;
Meeting({this.eventName, this.from, this.to, this.background, this.isAllDay});
}