2023-12-17 23:00:31 +01:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:go_router/go_router.dart';
|
|
|
|
import 'package:intl/intl.dart';
|
|
|
|
|
2024-01-09 12:34:44 +01:00
|
|
|
import '../../../utils/definitions/color_pair.dart';
|
|
|
|
import '../../../utils/logic/preferences_service.dart'; // Correct the path as necessary
|
2024-01-01 18:31:10 +01:00
|
|
|
|
2023-12-17 23:00:31 +01:00
|
|
|
class CalendarWidget extends StatefulWidget {
|
|
|
|
final DateTime currentDate;
|
|
|
|
|
2024-01-09 12:34:44 +01:00
|
|
|
const CalendarWidget({Key? key, required this.currentDate}) : super(key: key);
|
2023-12-17 23:00:31 +01:00
|
|
|
|
|
|
|
@override
|
2024-01-09 12:34:44 +01:00
|
|
|
State<CalendarWidget> createState() => _CalendarWidget();
|
2023-12-17 23:00:31 +01:00
|
|
|
}
|
|
|
|
|
2024-01-09 12:34:44 +01:00
|
|
|
class _CalendarWidget extends State<CalendarWidget> {
|
2024-01-01 18:31:10 +01:00
|
|
|
List<DateTime> dateList = []; // Initialized immediately as an empty list
|
|
|
|
Map<String, DiaryEntry?> diaryEntries = {}; // Holds diary entries by date
|
|
|
|
late ColorPair currentColorPair; // Holds the current color pair
|
2023-12-17 23:00:31 +01:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2024-01-01 18:31:10 +01:00
|
|
|
dateList = _generateDateList(widget.currentDate); // Ensured initialization
|
|
|
|
_loadColorPairAndDiaryData();
|
|
|
|
}
|
|
|
|
|
|
|
|
void _loadColorPairAndDiaryData() async {
|
|
|
|
// Load color pair
|
2024-01-09 12:34:44 +01:00
|
|
|
currentColorPair = await PreferencesService().loadColorPair();
|
2024-01-01 18:31:10 +01:00
|
|
|
|
|
|
|
// Generate date list
|
|
|
|
//dateList = _generateDateList(widget.currentDate);
|
|
|
|
|
|
|
|
// Load diary entries for each date
|
|
|
|
for (DateTime date in dateList) {
|
|
|
|
DiaryEntry? entry = await PreferencesService().getDiaryEntryByDate(date);
|
|
|
|
String key = DateFormat('yyyy-MM-dd').format(date);
|
|
|
|
diaryEntries[key] = entry;
|
|
|
|
}
|
|
|
|
setState(() {}); // Update the state to reflect new data
|
2023-12-17 23:00:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
List<DateTime> _generateDateList(DateTime date) {
|
|
|
|
List<DateTime> list = [];
|
|
|
|
DateTime firstOfMonth = DateTime(date.year, date.month, 1);
|
|
|
|
int dayOfWeek = firstOfMonth.weekday;
|
|
|
|
|
|
|
|
// Adjust to Monday start
|
|
|
|
int startDay = dayOfWeek - 1;
|
|
|
|
if (startDay < 0) startDay = 6;
|
|
|
|
|
2024-01-08 19:34:48 +01:00
|
|
|
// Dates from previous month to fill the first row
|
2023-12-17 23:00:31 +01:00
|
|
|
for (int i = startDay; i > 0; i--) {
|
|
|
|
list.add(firstOfMonth.subtract(Duration(days: i)));
|
|
|
|
}
|
|
|
|
|
2024-01-08 19:34:48 +01:00
|
|
|
// Dates of current month up to the current date
|
2023-12-17 23:00:31 +01:00
|
|
|
int daysInMonth = DateUtils.getDaysInMonth(date.year, date.month);
|
2024-01-08 19:34:48 +01:00
|
|
|
DateTime today = DateTime.now();
|
2023-12-17 23:00:31 +01:00
|
|
|
for (int i = 0; i < daysInMonth; i++) {
|
2024-01-08 19:34:48 +01:00
|
|
|
DateTime current = DateTime(date.year, date.month, i + 1);
|
|
|
|
if (current.isAfter(today)) break; // Stop adding dates after today
|
|
|
|
list.add(current);
|
2023-12-17 23:00:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Adjust list to end with a complete week
|
|
|
|
while (list.length % 7 != 0) {
|
2024-01-09 12:34:44 +01:00
|
|
|
list.add(list.last.add(const Duration(days: 1)));
|
2023-12-17 23:00:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-12-18 17:51:22 +01:00
|
|
|
double addon = dateList.length > 35 ? 50 : 0;
|
2024-01-08 19:34:48 +01:00
|
|
|
String monthName = DateFormat("MMMM yyyy").format(widget.currentDate); // For month header
|
2024-01-01 18:31:10 +01:00
|
|
|
|
|
|
|
return FutureBuilder<ColorPair>(
|
|
|
|
future: PreferencesService().loadColorPair(),
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (!snapshot.hasData) {
|
2024-01-09 12:34:44 +01:00
|
|
|
return const Center(child: CircularProgressIndicator());
|
2024-01-01 18:31:10 +01:00
|
|
|
}
|
|
|
|
ColorPair colorPair = snapshot.data!;
|
|
|
|
|
2024-01-09 12:34:44 +01:00
|
|
|
return SizedBox(
|
2024-01-01 18:31:10 +01:00
|
|
|
height: 360 + addon,
|
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
Padding(
|
2024-01-09 12:34:44 +01:00
|
|
|
padding: const EdgeInsets.symmetric(vertical: 8.0),
|
|
|
|
child: Text(monthName, style: const TextStyle(fontSize: 20, fontWeight: FontWeight.bold)), // Month Header
|
2024-01-01 18:31:10 +01:00
|
|
|
),
|
|
|
|
Expanded(
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.fromLTRB(25, 0, 25, 0),
|
|
|
|
child: GridView.builder(
|
2024-01-09 12:34:44 +01:00
|
|
|
physics: const NeverScrollableScrollPhysics(),
|
|
|
|
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
|
2024-01-01 18:31:10 +01:00
|
|
|
crossAxisCount: 7,
|
2023-12-17 23:00:31 +01:00
|
|
|
),
|
2024-01-01 18:31:10 +01:00
|
|
|
itemBuilder: (context, index) {
|
|
|
|
DateTime date = dateList[index];
|
2024-01-08 19:34:48 +01:00
|
|
|
bool isCurrentMonth = date.month == widget.currentDate.month;
|
2024-01-01 18:31:10 +01:00
|
|
|
double opacity = isCurrentMonth ? 1.0 : 0.15;
|
|
|
|
|
|
|
|
// Fetching diary entry percentage value
|
2024-01-08 19:34:48 +01:00
|
|
|
DiaryEntry? entry = diaryEntries[DateFormat('yyyy-MM-dd').format(date)];
|
|
|
|
double fillPercentage = entry != null ? entry.percentValue / 100.0 : 0.0;
|
2024-01-01 18:31:10 +01:00
|
|
|
|
|
|
|
return Center(
|
|
|
|
child: GestureDetector(
|
2024-01-09 12:34:44 +01:00
|
|
|
onTap: () {
|
|
|
|
DiaryEntry? entry = diaryEntries[DateFormat('yyyy-MM-dd').format(date)];
|
|
|
|
if (entry != null) {
|
|
|
|
context.go('/entry', extra: date);
|
|
|
|
} else {
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
const SnackBar(
|
|
|
|
content: Text("no entry there :("),
|
|
|
|
duration: Duration(seconds: 2),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
2024-01-01 18:31:10 +01:00
|
|
|
child: Container(
|
|
|
|
width: 50,
|
|
|
|
height: 50,
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
shape: BoxShape.circle,
|
2024-01-08 19:34:48 +01:00
|
|
|
border: Border.all(color: Colors.black, width: 2),
|
|
|
|
color: DateTime.now().day == date.day && DateTime.now().month == date.month && DateTime.now().year == date.year
|
|
|
|
? Colors.transparent // Setting background color for current day
|
2024-01-01 18:31:10 +01:00
|
|
|
: isCurrentMonth
|
|
|
|
? Colors.transparent
|
|
|
|
: Colors.grey.withOpacity(opacity),
|
|
|
|
),
|
|
|
|
child: Stack(
|
|
|
|
alignment: Alignment.center,
|
|
|
|
children: [
|
|
|
|
CircleWidget(
|
2024-01-08 19:34:48 +01:00
|
|
|
radius: 25 * fillPercentage, color: fillPercentage > 0 ? colorPair.backgroundColor : Colors.teal.withOpacity(opacity)),
|
2024-01-01 18:31:10 +01:00
|
|
|
Text(
|
2024-01-08 19:34:48 +01:00
|
|
|
DateTime.now().day == date.day && DateTime.now().month == date.month && DateTime.now().year == date.year
|
|
|
|
? "today"
|
|
|
|
: DateFormat("d").format(date),
|
2024-01-09 12:34:44 +01:00
|
|
|
style: const TextStyle(color: Colors.black),
|
2024-01-01 18:31:10 +01:00
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
itemCount: dateList.length,
|
|
|
|
),
|
2023-12-17 23:00:31 +01:00
|
|
|
),
|
|
|
|
),
|
2024-01-01 18:31:10 +01:00
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2023-12-17 23:00:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class CircleWidget extends StatelessWidget {
|
|
|
|
final double radius;
|
|
|
|
final Color color;
|
|
|
|
|
2024-01-09 12:34:44 +01:00
|
|
|
const CircleWidget({Key? key, required this.radius, required this.color}) : super(key: key);
|
2023-12-17 23:00:31 +01:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Container(
|
|
|
|
width: radius * 2,
|
|
|
|
height: radius * 2,
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
shape: BoxShape.circle,
|
|
|
|
color: color,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|