import 'package:flutter/material.dart'; import 'package:fast_med_flutter/ui/pages/add_event.dart'; import 'package:fast_med_flutter/ui/pages/view_event.dart'; import 'package:table_calendar/table_calendar.dart'; import 'package:http/http.dart' as http; import 'package:fast_med_flutter/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): ['Christmas'], DateTime(2020, 12, 31): ['Despedida del anyo nuevo'], DateTime(2021, 01, 01): ['Anyo Nuevo'], }; //Iniciar la Seccion de las Citas void main() async{ //Comunicacion con el servidor antes de iniciar el app var url = 'https://ada.uprrp.edu/~jorge.lopez19/FastMed/API/InsertEvent.php'; WidgetsFlutterBinding.ensureInitialized(); final data = await http.post(url, body: { }); 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(), }, ); } } // Define el homepage para el area de citas class MyHomePage extends StatefulWidget { @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State { CalendarController _controller; Map> _events; List _selectedEvents; @override void initState() { super.initState(); final _selectedDay = DateTime.now(); _controller = CalendarController(); _events = { //Ejemplos de Citas Previamente Hechas de Prueba Para los Recordatorios _selectedDay.add(Duration(days: 3)): Set.from( [ 'Cita Cardiologo', 'Cita Dentista']).toList(), _selectedDay.add(Duration(days: 22)): [ 'Cita Cardiologo', 'Cita Dentista' ], }; _selectedEvents = _events[_selectedDay] ?? []; void _onDaySelected(DateTime day, List events, List holidays) { print('CALLBACK: _onDaySelected'); setState(() { _selectedEvents = _events[_selectedDay] ?? []; }); } } Map encodeMap(Map map) { Map newMap = {}; map.forEach((key, value) { newMap[key.toString()] = map[key]; }); return newMap; } 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; } @override //Marca en el calendario los dias que no esta disponibles para hacer horarios void _onDaySelected(DateTime day, List events, List holidays) { print('CALLBACK: _onDaySelected'); setState(() { _selectedEvents = events; }); } void _onVisibleDaysChanged(DateTime first, DateTime last, CalendarFormat format) { print('CALLBACK: _onVisibleDaysChanged'); } void _onCalendarCreated(DateTime first, DateTime last, CalendarFormat format) { print('CALLBACK: _onCalendarCreated'); } //Definimos los detalles del area de las citas @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Citas Disponibles'), ), body: StreamBuilder>( // stream: eventDBS.streamList(), builder: (context, snapshot) { if (snapshot.hasData) { List allEvents = snapshot.data; if (allEvents.isNotEmpty) { _events = _groupEvents(allEvents); } } return 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, onVisibleDaysChanged: _onVisibleDaysChanged, onCalendarCreated: _onCalendarCreated, 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.title), onTap: () { Navigator.push( context, MaterialPageRoute( builder: (_) => EventDetailsPage( event: event, ))); }, )), ], ), ); } ), floatingActionButton: FloatingActionButton( child: Icon(Icons.add), onPressed: () => Navigator.pushNamed(context, 'add_event'), ), ); } }