|
| 1 | +part of 'tf_form.dart'; |
| 2 | + |
| 3 | +/// [TFDropdownField2] widget allows the user to pick a value from a dropdown list |
| 4 | +class TFDropdownField2<T extends TFDropdonwnItem> extends StatefulWidget { |
| 5 | + TFDropdownField2({ |
| 6 | + Key? key, |
| 7 | + this.title, |
| 8 | + this.hintText, |
| 9 | + required this.items, |
| 10 | + this.selectedItem, |
| 11 | + this.onChanged, |
| 12 | + this.validationTypes = const <TFValidationType>[], |
| 13 | + this.relatedController, |
| 14 | + this.style, |
| 15 | + this.enabled = true, |
| 16 | + this.showError = true, |
| 17 | + }) : super(key: key) { |
| 18 | + if (validationTypes.contains(TFValidationType.requiredIfHas) && relatedController == null) { |
| 19 | + throw ArgumentError("requiredIfHas type and relatedController should both be set."); |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + final String? title; |
| 24 | + final String? hintText; |
| 25 | + final List<T> items; |
| 26 | + final T? selectedItem; |
| 27 | + final Function(T selectedItem)? onChanged; |
| 28 | + final List<TFValidationType> validationTypes; |
| 29 | + final TextEditingController? relatedController; |
| 30 | + final TFFieldStyle? style; |
| 31 | + final bool enabled; |
| 32 | + final bool showError; |
| 33 | + |
| 34 | + @override |
| 35 | + State<TFDropdownField2<T>> createState() => _TFDropdownFieldState2<T>(); |
| 36 | +} |
| 37 | + |
| 38 | +class _TFDropdownFieldState2<T extends TFDropdonwnItem> extends State<TFDropdownField2<T>> { |
| 39 | + final LayerLink _dropdownLink = LayerLink(); |
| 40 | + final TextEditingController _titleController = TextEditingController(); |
| 41 | + OverlayEntry? _dropdownOverlay; |
| 42 | + |
| 43 | + void _showDropdown() { |
| 44 | + _dropdownOverlay = _buildDropListOverlay(); |
| 45 | + Overlay.of(context).insert(_dropdownOverlay!); |
| 46 | + } |
| 47 | + |
| 48 | + void _hideDropdown() { |
| 49 | + _dropdownOverlay?.remove(); |
| 50 | + _dropdownOverlay = null; |
| 51 | + } |
| 52 | + |
| 53 | + void _toggleDropdown() { |
| 54 | + if (_dropdownOverlay == null) { |
| 55 | + _showDropdown(); |
| 56 | + } else { |
| 57 | + _hideDropdown(); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @override |
| 62 | + void initState() { |
| 63 | + super.initState(); |
| 64 | + _titleController.text = widget.selectedItem?.displayTitle ?? ""; |
| 65 | + } |
| 66 | + |
| 67 | + @override |
| 68 | + void dispose() { |
| 69 | + _titleController.dispose(); |
| 70 | + _dropdownOverlay?.dispose(); |
| 71 | + super.dispose(); |
| 72 | + } |
| 73 | + |
| 74 | + @override |
| 75 | + Widget build(BuildContext context) { |
| 76 | + return CompositedTransformTarget( |
| 77 | + link: _dropdownLink, |
| 78 | + child: TFTextField( |
| 79 | + title: widget.title, |
| 80 | + hintText: widget.hintText, |
| 81 | + controller: _titleController, |
| 82 | + validationTypes: widget.validationTypes, |
| 83 | + relatedController: widget.relatedController, |
| 84 | + readOnly: true, |
| 85 | + style: widget.style, |
| 86 | + enabled: widget.enabled, |
| 87 | + showError: widget.showError, |
| 88 | + suffix: GestureDetector( |
| 89 | + onTap: _toggleDropdown, |
| 90 | + child: const Icon( |
| 91 | + Icons.arrow_drop_down, |
| 92 | + color: Colors.grey, |
| 93 | + ), |
| 94 | + ), |
| 95 | + onTap: _toggleDropdown, |
| 96 | + onFocusChanged: (hasFocus) { |
| 97 | + if (!hasFocus) _hideDropdown(); |
| 98 | + }, |
| 99 | + ), |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + OverlayEntry _buildDropListOverlay() { |
| 104 | + final renderBox = context.findRenderObject() as RenderBox; |
| 105 | + final size = renderBox.size; |
| 106 | + final offset = renderBox.localToGlobal(Offset.zero); |
| 107 | + final topOffset = offset.dy + size.height + 5; |
| 108 | + |
| 109 | + return OverlayEntry( |
| 110 | + builder: (context) => GestureDetector( |
| 111 | + onTap: () => _hideDropdown(), |
| 112 | + behavior: HitTestBehavior.translucent, |
| 113 | + child: SizedBox( |
| 114 | + height: MediaQuery.of(context).size.height, |
| 115 | + width: MediaQuery.of(context).size.width, |
| 116 | + child: Stack( |
| 117 | + children: [ |
| 118 | + Positioned( |
| 119 | + left: offset.dx, |
| 120 | + top: topOffset, |
| 121 | + width: size.width, |
| 122 | + child: CompositedTransformFollower( |
| 123 | + link: _dropdownLink, |
| 124 | + showWhenUnlinked: false, |
| 125 | + offset: Offset(0.0, size.height + 5.0), |
| 126 | + child: Material( |
| 127 | + elevation: 2, |
| 128 | + color: _tffStyle.fieldStyle.backgroundColor, |
| 129 | + child: Column( |
| 130 | + children: List.generate(widget.items.length, (index) { |
| 131 | + final item = widget.items[index]; |
| 132 | + final isSelected = widget.selectedItem?.id == item.id; |
| 133 | + return ListTile( |
| 134 | + title: Text(item.displayTitle), |
| 135 | + selected: isSelected, |
| 136 | + selectedColor: Theme.of(context).colorScheme.onPrimary, |
| 137 | + selectedTileColor: Theme.of(context).colorScheme.primary, |
| 138 | + trailing: isSelected ? const Icon(Icons.check, size: 18) : null, |
| 139 | + onTap: () { |
| 140 | + widget.onChanged?.call(item); |
| 141 | + _hideDropdown(); |
| 142 | + }, |
| 143 | + ); |
| 144 | + }), |
| 145 | + ), |
| 146 | + ), |
| 147 | + ), |
| 148 | + ), |
| 149 | + ], |
| 150 | + ), |
| 151 | + ), |
| 152 | + ), |
| 153 | + ); |
| 154 | + } |
| 155 | +} |
0 commit comments