Açıklama Yok

calendario.dart 6.6KB

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