import 'package:fast_med_flutter/model/event.dart'; import 'package:flutter/material.dart'; import 'package:fast_med_flutter/res/event_firestore_service.dart'; import 'package:http/http.dart' as http; import 'dart:async'; class AddEventPage extends StatefulWidget { final EventModel note; const AddEventPage({Key key, this.note}) : super(key: key); @override _AddEventPageState createState() => _AddEventPageState(); } //Controles de cada Evento que usaremos en todos los files class _AddEventPageState extends State { TextStyle style = TextStyle(fontFamily: 'Montserrat', fontSize: 20.0); TextEditingController _title; TextEditingController _description; TextEditingController _number; TextEditingController _name; TextEditingController _reason; DateTime _eventDate; DateTime _eventTime; DateTime selectedDate = DateTime.now(); List data; List holidays; final _formKey = GlobalKey(); final _key = GlobalKey(); bool processing; @override void initState() { super.initState(); _title = TextEditingController(text: widget.note != null ? widget.note.title : ""); _description = TextEditingController(text: widget.note != null ? widget.note.description : ""); _name = TextEditingController(text: widget.note != null ? widget.note.name : ""); _number = TextEditingController(text: widget.note != null ? widget.note.number : ""); _reason = TextEditingController(text: widget.note != null ? widget.note.reason : ""); _eventDate = DateTime.now(); _eventTime = DateTime.now(); processing = false; } // Definimos el Formulario con cada parametro para crear la cita @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.note != null ? "Edit Note" : "Crear Su Cita"), ), key: _key, body: Form( key: _formKey, child: Container( alignment: Alignment.center, child: ListView( children: [ // Area donde paciente anota su nombre. Si esta vacio no lo acepta. Padding( padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0), child: TextFormField( controller: _name, validator: (value) => (value.isEmpty) ? "Favor Anotar Nombre del Paciente" : null, style: style, decoration: InputDecoration( labelText: "Nombre completo del paciente", filled: true, fillColor: Colors.white, border: OutlineInputBorder(borderRadius: BorderRadius.circular(10))), ), ), //Area donde el paciente debe anotar su numero de telefono. Si esta vacio no lo acepta. Padding( padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0), child: TextFormField( controller: _number, validator: (value) => (value.isEmpty) ? "Favor Anotar Numero de Telefono" : null, style: style, decoration: InputDecoration( labelText: "Numero de Telefono", border: OutlineInputBorder(borderRadius: BorderRadius.circular(10))), ), ), //Area donde el paciente explica la razon de su cita medica. Si esta vacio no lo acepta Padding( padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0), child: TextFormField( controller: _reason, minLines: 3, maxLines: 5, validator: (value) => (value.isEmpty) ? "Favor Explicar Razon Para La Cita" : null, style: style, decoration: InputDecoration( labelText: "Razones para su cita", border: OutlineInputBorder(borderRadius: BorderRadius.circular(10))), ), ), const SizedBox(height: 10.0), //Area donde el paciente puede cambiar la fecha para la cita ListTile( title: Text("Date (YYYY-MM-DD)"), subtitle: Text("${_eventDate.year} - ${_eventDate.month} - ${_eventDate.day}"), onTap: ()async{ DateTime picked = await showDatePicker(context: context, initialDate: _eventDate, firstDate: DateTime(_eventDate.year-5), lastDate: DateTime(_eventDate.year+5)); if(picked != null) { setState(() { _eventDate = picked; }); } }, ), //Area donde el paciente puede elegir y cambiar la hora para la cita ListTile( title: Text("Time (HH: mm)"), subtitle: Text("${_eventDate.hour} - ${_eventDate.minute} - ${_eventDate.second}"), onTap: ()async{ final selectedTime = await _selectTime(context); if (selectedTime == null) return; print(selectedTime); setState(() { this.selectedDate = DateTime( selectedTime.hour, selectedTime.minute, ); }); } ), SizedBox(height: 10.0), //Se define el envio de los parametros al usar el boton de Crear la Cita processing ? Center(child: CircularProgressIndicator()) : Padding( padding: const EdgeInsets.symmetric(horizontal: 16.0), child: Material( elevation: 5.0, borderRadius: BorderRadius.circular(30.0), color: Theme.of(context).primaryColor, child: MaterialButton( onPressed: () async { if (_formKey.currentState.validate()) { setState(() { processing = true; }); if(widget.note != null) { await eventDBS.updateData(widget.note.id,{ "title": _title.text, "number": _number.text, "reason": _reason.text, "event_date": widget.note.eventDate, "time": widget.note.eventTime, }); }else{ await eventDBS.createItem(EventModel( title: _title.text, description: _description.text, name: _name.text, number: _number.text, reason: _reason.text, eventDate: _eventDate )); } Navigator.pop(context); setState(() { processing = false; }); } }, child: Text( "Crear Su Cita Ahora", style: style.copyWith( color: Colors.white, fontWeight: FontWeight.bold), ), ), ), ), ], ), ), ), ); } @override void dispose() { _title.dispose(); _description.dispose(); _name.dispose(); _number.dispose(); _reason.dispose(); super.dispose(); } } // Funcionalidades para poder seleccionar horarios Future _selectTime(BuildContext context) { final now = DateTime.now(); return showTimePicker( context: context, initialTime: TimeOfDay(hour: now.hour, minute: now.minute), ); }