import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:table_calendar/table_calendar.dart'; import 'package:fast_med_flutter/ui/pages/add_event.dart'; import 'model/event.dart'; // Dias de Citas No Disponibles final Map _holidays = { DateTime(2020, 11, 6): ['No Hay Citas'], DateTime(2020, 11, 3): ['Dia Elecciones'], DateTime(2020,12,25): ['Chrismas'], DateTime(2020,12,31): ['Despedida del anyo nuevo'], DateTime(2021,1,1): ['Anyo nuevo'], }; //Iniciar la Seccion de las Citas void main() => runApp(Calendario()); //Inicio del Area de las Citas Disponibles class Calendario extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Citas Disponibles', theme: ThemeData( primarySwatch: Colors.blue, ), home: MyHomePage(), routes:{ "add_event": (_) => AddEventPage(), }, ); } } class MyHomePage extends StatefulWidget { @override _HomePageState createState() => _HomePageState(); } //Definimos los controladores del Calendario class _HomePageState extends State { CalendarController _controller; Map> _events; List _selectedEvents; TextEditingController _eventController; SharedPreferences prefs; @override void initState() { super.initState(); _controller = CalendarController(); _eventController = TextEditingController(); _events = {}; _selectedEvents = []; final _selectedDay = DateTime.now(); initPrefs(); } initPrefs() async { prefs = await SharedPreferences.getInstance(); setState(() { _events = Map>.from( decodeMap(json.decode(prefs.getString("events") ?? "{}"))); }); } // Definimos las fechas en la area de citas Map> _groupEvents(List events) { Map> data = {}; events.forEach((event) { DateTime date = DateTime(event.eventDate.year, event.eventDate.month, event.eventDate.day, 12); if(data[date] == null) data[date] = []; data[date].add(event); }); return data; } Map encodeMap(Map map) { Map newMap = {}; map.forEach((key, value) { newMap[key.toString()] = map[key]; }); return newMap; } Map decodeMap(Map map) { Map newMap = {}; map.forEach((key, value) { newMap[DateTime.parse(key)] = map[key]; }); return newMap; } //Definimos el estilo del area de las Citas Disponibles. //En este ejemplo, el calendario comenzara mostrandose por meses. @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Citas Disponibles'), ), body: SingleChildScrollView( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ TableCalendar( events: _events, holidays: _holidays, initialCalendarFormat: CalendarFormat.month, calendarStyle: CalendarStyle( canEventMarkersOverflow: true, todayColor: Colors.orange, selectedColor: Theme.of(context).primaryColor, todayStyle: TextStyle( fontWeight: FontWeight.bold, fontSize: 18.0, color: Colors.white)), headerStyle: HeaderStyle( centerHeaderTitle: true, formatButtonDecoration: BoxDecoration( color: Colors.orange, borderRadius: BorderRadius.circular(20.0), ), formatButtonTextStyle: TextStyle(color: Colors.white), formatButtonShowsNext: false, ), startingDayOfWeek: StartingDayOfWeek.monday, builders: CalendarBuilders( selectedDayBuilder: (context, date, events) => Container( margin: const EdgeInsets.all(4.0), alignment: Alignment.center, decoration: BoxDecoration( color: Theme.of(context).primaryColor, borderRadius: BorderRadius.circular(10.0)), child: Text( date.day.toString(), style: TextStyle(color: Colors.white), )), todayDayBuilder: (context, date, events) => Container( margin: const EdgeInsets.all(4.0), alignment: Alignment.center, decoration: BoxDecoration( color: Colors.orange, borderRadius: BorderRadius.circular(10.0)), child: Text( date.day.toString(), style: TextStyle(color: Colors.white), )), ), calendarController: _controller, ), ..._selectedEvents.map((event) => ListTile( title: Text(event), )), ], ), ), floatingActionButton: FloatingActionButton( child: Icon(Icons.add), onPressed: _showAddDialog, ), ); } //Definimos el boton para crear Citas. _showAddDialog() async { await showDialog( context: context, builder: (context) => AlertDialog( content: TextField( controller: _eventController, decoration: InputDecoration( labelText: "Razon para crear su cita", filled: true, fillColor: Colors.white, border: OutlineInputBorder(borderRadius: BorderRadius.circular(10))), ), actions: [ FlatButton( child: Text("Crear Su Cita Ahora"), onPressed: () { if (_eventController.text.isEmpty) return; if (_events[_controller.selectedDay] != null) { _events[_controller.selectedDay] .add(_eventController.text); } else { _events[_controller.selectedDay] = [ _eventController.text ]; } prefs.setString("events", json.encode(encodeMap(_events))); _eventController.clear(); Navigator.pop(context); }, ) ], )); setState(() { _selectedEvents = _events[_controller.selectedDay]; }); } }