Add calendar task indicators with priority colors and animation
- Fix calendar navigation using ValueKey to force rebuild - Add colored dots on calendar days with tasks (red/orange/green) - Show highest priority of incomplete tasks for each day - Add subtle pulsing animation to task indicators - Load tasks for visible month when calendar opens
This commit is contained in:
parent
864560ef2e
commit
da873afae0
@ -2,6 +2,12 @@ import 'package:flutter/material.dart';
|
|||||||
import 'package:agenda_tasks/l10n/app_localizations.dart';
|
import 'package:agenda_tasks/l10n/app_localizations.dart';
|
||||||
import 'package:go_router/go_router.dart';
|
import 'package:go_router/go_router.dart';
|
||||||
import 'package:intl/intl.dart';
|
import 'package:intl/intl.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
|
import '../../../../core/di/injection_container.dart';
|
||||||
|
import '../../domain/entities/task_entity.dart';
|
||||||
|
import '../../domain/enums/priority.dart';
|
||||||
|
import '../../domain/repositories/task_repository.dart';
|
||||||
|
|
||||||
class CalendarPage extends StatefulWidget {
|
class CalendarPage extends StatefulWidget {
|
||||||
const CalendarPage({super.key});
|
const CalendarPage({super.key});
|
||||||
@ -13,11 +19,60 @@ class CalendarPage extends StatefulWidget {
|
|||||||
class _CalendarPageState extends State<CalendarPage> {
|
class _CalendarPageState extends State<CalendarPage> {
|
||||||
late DateTime _focusedMonth;
|
late DateTime _focusedMonth;
|
||||||
DateTime? _selectedDate;
|
DateTime? _selectedDate;
|
||||||
|
Map<String, List<TaskEntity>> _tasksByDate = {};
|
||||||
|
bool _isLoading = false;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
_focusedMonth = DateTime.now();
|
_focusedMonth = DateTime.now();
|
||||||
|
_loadTasksForMonth();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _loadTasksForMonth() async {
|
||||||
|
setState(() => _isLoading = true);
|
||||||
|
|
||||||
|
final repository = getIt<TaskRepository>();
|
||||||
|
final Map<String, List<TaskEntity>> tasks = {};
|
||||||
|
|
||||||
|
final firstDay = DateTime(_focusedMonth.year, _focusedMonth.month, 1);
|
||||||
|
final lastDay = DateTime(_focusedMonth.year, _focusedMonth.month + 1, 0);
|
||||||
|
|
||||||
|
for (int day = 1; day <= lastDay.day; day++) {
|
||||||
|
final date = DateTime(_focusedMonth.year, _focusedMonth.month, day);
|
||||||
|
final dateStr = DateFormat('yyyy-MM-dd').format(date);
|
||||||
|
final result = await repository.getTasksByDate(date);
|
||||||
|
result.when(
|
||||||
|
success: (data) {
|
||||||
|
if (data.isNotEmpty) {
|
||||||
|
tasks[dateStr] = data;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error: (_) {},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mounted) {
|
||||||
|
setState(() {
|
||||||
|
_tasksByDate = tasks;
|
||||||
|
_isLoading = false;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Priority? _getHighestPriority(List<TaskEntity>? tasks) {
|
||||||
|
if (tasks == null || tasks.isEmpty) return null;
|
||||||
|
|
||||||
|
final incompleteTasks = tasks.where((t) => !t.isDone).toList();
|
||||||
|
if (incompleteTasks.isEmpty) return null;
|
||||||
|
|
||||||
|
if (incompleteTasks.any((t) => t.priority == Priority.high)) {
|
||||||
|
return Priority.high;
|
||||||
|
}
|
||||||
|
if (incompleteTasks.any((t) => t.priority == Priority.medium)) {
|
||||||
|
return Priority.medium;
|
||||||
|
}
|
||||||
|
return Priority.low;
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@ -42,22 +97,28 @@ class _CalendarPageState extends State<CalendarPage> {
|
|||||||
setState(() {
|
setState(() {
|
||||||
_focusedMonth = DateTime(_focusedMonth.year, _focusedMonth.month - 1);
|
_focusedMonth = DateTime(_focusedMonth.year, _focusedMonth.month - 1);
|
||||||
});
|
});
|
||||||
|
_loadTasksForMonth();
|
||||||
},
|
},
|
||||||
onNext: () {
|
onNext: () {
|
||||||
setState(() {
|
setState(() {
|
||||||
_focusedMonth = DateTime(_focusedMonth.year, _focusedMonth.month + 1);
|
_focusedMonth = DateTime(_focusedMonth.year, _focusedMonth.month + 1);
|
||||||
});
|
});
|
||||||
|
_loadTasksForMonth();
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
_WeekdayHeaders(locale: locale),
|
_WeekdayHeaders(locale: locale),
|
||||||
Expanded(
|
Expanded(
|
||||||
child: _CalendarGrid(
|
child: _isLoading
|
||||||
focusedMonth: _focusedMonth,
|
? const Center(child: CircularProgressIndicator())
|
||||||
selectedDate: _selectedDate,
|
: _CalendarGrid(
|
||||||
onDateSelected: (date) {
|
focusedMonth: _focusedMonth,
|
||||||
setState(() => _selectedDate = date);
|
selectedDate: _selectedDate,
|
||||||
},
|
tasksByDate: _tasksByDate,
|
||||||
),
|
getHighestPriority: _getHighestPriority,
|
||||||
|
onDateSelected: (date) {
|
||||||
|
setState(() => _selectedDate = date);
|
||||||
|
},
|
||||||
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
@ -154,11 +215,15 @@ class _WeekdayHeaders extends StatelessWidget {
|
|||||||
class _CalendarGrid extends StatelessWidget {
|
class _CalendarGrid extends StatelessWidget {
|
||||||
final DateTime focusedMonth;
|
final DateTime focusedMonth;
|
||||||
final DateTime? selectedDate;
|
final DateTime? selectedDate;
|
||||||
|
final Map<String, List<TaskEntity>> tasksByDate;
|
||||||
|
final Priority? Function(List<TaskEntity>?) getHighestPriority;
|
||||||
final ValueChanged<DateTime> onDateSelected;
|
final ValueChanged<DateTime> onDateSelected;
|
||||||
|
|
||||||
const _CalendarGrid({
|
const _CalendarGrid({
|
||||||
required this.focusedMonth,
|
required this.focusedMonth,
|
||||||
required this.selectedDate,
|
required this.selectedDate,
|
||||||
|
required this.tasksByDate,
|
||||||
|
required this.getHighestPriority,
|
||||||
required this.onDateSelected,
|
required this.onDateSelected,
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -185,6 +250,7 @@ class _CalendarGrid extends StatelessWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final date = DateTime(focusedMonth.year, focusedMonth.month, dayOffset + 1);
|
final date = DateTime(focusedMonth.year, focusedMonth.month, dayOffset + 1);
|
||||||
|
final dateStr = DateFormat('yyyy-MM-dd').format(date);
|
||||||
final isToday = date.year == today.year &&
|
final isToday = date.year == today.year &&
|
||||||
date.month == today.month &&
|
date.month == today.month &&
|
||||||
date.day == today.day;
|
date.day == today.day;
|
||||||
@ -193,6 +259,9 @@ class _CalendarGrid extends StatelessWidget {
|
|||||||
date.month == selectedDate!.month &&
|
date.month == selectedDate!.month &&
|
||||||
date.day == selectedDate!.day;
|
date.day == selectedDate!.day;
|
||||||
|
|
||||||
|
final tasks = tasksByDate[dateStr];
|
||||||
|
final highestPriority = getHighestPriority(tasks);
|
||||||
|
|
||||||
return GestureDetector(
|
return GestureDetector(
|
||||||
onTap: () => onDateSelected(date),
|
onTap: () => onDateSelected(date),
|
||||||
child: Container(
|
child: Container(
|
||||||
@ -205,18 +274,98 @@ class _CalendarGrid extends StatelessWidget {
|
|||||||
: null,
|
: null,
|
||||||
borderRadius: BorderRadius.circular(8),
|
borderRadius: BorderRadius.circular(8),
|
||||||
),
|
),
|
||||||
child: Center(
|
child: Stack(
|
||||||
child: Text(
|
alignment: Alignment.center,
|
||||||
'${dayOffset + 1}',
|
children: [
|
||||||
style: TextStyle(
|
Text(
|
||||||
color: isSelected
|
'${dayOffset + 1}',
|
||||||
? Theme.of(context).colorScheme.onPrimary
|
style: TextStyle(
|
||||||
: isToday
|
color: isSelected
|
||||||
? Theme.of(context).colorScheme.onPrimaryContainer
|
? Theme.of(context).colorScheme.onPrimary
|
||||||
: null,
|
: isToday
|
||||||
fontWeight: isToday || isSelected ? FontWeight.bold : null,
|
? Theme.of(context).colorScheme.onPrimaryContainer
|
||||||
|
: null,
|
||||||
|
fontWeight: isToday || isSelected ? FontWeight.bold : null,
|
||||||
|
),
|
||||||
),
|
),
|
||||||
),
|
if (highestPriority != null)
|
||||||
|
Positioned(
|
||||||
|
bottom: 4,
|
||||||
|
child: _PulsingDot(priority: highestPriority),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _PulsingDot extends StatefulWidget {
|
||||||
|
final Priority priority;
|
||||||
|
|
||||||
|
const _PulsingDot({required this.priority});
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<_PulsingDot> createState() => _PulsingDotState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _PulsingDotState extends State<_PulsingDot>
|
||||||
|
with SingleTickerProviderStateMixin {
|
||||||
|
late AnimationController _controller;
|
||||||
|
late Animation<double> _animation;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
_controller = AnimationController(
|
||||||
|
duration: const Duration(milliseconds: 1500),
|
||||||
|
vsync: this,
|
||||||
|
)..repeat(reverse: true);
|
||||||
|
|
||||||
|
_animation = Tween<double>(begin: 0.6, end: 1.0).animate(
|
||||||
|
CurvedAnimation(parent: _controller, curve: Curves.easeInOut),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dispose() {
|
||||||
|
_controller.dispose();
|
||||||
|
super.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
Color _getPriorityColor() {
|
||||||
|
switch (widget.priority) {
|
||||||
|
case Priority.high:
|
||||||
|
return Colors.red.shade600;
|
||||||
|
case Priority.medium:
|
||||||
|
return Colors.orange.shade600;
|
||||||
|
case Priority.low:
|
||||||
|
return Colors.green.shade600;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return AnimatedBuilder(
|
||||||
|
animation: _animation,
|
||||||
|
builder: (context, child) {
|
||||||
|
return Opacity(
|
||||||
|
opacity: _animation.value,
|
||||||
|
child: Container(
|
||||||
|
width: 6,
|
||||||
|
height: 6,
|
||||||
|
decoration: BoxDecoration(
|
||||||
|
color: _getPriorityColor(),
|
||||||
|
shape: BoxShape.circle,
|
||||||
|
boxShadow: [
|
||||||
|
BoxShadow(
|
||||||
|
color: _getPriorityColor().withOpacity(0.5),
|
||||||
|
blurRadius: 2,
|
||||||
|
spreadRadius: 1,
|
||||||
|
),
|
||||||
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|||||||
@ -16,7 +16,9 @@ class DailyAgendaPage extends StatelessWidget {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
// Use key to force rebuild when date changes
|
||||||
return ChangeNotifierProvider(
|
return ChangeNotifierProvider(
|
||||||
|
key: ValueKey(initialDate),
|
||||||
create: (_) {
|
create: (_) {
|
||||||
final vm = getIt<DailyTasksViewModel>();
|
final vm = getIt<DailyTasksViewModel>();
|
||||||
if (initialDate != null) {
|
if (initialDate != null) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user