Geen omschrijving

calendario.dart 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. import 'package:http/http.dart' as http;
  6. import 'package:fast_med_flutter/model/event.dart';
  7. // Dias de Citas No Disponibles
  8. final Map<DateTime, List> _holidays = {
  9. DateTime(2020, 11, 6): ['No Hay Citas'],
  10. DateTime(2020, 11, 3): ['Dia Elecciones'],
  11. DateTime(2020, 12, 25): ['Christmas'],
  12. DateTime(2020, 12, 31): ['Despedida del anyo nuevo'],
  13. DateTime(2021, 01, 01): ['Anyo Nuevo'],
  14. };
  15. //Iniciar la Seccion de las Citas
  16. void main() async{
  17. //Comunicacion con el servidor antes de iniciar el app
  18. var url = 'https://ada.uprrp.edu/~jorge.lopez19/FastMed/API/InsertEvent.php';
  19. WidgetsFlutterBinding.ensureInitialized();
  20. final data = await http.post(url, body: {
  21. });
  22. runApp(Calendario());
  23. }
  24. //Inicio del Area de las Citas Disponibles
  25. class Calendario extends StatelessWidget {
  26. @override
  27. Widget build(BuildContext context) {
  28. return MaterialApp(
  29. title: 'Citas Disponibles',
  30. theme: ThemeData(
  31. primarySwatch: Colors.blue,
  32. ),
  33. home: MyHomePage(),
  34. routes: {
  35. "add_event": (_) => AddEventPage(),
  36. },
  37. );
  38. }
  39. }
  40. // Define el homepage para el area de citas
  41. class MyHomePage extends StatefulWidget {
  42. @override
  43. _MyHomePageState createState() => _MyHomePageState();
  44. }
  45. class _MyHomePageState extends State<MyHomePage> {
  46. CalendarController _controller;
  47. Map<DateTime, List<dynamic>> _events;
  48. List<dynamic> _selectedEvents;
  49. @override
  50. void initState() {
  51. super.initState();
  52. final _selectedDay = DateTime.now();
  53. _controller = CalendarController();
  54. _events = {
  55. //Ejemplos de Citas Previamente Hechas de Prueba Para los Recordatorios
  56. _selectedDay.add(Duration(days: 3)): Set.from(
  57. [ 'Cita Cardiologo', 'Cita Dentista']).toList(),
  58. _selectedDay.add(Duration(days: 22)): [
  59. 'Cita Cardiologo',
  60. 'Cita Dentista'
  61. ],
  62. };
  63. _selectedEvents = _events[_selectedDay] ?? [];
  64. void _onDaySelected(DateTime day, List events, List holidays) {
  65. print('CALLBACK: _onDaySelected');
  66. setState(() {
  67. _selectedEvents = _events[_selectedDay] ?? [];
  68. });
  69. }
  70. }
  71. Map<String, dynamic> encodeMap(Map<DateTime, dynamic> map) {
  72. Map<String, dynamic> newMap = {};
  73. map.forEach((key, value) {
  74. newMap[key.toString()] = map[key];
  75. });
  76. return newMap;
  77. }
  78. Map<DateTime, List<dynamic>> _groupEvents(List<EventModel> events) {
  79. Map<DateTime, List<dynamic>> data = {};
  80. events.forEach((event) {
  81. DateTime date = DateTime(event.eventDate.year, event.eventDate.month,
  82. event.eventDate.day, 12);
  83. if (data[date] == null) data[date] = [];
  84. data[date].add(event);
  85. });
  86. return data;
  87. }
  88. @override
  89. //Marca en el calendario los dias que no esta disponibles para hacer horarios
  90. void _onDaySelected(DateTime day, List events, List holidays) {
  91. print('CALLBACK: _onDaySelected');
  92. setState(() {
  93. _selectedEvents = events;
  94. });
  95. }
  96. void _onVisibleDaysChanged(DateTime first, DateTime last,
  97. CalendarFormat format) {
  98. print('CALLBACK: _onVisibleDaysChanged');
  99. }
  100. void _onCalendarCreated(DateTime first, DateTime last,
  101. CalendarFormat format) {
  102. print('CALLBACK: _onCalendarCreated');
  103. }
  104. //Definimos los detalles del area de las citas
  105. @override
  106. Widget build(BuildContext context) {
  107. return Scaffold(
  108. appBar: AppBar(
  109. title: Text('Citas Disponibles'),
  110. ),
  111. body: StreamBuilder<List<EventModel>>(
  112. // stream: eventDBS.streamList(),
  113. builder: (context, snapshot) {
  114. if (snapshot.hasData) {
  115. List<EventModel> allEvents = snapshot.data;
  116. if (allEvents.isNotEmpty) {
  117. _events = _groupEvents(allEvents);
  118. }
  119. }
  120. return SingleChildScrollView(
  121. child: Column(
  122. crossAxisAlignment: CrossAxisAlignment.start,
  123. children: <Widget>[
  124. TableCalendar(
  125. events: _events,
  126. holidays: _holidays,
  127. initialCalendarFormat: CalendarFormat.month,
  128. calendarStyle: CalendarStyle(
  129. canEventMarkersOverflow: true,
  130. todayColor: Colors.orange,
  131. selectedColor: Theme
  132. .of(context)
  133. .primaryColor,
  134. todayStyle: TextStyle(
  135. fontWeight: FontWeight.bold,
  136. fontSize: 18.0,
  137. color: Colors.white)),
  138. headerStyle: HeaderStyle(
  139. centerHeaderTitle: true,
  140. formatButtonDecoration: BoxDecoration(
  141. color: Colors.orange,
  142. borderRadius: BorderRadius.circular(20.0),
  143. ),
  144. formatButtonTextStyle: TextStyle(color: Colors.white),
  145. formatButtonShowsNext: false,
  146. ),
  147. startingDayOfWeek: StartingDayOfWeek.monday,
  148. onVisibleDaysChanged: _onVisibleDaysChanged,
  149. onCalendarCreated: _onCalendarCreated,
  150. builders: CalendarBuilders(
  151. selectedDayBuilder: (context, date, events) =>
  152. Container(
  153. margin: const EdgeInsets.all(4.0),
  154. alignment: Alignment.center,
  155. decoration: BoxDecoration(
  156. color: Theme
  157. .of(context)
  158. .primaryColor,
  159. borderRadius: BorderRadius.circular(10.0)),
  160. child: Text(
  161. date.day.toString(),
  162. style: TextStyle(color: Colors.white),
  163. )),
  164. todayDayBuilder: (context, date, events) =>
  165. Container(
  166. margin: const EdgeInsets.all(4.0),
  167. alignment: Alignment.center,
  168. decoration: BoxDecoration(
  169. color: Colors.orange,
  170. borderRadius: BorderRadius.circular(10.0)),
  171. child: Text(
  172. date.day.toString(),
  173. style: TextStyle(color: Colors.white),
  174. )),
  175. ),
  176. calendarController: _controller,
  177. ),
  178. ..._selectedEvents.map((event) =>
  179. ListTile(
  180. title: Text(event.title),
  181. onTap: () {
  182. Navigator.push(
  183. context,
  184. MaterialPageRoute(
  185. builder: (_) =>
  186. EventDetailsPage(
  187. event: event,
  188. )));
  189. },
  190. )),
  191. ],
  192. ),
  193. );
  194. }
  195. ),
  196. floatingActionButton: FloatingActionButton(
  197. child: Icon(Icons.add),
  198. onPressed: () => Navigator.pushNamed(context, 'add_event'),
  199. ),
  200. );
  201. }
  202. }