From aed13136efa7e0489c72c51c48d07dd0abdbc8bf Mon Sep 17 00:00:00 2001 From: yaso-meth Date: Tue, 13 Aug 2024 12:11:16 +0200 Subject: [PATCH] add a time input field --- .../inputsAndButtons/mihTimeInput.dart | 44 +++++++++++++------ 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/Frontend/patient_manager/lib/components/inputsAndButtons/mihTimeInput.dart b/Frontend/patient_manager/lib/components/inputsAndButtons/mihTimeInput.dart index 77786d29..e611d199 100644 --- a/Frontend/patient_manager/lib/components/inputsAndButtons/mihTimeInput.dart +++ b/Frontend/patient_manager/lib/components/inputsAndButtons/mihTimeInput.dart @@ -1,12 +1,12 @@ import 'package:flutter/material.dart'; import 'package:patient_manager/main.dart'; -class MIHDateField extends StatefulWidget { +class MIHTimeField extends StatefulWidget { final controller; final String LableText; final bool required; - const MIHDateField({ + const MIHTimeField({ super.key, required this.controller, required this.LableText, @@ -14,23 +14,41 @@ class MIHDateField extends StatefulWidget { }); @override - State createState() => _MIHDateFieldState(); + State createState() => _MIHDateFieldState(); } -class _MIHDateFieldState extends State { +class _MIHDateFieldState extends State { FocusNode _focus = FocusNode(); bool startup = true; - // bool makeEditable() { - Future _selectDate(BuildContext context) async { - DateTime? picked = await showDatePicker( + + Future _selectTime(BuildContext context) async { + TimeOfDay? picked = await showTimePicker( context: context, - initialDate: DateTime.now(), - firstDate: DateTime(2000), - lastDate: DateTime(2100), + initialTime: TimeOfDay.now(), + builder: (context, child) { + return MediaQuery( + data: MediaQuery.of(context).copyWith(alwaysUse24HourFormat: true), + child: child as Widget, + ); + }, ); if (picked != null) { + String hours = ""; + String minutes = ""; setState(() { - widget.controller.text = picked.toString().split(" ")[0]; + if (picked.hour <= 9) { + hours = "0${picked.hour}"; + } else { + hours = "${picked.hour}"; + } + + if (picked.minute <= 9) { + minutes = "0${picked.minute}"; + } else { + minutes = "${picked.minute}"; + } + + widget.controller.text = "$hours:$minutes"; }); } } @@ -108,7 +126,7 @@ class _MIHDateFieldState extends State { //labelText: widget.LableText, //labelStyle: const TextStyle(color: Colors.blueAccent), prefixIcon: Icon( - Icons.calendar_today, + Icons.access_alarm, color: MzanziInnovationHub.of(context)!.theme.secondaryColor(), ), fillColor: MzanziInnovationHub.of(context)!.theme.primaryColor(), @@ -139,7 +157,7 @@ class _MIHDateFieldState extends State { ), ), onTap: () { - _selectDate(context); + _selectTime(context); }, ); }