summaryrefslogtreecommitdiff
path: root/lib/screens/events_screen.dart
blob: 9183e5e404e93c41a854b4579b4c6ffe8acf549c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import 'package:flutter/material.dart';
import 'package:pmsna1/utils.dart';
import 'package:provider/provider.dart';
import 'package:table_calendar/table_calendar.dart';

import '../models/event.dart';
import '../providers/events_provider.dart';
import '../widgets/event_list.dart';

enum Vista { calendario, lista }

class EventsScreen extends StatefulWidget {
  const EventsScreen({super.key});

  @override
  State<EventsScreen> createState() => _EventsScreenState();
}

class _EventsScreenState extends State<EventsScreen> {
  Vista vista = Vista.calendario;
  DateTime _selectedDay = DateTime.now();
  DateTime _focusedDay = DateTime.now();
  CalendarFormat _calendarFormat = CalendarFormat.month;

  Color? getEventColor(Event e) {
    DateTime today = DateTime.now();
    if (daysBetween(today, e.date) > 0 && daysBetween(today, e.date) <= 2) {
      // 7. Cuando el evento esté a 2 días de realizarse, la
      // celda del calendario deberá pintarse de color amarillo
      return Colors.yellow;
    } else if (isSameDay(e.date, today)) {
      // 8. Cuando el evento sea el día en curso cambiará a un
      // color verde
      return Colors.green;
    } else if (today.isAfter(e.date) && !e.completed) {
      // 9. Cuando el evento ya haya pasado y no se haya
      // completado, entonces se marcará en rojo
      return Colors.red;
    } else if (today.isAfter(e.date) && e.completed) {
      // 10. Si el evento ya pasó y está como completado se
      // dejará en verde
      return Colors.green;
    }
    return null;
  }

  @override
  Widget build(BuildContext context) {
    EventsProvider provider = Provider.of<EventsProvider>(context);

    return Scaffold(
      appBar: AppBar(
        title: const Text('Eventos'),
        actions: [
          SegmentedButton<Vista>(
            showSelectedIcon: false,
            segments: const [
              ButtonSegment<Vista>(
                value: Vista.calendario,
                icon: Icon(Icons.calendar_month),
              ),
              ButtonSegment<Vista>(
                value: Vista.lista,
                icon: Icon(Icons.list),
              ),
            ],
            selected: <Vista>{vista},
            onSelectionChanged: (p0) {
              setState(() {
                vista = p0.first;
              });
            },
          )
        ],
      ),
      body: Container(
          child: vista == Vista.calendario
              ? SingleChildScrollView(
                  child: TableCalendar(
                    firstDay: DateTime.fromMicrosecondsSinceEpoch(0),
                    lastDay: DateTime.utc(9000, 12, 31),
                    focusedDay: _focusedDay,
                    selectedDayPredicate: (day) {
                      return isSameDay(_selectedDay, day);
                    },
                    onDaySelected: (selectedDay, focusedDay) {
                      setState(() {
                        _selectedDay = selectedDay;
                        _focusedDay = focusedDay;
                      });
                    },
                    calendarFormat: _calendarFormat,
                    onFormatChanged: (format) {
                      setState(() {
                        _calendarFormat = format;
                      });
                    },
                    eventLoader: (day) => provider.getEventsForDay(day),
                    // Source: https://stackoverflow.com/a/69036998
                    calendarBuilders: CalendarBuilders(
                      singleMarkerBuilder: (context, day, event) {
                        if (event == null) return null;
                        Event e = event as Event;
                        Color color = getEventColor(e) ??
                            Theme.of(context).colorScheme.onSurface;
                        return Container(
                          decoration: BoxDecoration(
                            shape: BoxShape.circle,
                            color: color,
                          ),
                          width: 5.0,
                          height: 5.0,
                          margin: const EdgeInsets.symmetric(horizontal: 1.5),
                        );
                      },
                    ),
                  ),
                )
              : const EventList()),
      floatingActionButton: FloatingActionButton.extended(
        label: const Text('Nuevo'),
        icon: const Icon(Icons.add),
        onPressed: () {
          Navigator.of(context).pushNamed('/newevent');
        },
      ),
    );
  }
}