From 61ea9f9b4427adfbdd6857ced5079ada073a9b87 Mon Sep 17 00:00:00 2001 From: yaso-meth Date: Thu, 4 Jul 2024 14:24:27 +0200 Subject: [PATCH] add required option. add icon to clear coltroller. added dynamic title. all paramater of items for menu in lis format. --- .../lib/components/myDropdownInput.dart | 99 +++++++++++++++++-- 1 file changed, 91 insertions(+), 8 deletions(-) diff --git a/Frontend/patient_manager/lib/components/myDropdownInput.dart b/Frontend/patient_manager/lib/components/myDropdownInput.dart index 8544b754..573ea237 100644 --- a/Frontend/patient_manager/lib/components/myDropdownInput.dart +++ b/Frontend/patient_manager/lib/components/myDropdownInput.dart @@ -1,12 +1,12 @@ import 'package:flutter/material.dart'; -import 'package:flutter/widgets.dart'; import 'package:patient_manager/objects/appUser.dart'; class MyDropdownField extends StatefulWidget { - final controller; + final TextEditingController controller; final AppUser signedInUser; - //final String hintText; - final List> dropdownOptions; + final String hintText; + final bool required; + final List dropdownOptions; //final bool editable; @@ -14,8 +14,9 @@ class MyDropdownField extends StatefulWidget { super.key, required this.controller, required this.signedInUser, - //required this.hintText, + required this.hintText, required this.dropdownOptions, + required this.required, }); @override @@ -23,7 +24,68 @@ class MyDropdownField extends StatefulWidget { } class _MyDropdownFieldState extends State { - var dropbownItems = ["Dr.", "Assistant"]; + //var dropbownItems = ["Dr.", "Assistant"]; + bool startup = true; + final FocusNode _focus = FocusNode(); + late var menu; + + Widget setRequiredText() { + if (widget.required) { + return Row( + mainAxisSize: MainAxisSize.min, + children: [ + const Text( + "*", + style: TextStyle(color: Colors.red), + ), + const SizedBox( + width: 8.0, + ), + Text(widget.hintText, + style: const TextStyle(color: Colors.blueAccent)), + ], + ); + } else { + return Text(widget.hintText, + style: const TextStyle(color: Colors.blueAccent)); + } + } + + void _onFocusChange() { + setState(() { + startup = false; + }); + } + + String? get _errorText { + final text = widget.controller.text; + if (startup) { + return null; + } + if (!widget.required) { + return null; + } + if (text.isEmpty) { + return "${widget.hintText} is required"; + } + return null; + } + + List> buidMenueOptions(List options) { + List> menueList = []; + for (final i in options) { + menueList.add(DropdownMenuEntry(value: i, label: i)); + } + return menueList; + } + + @override + void initState() { + menu = buidMenueOptions(widget.dropdownOptions); + _focus.addListener(_onFocusChange); + super.initState(); + } + // bool makeEditable() { @override Widget build(BuildContext context) { @@ -32,7 +94,24 @@ class _MyDropdownFieldState extends State { child: DropdownMenu( controller: widget.controller, expandedInsets: EdgeInsets.zero, - label: const Text("Title", style: TextStyle(color: Colors.blueAccent)), + label: setRequiredText(), + errorText: _errorText, + focusNode: _focus, + onSelected: (_) => setState(() { + startup = false; + }), + leadingIcon: IconButton( + onPressed: () { + setState(() { + startup = false; + }); + widget.controller.clear(); + }, + icon: const Icon( + Icons.delete_outline_rounded, + color: Colors.blueAccent, + ), + ), menuStyle: const MenuStyle( backgroundColor: WidgetStatePropertyAll(Colors.white), side: WidgetStatePropertyAll( @@ -50,7 +129,11 @@ class _MyDropdownFieldState extends State { ), outlineBorder: BorderSide(color: Colors.blue), ), - dropdownMenuEntries: widget.dropdownOptions, + dropdownMenuEntries: menu, + // const >[ + // DropdownMenuEntry(value: "Dr.", label: "Dr."), + // DropdownMenuEntry(value: "Assistant", label: "Assistant"), + // ], ), ); }