ModernMemoires/lib/views/statistic/widget/calendar_widget.dart

228 lines
8.1 KiB
Dart

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:intl/intl.dart';
import '../../../utils/definitions/ColorPairs.dart';
import '../../../utils/logic/PreferencesService.dart'; // Correct the path as necessary
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> {
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
@override
void initState() {
super.initState();
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
}
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) {
double addon = dateList.length > 35 ? 50 : 0;
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,
),
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)];
if (entry != null) {
print(entry);
print(entry!.date.toString());
print(entry!.percentValue);
print(entry!.texts);
}
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,
),
),
),
],
),
);
});
}
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),
),
],
),
),
);
}
}
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,
),
);
}
}