Browse Source

Sprint 2 Complete

JorgeIan 3 years ago
parent
commit
d7eed94568
2 changed files with 148 additions and 137 deletions
  1. 148
    109
      fast_med_flutter/lib/routes/calendario.dart
  2. 0
    28
      fast_med_flutter/lib/ui/pages/view_event.dart

+ 148
- 109
fast_med_flutter/lib/routes/calendario.dart View File

@@ -1,30 +1,25 @@
1
-import 'package:flutter/material.dart';
2
-import 'package:fast_med_flutter/ui/pages/add_event.dart';
3
-import 'package:fast_med_flutter/ui/pages/view_event.dart';
4
-import 'package:table_calendar/table_calendar.dart';
5 1
 import 'dart:convert';
2
+import 'package:flutter/material.dart';
6 3
 import 'package:shared_preferences/shared_preferences.dart';
7
-//import 'package:firebase_core/firebase_core.dart';
8
-import 'dart:async';
9
-import 'package:http/http.dart' as http;
10
-
4
+import 'package:table_calendar/table_calendar.dart';
5
+import 'package:fast_med_flutter/ui/pages/add_event.dart';
11 6
 import 'model/event.dart';
12 7
 
13
-void main() async{
14
-
15
-  var url = 'https://ada.uprrp.edu/~jorge.lopez19/FastMed/API/InsertEvent.php';
16
-  WidgetsFlutterBinding.ensureInitialized();
17
-  //==final data = await http.post(url, body: {
18
-    //"name": name.text,
19
-    //"reason": reason.text,
20
-    //"date": date.text,
21
-    //"time": time.text,
22
- // }
23
-  //);
24
-  runApp(Calendario());
25
-}
8
+// Dias de Citas No Disponibles
9
+
10
+final Map<DateTime, List> _holidays = {
11
+DateTime(2020, 11, 6): ['No Hay Citas'],
12
+DateTime(2020, 11, 3): ['Dia Elecciones'],
13
+DateTime(2020,12,25): ['Chrismas'],
14
+DateTime(2020,12,31): ['Despedida del anyo nuevo'],
15
+  DateTime(2021,1,1): ['Anyo nuevo'],
16
+};
26 17
 
18
+//Iniciar la Seccion de las Citas
27 19
 
20
+void main() => runApp(Calendario());
21
+
22
+//Inicio del Area de las Citas Disponibles
28 23
 
29 24
 class Calendario extends StatelessWidget {
30 25
   @override
@@ -35,32 +30,48 @@ class Calendario extends StatelessWidget {
35 30
         primarySwatch: Colors.blue,
36 31
       ),
37 32
       home: MyHomePage(),
38
-      routes: {
33
+      routes:{
39 34
         "add_event": (_) => AddEventPage(),
40 35
       },
41 36
     );
42 37
   }
43 38
 }
44 39
 
45
-
46
-
47 40
 class MyHomePage extends StatefulWidget {
48 41
   @override
49
-  _MyHomePageState createState() => _MyHomePageState();
42
+  _HomePageState createState() => _HomePageState();
50 43
 }
51 44
 
