Files
bini-google-tv/flutter_app/lib/widgets/calendar_widget.dart
2026-01-24 21:44:13 +09:00

120 lines
4.2 KiB
Dart

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
class CalendarWidget extends StatelessWidget {
const CalendarWidget({super.key});
@override
Widget build(BuildContext context) {
final now = DateTime.now();
final firstDayOfMonth = DateTime(now.year, now.month, 1);
final lastDayOfMonth = DateTime(now.year, now.month + 1, 0);
final daysInMonth = lastDayOfMonth.day;
final startingWeekday = firstDayOfMonth.weekday; // Mon=1, Sun=7
// Simple calendar logic
// We need to pad the beginning with empty slots
// If week starts on Sunday, adjust accordingly. Let's assume Mon start for now or use locale.
// Let's assume standard Sun-Sat or Mon-Sun. Let's go with Sun-Sat for standard calendar view often seen in KR/US.
// DateTime.weekday: Mon=1, Sun=7.
// If we want Sun start: Sun=0, Mon=1...
// Let's adjust so Sunday is first.
int offset = startingWeekday %
7; // If startingWeekday is 7 (Sun), offset is 0. If 1 (Mon), offset is 1.
return Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: Theme.of(context).cardTheme.color,
borderRadius: BorderRadius.circular(16),
),
child: Column(
children: [
// Header
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
DateFormat('MMMM yyyy').format(now),
style: Theme.of(context).textTheme.titleLarge?.copyWith(
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
const Icon(Icons.calendar_today, color: Colors.white54),
],
),
const SizedBox(height: 16),
// Days Header
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: ['S', 'M', 'T', 'W', 'T', 'F', 'S'].map((day) {
return Expanded(
child: Center(
child: Text(
day,
style: const TextStyle(
color: Colors.white54,
fontWeight: FontWeight.bold,
),
),
),
);
}).toList(),
),
const SizedBox(height: 8),
// Days Grid
Expanded(
child: Column(
children: List.generate(6, (row) {
return Expanded(
child: Row(
children: List.generate(7, (col) {
final index = row * 7 + col;
final dayNumber = index - offset + 1;
if (dayNumber < 1 || dayNumber > daysInMonth) {
return const Expanded(child: SizedBox.shrink());
}
final isToday = dayNumber == now.day;
return Expanded(
child: Container(
margin: const EdgeInsets.all(2),
decoration: isToday
? BoxDecoration(
color: Theme.of(context).colorScheme.primary,
shape: BoxShape.circle,
)
: null,
child: Center(
child: FittedBox(
fit: BoxFit.scaleDown,
child: Text(
'$dayNumber',
style: TextStyle(
color: isToday ? Colors.black : Colors.white,
fontWeight: isToday
? FontWeight.bold
: FontWeight.normal,
fontSize: 12,
),
),
),
),
),
);
}),
),
);
}),
),
),
],
),
);
}
}