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

138 lines
4.0 KiB
Dart

import 'dart:math';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:intl/intl.dart';
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> {
late List<DateTime> dateList;
@override
void initState() {
super.initState();
dateList = _generateDateList(widget.currentDate);
}
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) {
print(dateList.length);
double addon = dateList.length > 35 ? 50 : 0;
return Container(
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
color: Colors.black.withOpacity(0.3),
style: BorderStyle.solid,
width: 2))),
height: 300 + addon,
child: Padding(
padding: const EdgeInsets.fromLTRB(25, 0, 25, 0),
child: GridView.builder(
physics:
NeverScrollableScrollPhysics(), // Disable scrolling for GridView
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 7,
),
itemBuilder: (context, index) {
DateTime date = dateList[index];
bool isCurrentMonth = date.month == widget.currentDate.month;
double outerRadius = 25; // Fixed outer radius
double innerRadius = Random().nextDouble() * outerRadius;
double opacity = isCurrentMonth ? 1.0 : 0.15;
return Center(
child: GestureDetector(
onTap: () => {
context.go('/entry', extra: date)
}, // Using the callback here
child: Container(
width: outerRadius * 2,
height: outerRadius * 2,
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(color: Colors.black, width: 2),
color: isCurrentMonth
? Colors.transparent
: Colors.grey.withOpacity(opacity),
),
child: Stack(
alignment: Alignment.center,
children: [
CircleWidget(
radius: innerRadius,
color: Colors.lightGreenAccent.withOpacity(0.75)),
Text(
DateFormat("d").format(date),
style: TextStyle(color: Colors.black),
),
],
),
),
),
);
},
itemCount: dateList.length,
),
),
);
}
}
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,
),
);
}
}