52
-class _MyHomePageState extends State<MyHomePage> {
45
+//Definimos los controladores del Calendario
46
+
47
+class _HomePageState extends State<MyHomePage> {
53 48
   CalendarController _controller;
54 49
   Map<DateTime, List<dynamic>> _events;
55 50
   List<dynamic> _selectedEvents;
51
+  TextEditingController _eventController;
52
+  SharedPreferences prefs;
53
+
54
+
56 55
 
57 56
   @override
58 57
   void initState() {
59 58
     super.initState();
60 59
     _controller = CalendarController();
60
+    _eventController = TextEditingController();
61 61
     _events = {};
62 62
     _selectedEvents = [];
63
+    final _selectedDay = DateTime.now();
64
+    initPrefs();
65
+  }
66
+
67
+  initPrefs() async {
68
+    prefs = await SharedPreferences.getInstance();
69
+    setState(() {
70
+      _events = Map<DateTime, List<dynamic>>.from(
71
+          decodeMap(json.decode(prefs.getString("events") ?? "{}")));
72
+    });
63 73
   }
74
+  // Definimos las fechas en la area de citas
64 75
 
65 76
   Map<DateTime, List<dynamic>> _groupEvents(List<EventModel> events) {
66 77
     Map<DateTime,List<dynamic>> data = {};
@@ -73,103 +84,131 @@ class _MyHomePageState extends State<MyHomePage> {
73 84
     return data;
74 85
   }
75 86
 
87
+  Map<String, dynamic> encodeMap(Map<DateTime, dynamic> map) {
88
+    Map<String, dynamic> newMap = {};
89
+    map.forEach((key, value) {
90
+      newMap[key.toString()] = map[key];
91
+    });
92
+    return newMap;
93
+  }
94
+
95
+  Map<DateTime, dynamic> decodeMap(Map<String, dynamic> map) {
96
+    Map<DateTime, dynamic> newMap = {};
97
+    map.forEach((key, value) {
98
+      newMap[DateTime.parse(key)] = map[key];
99
+    });
100
+    return newMap;
101
+  }
76 102
 
77 103
 
104
+//Definimos el estilo del area de las Citas Disponibles.
105
+  //En este ejemplo, el calendario comenzara mostrandose por meses.
78 106
   @override
79 107
   Widget build(BuildContext context) {
80 108
     return Scaffold(
81 109
       appBar: AppBar(
82 110
         title: Text('Citas Disponibles'),
83 111
       ),
84
-      body: StreamBuilder<List<EventModel>>(
85
-        // stream: eventDBS.streamList(),
86
-          builder: (context, snapshot) {
87
-            if (snapshot.hasData) {
88
-              List<EventModel> allEvents = snapshot.data;
89
-              if (allEvents.isNotEmpty) {
90
-                _events = _groupEvents(allEvents);
91
-              }
92
-            }
93
-
94
-            return SingleChildScrollView(
95
-              child: Column(
96
-                crossAxisAlignment: CrossAxisAlignment.start,
97
-                children: <Widget>[
98
-                  TableCalendar(
99
-                    events: _events,
100
-                    initialCalendarFormat: CalendarFormat.month,
101
-                    calendarStyle: CalendarStyle(
102
-                        canEventMarkersOverflow: true,
103
-                        todayColor: Colors.orange,
104
-                        selectedColor: Theme
105
-                            .of(context)
106
-                            .primaryColor,
107
-                        todayStyle: TextStyle(
108
-                            fontWeight: FontWeight.bold,
109
-                            fontSize: 18.0,
110
-                            color: Colors.white)),
111
-                    headerStyle: HeaderStyle(
112
-                      centerHeaderTitle: true,
113
-                      formatButtonDecoration: BoxDecoration(
112
+      body: SingleChildScrollView(
113
+        child: Column(
114
+          crossAxisAlignment: CrossAxisAlignment.start,
115
+          children: <Widget>[
116
+            TableCalendar(
117
+              events: _events,
118
+              holidays: _holidays,
119
+              initialCalendarFormat: CalendarFormat.month,
120
+              calendarStyle: CalendarStyle(
121
+                  canEventMarkersOverflow: true,
122
+                  todayColor: Colors.orange,
123
+                  selectedColor: Theme.of(context).primaryColor,
124
+                  todayStyle: TextStyle(
125
+                      fontWeight: FontWeight.bold,
126
+                      fontSize: 18.0,
127
+                      color: Colors.white)),
128
+              headerStyle: HeaderStyle(
129
+                centerHeaderTitle: true,
130
+                formatButtonDecoration: BoxDecoration(
131
+                  color: Colors.orange,
132
+                  borderRadius: BorderRadius.circular(20.0),
133
+                ),
134
+                formatButtonTextStyle: TextStyle(color: Colors.white),
135
+                formatButtonShowsNext: false,
136
+              ),
137
+              startingDayOfWeek: StartingDayOfWeek.monday,
138
+
139
+              builders: CalendarBuilders(
140
+                selectedDayBuilder: (context, date, events) => Container(
141
+                    margin: const EdgeInsets.all(4.0),
142
+                    alignment: Alignment.center,
143
+                    decoration: BoxDecoration(
144
+                        color: Theme.of(context).primaryColor,
145
+                        borderRadius: BorderRadius.circular(10.0)),
146
+                    child: Text(
147
+                      date.day.toString(),
148
+                      style: TextStyle(color: Colors.white),
149
+                    )),
150
+                todayDayBuilder: (context, date, events) => Container(
151
+                    margin: const EdgeInsets.all(4.0),
152
+                    alignment: Alignment.center,
153
+                    decoration: BoxDecoration(
114 154
                         color: Colors.orange,
115
-                        borderRadius: BorderRadius.circular(20.0),
116
-                      ),
117
-                      formatButtonTextStyle: TextStyle(color: Colors.white),
118
-                      formatButtonShowsNext: false,
119
-                    ),
120
-                    startingDayOfWeek: StartingDayOfWeek.monday,
121
-
122
-                    builders: CalendarBuilders(
123
-                      selectedDayBuilder: (context, date, events) =>
124
-                          Container(
125
-                              margin: const EdgeInsets.all(4.0),
126
-                              alignment: Alignment.center,
127
-                              decoration: BoxDecoration(
128
-                                  color: Theme
129
-                                      .of(context)
130
-                                      .primaryColor,
131
-                                  borderRadius: BorderRadius.circular(10.0)),
132
-                              child: Text(
133
-                                date.day.toString(),
134
-                                style: TextStyle(color: Colors.white),
135
-                              )),
136
-                      todayDayBuilder: (context, date, events) =>
137
-                          Container(
138
-                              margin: const EdgeInsets.all(4.0),
139
-                              alignment: Alignment.center,
140
-                              decoration: BoxDecoration(
141
-                                  color: Colors.orange,
142
-                                  borderRadius: BorderRadius.circular(10.0)),
143
-                              child: Text(
144
-                                date.day.toString(),
145
-                                style: TextStyle(color: Colors.white),
146
-                              )),
147
-                    ),
148
-                    calendarController: _controller,
149
-                  ),
150
-                  ..._selectedEvents.map((event) =>
151
-                      ListTile(
152
-                        title: Text(event.title),
153
-                        onTap: () {
154
-                          Navigator.push(
155
-                              context,
156
-                              MaterialPageRoute(
157
-                                  builder: (_) =>
158
-                                      EventDetailsPage(
159
-                                        event: event,
160
-                                      )));
161
-                        },
162
-                      )),
163
-                ],
155
+                        borderRadius: BorderRadius.circular(10.0)),
156
+                    child: Text(
157
+                      date.day.toString(),
158
+                      style: TextStyle(color: Colors.white),
159
+                    )),
164 160
               ),
165
-            );
166
-          }
161
+              calendarController: _controller,
162
+            ),
163
+            ..._selectedEvents.map((event) => ListTile(
164
+              title: Text(event),
165
+            )),
166
+          ],
167
+        ),
167 168
       ),
168 169
       floatingActionButton: FloatingActionButton(
169 170
         child: Icon(Icons.add),
170
-        onPressed: () => Navigator.pushNamed(context, 'add_event'),
171
+        onPressed: _showAddDialog,
171 172
       ),
172 173
     );
173 174
   }
174
-}
175 175
 
176
+  //Definimos el boton para crear Citas.
177
+
178
+  _showAddDialog() async {
179
+    await showDialog(
180
+        context: context,
181
+        builder: (context) => AlertDialog(
182
+          content: TextField(
183
+            controller: _eventController,
184
+            decoration: InputDecoration(
185
+                labelText: "Razon para crear su cita",
186
+                filled: true,
187
+                fillColor: Colors.white,
188
+                border: OutlineInputBorder(borderRadius: BorderRadius.circular(10))),
189
+          ),
190
+          actions: <Widget>[
191
+            FlatButton(
192
+              child: Text("Crear Su Cita Ahora"),
193
+              onPressed: () {
194
+                if (_eventController.text.isEmpty) return;
195
+                if (_events[_controller.selectedDay] != null) {
196
+                  _events[_controller.selectedDay]
197
+                      .add(_eventController.text);
198
+                } else {
199
+                  _events[_controller.selectedDay] = [
200
+                    _eventController.text
201
+                  ];
202
+                }
203
+                prefs.setString("events", json.encode(encodeMap(_events)));
204
+                _eventController.clear();
205
+                Navigator.pop(context);
206
+              },
207
+            )
208
+          ],
209
+        ));
210
+    setState(() {
211
+      _selectedEvents = _events[_controller.selectedDay];
212
+    });
213
+  }
214
+}

+ 0
- 28
fast_med_flutter/lib/ui/pages/view_event.dart View File

@@ -1,28 +0,0 @@
1
-import 'package:flutter/material.dart';
2
-import 'package:fast_med_flutter/model/event.dart';
3
-
4
-class EventDetailsPage extends StatelessWidget {
5
-  final EventModel event;
6
-
7
-  const EventDetailsPage({Key key, this.event}) : super(key: key);
8
-
9
-  @override
10
-  Widget build(BuildContext context){
11
-    return Scaffold(
12
-      appBar: AppBar(
13
-        title: Text('Note details'),
14
-      ),
15
-      body: SingleChildScrollView(
16
-        padding: const EdgeInsets.all(16.0),
17
-        child: Column(
18
-          crossAxisAlignment: CrossAxisAlignment.start,
19
-          children: <Widget>[
20
-            Text(event.title, style: Theme.of(context).textTheme.display1,),
21
-            SizedBox(height: 20.0),
22
-            Text(event.description)
23
-          ],
24
-        ),
25
-      ),
26
-    );
27
-  }
28
-}