123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- 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<DateTime, List> _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<MyHomePage> {
- CalendarController _controller;
- Map<DateTime, List<dynamic>> _events;
- List<dynamic> _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<DateTime, List<dynamic>>.from(
- decodeMap(json.decode(prefs.getString("events") ?? "{}")));
- });
- }
- // Definimos las fechas en la area de citas
-
- Map<DateTime, List<dynamic>> _groupEvents(List<EventModel> events) {
- Map<DateTime,List<dynamic>> 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<String, dynamic> encodeMap(Map<DateTime, dynamic> map) {
- Map<String, dynamic> newMap = {};
- map.forEach((key, value) {
- newMap[key.toString()] = map[key];
- });
- return newMap;
- }
-
- Map<DateTime, dynamic> decodeMap(Map<String, dynamic> map) {
- Map<DateTime, dynamic> 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: <Widget>[
- 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: <Widget>[
- 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];
- });
- }
- }
|