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-01 18:31:10 +01:00
|
|
|
import '../../../utils/definitions/ColorPairs.dart';
|
|
|
|
import '../../../utils/logic/PreferencesService.dart'; // Correct the path as necessary
|
|
|
|
|
2023-12-17 23:00:31 +01:00
|
|
|
class CalendarWidget extends StatefulWidget {
|
|
|
|
final DateTime currentDate;
|
|
|
|
|
|
|
|
CalendarWidget({Key? key, required this.currentDate}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
_CalendarWidgetState createState() => _CalendarWidgetState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _CalendarWidgetState 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
|
|
|
|
currentColorPair =
|
|
|
|
await PreferencesService().loadColorPair() ?? colorPairs[0];
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
// Dates from previous month
|
|
|
|
for (int i = startDay; i > 0; i--) {
|
|
|
|
list.add(firstOfMonth.subtract(Duration(days: i)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dates of current month
|
|
|
|
int daysInMonth = DateUtils.getDaysInMonth(date.year, date.month);
|
|
|
|
for (int i = 0; i < daysInMonth; i++) {
|
|
|
|
list.add(DateTime(date.year, date.month, i + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adjust list to end with a complete week
|
|
|
|
while (list.length % 7 != 0) {
|
|
|
|
list.add(list.last.add(Duration(days: 1)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-12-18 17:51:22 +01:00
|
|
|
double addon = dateList.length > 35 ? 50 : 0;
|
2024-01-01 18:31:10 +01:00
|
|
|
String monthName =
|
|
|
|
DateFormat("MMMM yyyy").format(widget.currentDate); // For month header
|
|
|
|
|
|
|
|
return FutureBuilder<ColorPair>(
|
|
|
|
future: PreferencesService().loadColorPair(),
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (!snapshot.hasData) {
|
|
|
|
return Center(child: CircularProgressIndicator());
|
|
|
|
}
|
|
|
|
ColorPair colorPair = snapshot.data!;
|
|
|
|
|
|
|
|
return Container(
|
|
|
|
height: 360 + addon,
|
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.symmetric(vertical: 8.0),
|
|
|
|
child: Text(monthName,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 20,
|
|
|
|
fontWeight: FontWeight.bold)), // Month Header
|
|
|
|
),
|
|
|
|
Expanded(
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.fromLTRB(25, 0, 25, 0),
|
|
|
|
child: GridView.builder(
|
|
|
|
physics: NeverScrollableScrollPhysics(),
|
|
|
|
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
|
|
|
|
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];
|
|
|
|
bool isCurrentMonth =
|
|
|
|
date.month == widget.currentDate.month;
|
|
|
|
double opacity = isCurrentMonth ? 1.0 : 0.15;
|
|
|
|
|
|
|
|
// Fetching diary entry percentage value
|
|
|
|
DiaryEntry? entry =
|
|
|
|
diaryEntries[DateFormat('yyyy-MM-dd').format(date)];
|
|
|
|
double fillPercentage =
|
|
|
|
entry != null ? entry.percentValue / 100.0 : 0.0;
|
|
|
|
|
|
|
|
return Center(
|
|
|
|
child: GestureDetector(
|
|
|
|
onTap: () => {context.go('/entry', extra: date)},
|
|
|
|
child: Container(
|
|
|
|
width: 50,
|
|
|
|
height: 50,
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
shape: BoxShape.circle,
|
|
|
|
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
|
|
|
|
.orange // Setting background color for current day
|
|
|
|
: isCurrentMonth
|
|
|
|
? Colors.transparent
|
|
|
|
: Colors.grey.withOpacity(opacity),
|
|
|
|
),
|
|
|
|
child: Stack(
|
|
|
|
alignment: Alignment.center,
|
|
|
|
children: [
|
|
|
|
CircleWidget(
|
|
|
|
radius: 25 * fillPercentage,
|
|
|
|
color: fillPercentage > 0
|
|
|
|
? colorPair.backgroundColor
|
|
|
|
: Colors.teal.withOpacity(opacity)),
|
|
|
|
Text(
|
|
|
|
DateFormat("d").format(date),
|
|
|
|
style: TextStyle(color: Colors.black),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
itemCount: dateList.length,
|
|
|
|
),
|
2023-12-17 23:00:31 +01:00
|
|
|
),
|
|
|
|
),
|
2024-01-01 18:31:10 +01:00
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildDateCell(DateTime date, DiaryEntry? entry) {
|
|
|
|
bool isCurrentMonth = date.month == widget.currentDate.month;
|
|
|
|
double opacity = isCurrentMonth ? 1.0 : 0.5;
|
|
|
|
Color bgColor = entry != null
|
|
|
|
? currentColorPair.backgroundColor.withOpacity(opacity)
|
|
|
|
: Colors.transparent;
|
|
|
|
double fillPercentage = entry != null
|
|
|
|
? entry.percentValue / 100
|
|
|
|
: 0; // Assuming percentValue is 0-100
|
|
|
|
|
|
|
|
return GestureDetector(
|
|
|
|
onTap: () {
|
|
|
|
context.go('/entry', extra: date);
|
|
|
|
},
|
|
|
|
child: Container(
|
|
|
|
// ... existing decoration adjusted for opacity ...
|
|
|
|
child: Stack(
|
|
|
|
alignment: Alignment.center,
|
|
|
|
children: [
|
|
|
|
// Inner circle representing diary entry value
|
|
|
|
CircleWidget(
|
|
|
|
radius: 25 * fillPercentage, // Adjust radius based on value
|
|
|
|
color: fillPercentage > 0
|
|
|
|
? currentColorPair.backgroundColor
|
|
|
|
: Colors.grey.withOpacity(opacity)),
|
|
|
|
Text(
|
|
|
|
DateFormat("d").format(date),
|
|
|
|
style: TextStyle(color: Colors.black),
|
|
|
|
),
|
|
|
|
],
|
2023-12-17 23:00:31 +01:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class CircleWidget extends StatelessWidget {
|
|
|
|
final double radius;
|
|
|
|
final Color color;
|
|
|
|
|
|
|
|
CircleWidget({Key? key, required this.radius, required this.color})
|
|
|
|
: super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Container(
|
|
|
|
width: radius * 2,
|
|
|
|
height: radius * 2,
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
shape: BoxShape.circle,
|
|
|
|
color: color,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|