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'; import 'dart:convert'; class AddEventPage extends StatefulWidget { final EventModel note; const AddEventPage({Key key, this.note}) : super(key: key); @override _AddEventPageState createState() => _AddEventPageState(); } class _AddEventPageState extends State { TextStyle style = TextStyle(fontFamily: 'Montserrat', fontSize: 20.0); TextEditingController _title; TextEditingController _description; TextEditingController _number; TextEditingController _name; TextEditingController _reason; TextEditingController _date; TextEditingController _time; DateTime _eventDate; DateTime _eventTime; DateTime selectedDate = DateTime.now(); List data; 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 : ""); //_date = TextEditingController(text: widget.note != null ? widget.note.eventDate; _eventDate = DateTime.now(); //_eventTime = Time.now(); processing = false; } @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: [ 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))), ), ), 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))), ), ), 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), 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; }); } }, ), 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), 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.DateTime, }); }else{ await eventDBS.createItem(EventModel( title: _title.text, description: _description.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(); super.dispose(); } } Future _selectTime(BuildContext context) { final now = DateTime.now(); return showTimePicker( context: context, initialTime: TimeOfDay(hour: now.hour, minute: now.minute), ); } Future _selectDateTime(BuildContext context) => showDatePicker( context: context, initialDate: DateTime.now().add(Duration(seconds: 1)), firstDate: DateTime.now(), lastDate: DateTime(2100), );