From 1770fedf0ca3f28d03f1d2edc95d287e8566d561 Mon Sep 17 00:00:00 2001 From: Abdullah Al-Hossain Date: Wed, 23 Aug 2023 07:05:22 +0600 Subject: [PATCH 1/2] displayValue property added to display value in conjunction with name. --- README.md | 4 ++++ lib/dropdown_textfield.dart | 23 ++++++++++++++++------- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index dddc813..7356c99 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,10 @@ isEnabled=false to disable textfield,default value true ### enableSearch enableSearch=true to enable search option in dropdown,as of now this feature enabled only for single selection dropdown +### displayValue +to display value in conjunction with name and display the value on selection. +more specifically, country code in selection dropdown show 'Bangladesh(+880)' and display '+880' on selection - where name is 'Bangladesh' and value is '+880'. + ### displayCompleteItem set displayCompleteItem=true, if you want show complete list of selected item in textfield else it will display like "number_of_item item selected" diff --git a/lib/dropdown_textfield.dart b/lib/dropdown_textfield.dart index 554a672..613a944 100644 --- a/lib/dropdown_textfield.dart +++ b/lib/dropdown_textfield.dart @@ -60,6 +60,7 @@ class DropDownTextField extends StatefulWidget { this.validator, this.isEnabled = true, this.enableSearch = false, + this.displayValue = false, this.readOnly = true, this.dropdownRadius = 12, this.textFieldDecoration, @@ -137,6 +138,7 @@ class DropDownTextField extends StatefulWidget { multiController = controller, isMultiSelection = true, enableSearch = false, + displayValue = false, readOnly = true, searchAutofocus = false, searchKeyboardType = null, @@ -192,6 +194,9 @@ class DropDownTextField extends StatefulWidget { ///by setting enableSearch=true enable search option in dropdown,as of now this feature enabled only for single selection dropdown final bool enableSearch; + ///by setting displayValue=true enable displaying the 'value' instead of 'name' and in list it will show 'name(value)' + final bool displayValue; + final bool readOnly; ///set displayCompleteItem=true, if you want show complete list of item in textfield else it will display like "number_of_item item selected" @@ -442,7 +447,7 @@ class _DropDownTextFieldState extends State } else { if (widget.singleController != null) { if (widget.singleController!.dropDownValue != null) { - _cnt.text = widget.singleController!.dropDownValue!.name; + _cnt.text = widget.displayValue ? widget.singleController!.dropDownValue!.value : widget.singleController!.dropDownValue!.name; } else { _cnt.clear(); } @@ -450,7 +455,7 @@ class _DropDownTextFieldState extends State } _listTileTextStyle = - (widget.listTextStyle ?? Theme.of(context).textTheme.subtitle1)!; + (widget.listTextStyle ?? Theme.of(context).textTheme.titleMedium)!; _listTileHeight = _textWidgetSize("dummy Text", _listTileTextStyle).height + _listPadding.top + @@ -669,7 +674,7 @@ class _DropDownTextFieldState extends State builder: buildOverlay, ))), ); - overlay?.insert(_isScrollPadding ? _entry2! : _entry!); + overlay.insert(_isScrollPadding ? _entry2! : _entry!); } _openOutSideClickOverlay(BuildContext context) { @@ -687,7 +692,7 @@ class _DropDownTextFieldState extends State ), ); }); - overlay2?.insert(_barrierOverlay!); + overlay2.insert(_barrierOverlay!); } void hideOverlay() { @@ -767,13 +772,14 @@ class _DropDownTextFieldState extends State mainFocusNode: _textFieldFocusNode, searchFocusNode: _searchFocusNode, enableSearch: widget.enableSearch, + displayValue: widget.displayValue, height: _height, listTileHeight: _listTileHeight, dropDownList: _dropDownList, listTextStyle: _listTileTextStyle, onChanged: (item) { setState(() { - _cnt.text = item.name; + _cnt.text = widget.displayValue? item.value : item.name; _isExpanded = !_isExpanded; }); if (widget.singleController != null) { @@ -868,6 +874,7 @@ class SingleSelection extends StatefulWidget { required this.onChanged, required this.height, required this.enableSearch, + required this.displayValue, required this.searchHeight, required this.searchFocusNode, required this.mainFocusNode, @@ -889,6 +896,7 @@ class SingleSelection extends StatefulWidget { final double height; final double listTileHeight; final bool enableSearch; + final bool displayValue; final double searchHeight; final FocusNode searchFocusNode; final FocusNode mainFocusNode; @@ -1025,7 +1033,7 @@ class _SingleSelectionState extends State { alignment: Alignment.centerLeft, child: FittedBox( fit: BoxFit.fitHeight, - child: Text(newDropDownList[index].name, + child: Text(widget.displayValue ? '${newDropDownList[index].name}(${newDropDownList[index].value})' : newDropDownList[index].name, style: widget.listTextStyle), ), ), @@ -1311,7 +1319,8 @@ class _KeyboardVisibilityBuilderState extends State @override void didChangeMetrics() { - final bottomInset = WidgetsBinding.instance.window.viewInsets.bottom; + // final bottomInset = WidgetsBinding.instance.window.viewInsets.bottom; + final bottomInset = WidgetsBinding.instance.platformDispatcher.views.first.viewInsets.bottom; final newValue = bottomInset > 0.0; if (newValue != _isKeyboardVisible) { setState(() { From 9f49cd02b090e96a0ace9c4667dcea3cdac8ad07 Mon Sep 17 00:00:00 2001 From: Abdullah Al-Hossain Date: Wed, 23 Aug 2023 07:34:15 +0600 Subject: [PATCH 2/2] displayValue is optional. --- lib/dropdown_textfield.dart | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/dropdown_textfield.dart b/lib/dropdown_textfield.dart index 613a944..4843050 100644 --- a/lib/dropdown_textfield.dart +++ b/lib/dropdown_textfield.dart @@ -195,7 +195,7 @@ class DropDownTextField extends StatefulWidget { final bool enableSearch; ///by setting displayValue=true enable displaying the 'value' instead of 'name' and in list it will show 'name(value)' - final bool displayValue; + final bool? displayValue; final bool readOnly; @@ -447,7 +447,7 @@ class _DropDownTextFieldState extends State } else { if (widget.singleController != null) { if (widget.singleController!.dropDownValue != null) { - _cnt.text = widget.displayValue ? widget.singleController!.dropDownValue!.value : widget.singleController!.dropDownValue!.name; + _cnt.text = (widget.displayValue?? false) ? widget.singleController!.dropDownValue!.value : widget.singleController!.dropDownValue!.name; } else { _cnt.clear(); } @@ -779,7 +779,7 @@ class _DropDownTextFieldState extends State listTextStyle: _listTileTextStyle, onChanged: (item) { setState(() { - _cnt.text = widget.displayValue? item.value : item.name; + _cnt.text = (widget.displayValue?? false) ? item.value : item.name; _isExpanded = !_isExpanded; }); if (widget.singleController != null) { @@ -874,7 +874,7 @@ class SingleSelection extends StatefulWidget { required this.onChanged, required this.height, required this.enableSearch, - required this.displayValue, + this.displayValue, required this.searchHeight, required this.searchFocusNode, required this.mainFocusNode, @@ -896,7 +896,7 @@ class SingleSelection extends StatefulWidget { final double height; final double listTileHeight; final bool enableSearch; - final bool displayValue; + final bool? displayValue; final double searchHeight; final FocusNode searchFocusNode; final FocusNode mainFocusNode; @@ -1033,7 +1033,7 @@ class _SingleSelectionState extends State { alignment: Alignment.centerLeft, child: FittedBox( fit: BoxFit.fitHeight, - child: Text(widget.displayValue ? '${newDropDownList[index].name}(${newDropDownList[index].value})' : newDropDownList[index].name, + child: Text((widget.displayValue?? false) ? '${newDropDownList[index].name}(${newDropDownList[index].value})' : newDropDownList[index].name, style: widget.listTextStyle), ), ),