|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | + |
| 3 | +class GFDropdown<T> extends StatefulWidget { |
| 4 | + GFDropdown( |
| 5 | + {Key key, |
| 6 | + @required this.items, |
| 7 | + this.icon, |
| 8 | + this.selectedItemBuilder, |
| 9 | + this.value, |
| 10 | + this.hint, |
| 11 | + this.disabledHint, |
| 12 | + @required this.onChanged, |
| 13 | + this.onTap, |
| 14 | + this.elevation = 8, |
| 15 | + this.style, |
| 16 | + this.underline, |
| 17 | + this.iconDisabledColor, |
| 18 | + this.iconEnabledColor, |
| 19 | + this.iconSize = 24.0, |
| 20 | + this.isDense = false, |
| 21 | + this.isExpanded = false, |
| 22 | + this.itemHeight = kMinInteractiveDimension, |
| 23 | + this.focusColor, |
| 24 | + this.focusNode, |
| 25 | + this.autofocus = false, |
| 26 | + this.dropdownColor, |
| 27 | + this.padding = const EdgeInsets.all(5), |
| 28 | + this.borderRadius = const BorderRadius.all(Radius.circular(4)), |
| 29 | + this.borderColor}); |
| 30 | + |
| 31 | + DropdownButtonBuilder selectedItemBuilder; |
| 32 | + final List<DropdownMenuItem<T>> items; |
| 33 | + |
| 34 | + /// The widget to use for the drop-down button's icon. |
| 35 | + /// |
| 36 | + /// Defaults to an [Icon] with the [Icons.arrow_drop_down] glyph. |
| 37 | + final Widget icon; |
| 38 | + |
| 39 | + /// The z-coordinate at which to place the menu when open. |
| 40 | + final int elevation; |
| 41 | + |
| 42 | + /// The value of the currently selected [DropdownMenuItem]. |
| 43 | + final T value; |
| 44 | + |
| 45 | + /// the color of the border of the dropdown button |
| 46 | + final Color borderColor; |
| 47 | + |
| 48 | + ///The padding given inside the dropdown |
| 49 | + final EdgeInsets padding; |
| 50 | + |
| 51 | + /// A placeholder widget that is displayed by the dropdown button. |
| 52 | + final Widget hint; |
| 53 | + |
| 54 | + /// A message to show when the dropdown is disabled. |
| 55 | + final Widget disabledHint; |
| 56 | + |
| 57 | + /// Called when the user selects an item. |
| 58 | + /// |
| 59 | + /// If the [onChanged] callback is null or the list of [DropdownButton.items] |
| 60 | + /// is null then the dropdown button will be disabled, |
| 61 | + final ValueChanged<T> onChanged; |
| 62 | + |
| 63 | + /// Called when the dropdown button is tapped. |
| 64 | + final VoidCallback onTap; |
| 65 | + |
| 66 | + /// Defaults to the [TextTheme.subtitle1] value of the current |
| 67 | + /// [ThemeData.textTheme] of the current [Theme]. |
| 68 | + final TextStyle style; |
| 69 | + |
| 70 | + /// The widget to use for drawing the drop-down button's underline. |
| 71 | + final Widget underline; |
| 72 | + |
| 73 | + /// The color of any [Icon] descendant of [icon] if this button is disabled, |
| 74 | + /// i.e. if [onChanged] is null. |
| 75 | + final Color iconDisabledColor; |
| 76 | + |
| 77 | + /// The color of any [Icon] descendant of [icon] if this button is enabled, |
| 78 | + /// i.e. if [onChanged] is defined. |
| 79 | + final Color iconEnabledColor; |
| 80 | + |
| 81 | + /// The size to use for the drop-down button's down arrow icon button. |
| 82 | + final double iconSize; |
| 83 | + |
| 84 | + /// Reduce the button's height. |
| 85 | + final bool isDense; |
| 86 | + |
| 87 | + /// Set the dropdown's inner contents to horizontally fill its parent. |
| 88 | + final bool isExpanded; |
| 89 | + |
| 90 | + /// The default value is [kMinInteractiveDimension], which is also the minimum |
| 91 | + /// height for menu items. |
| 92 | + /// |
| 93 | + /// If this value is null and there isn't enough vertical room for the menu, |
| 94 | + final double itemHeight; |
| 95 | + |
| 96 | + /// The color for the button's [Material] when it has the input focus. |
| 97 | + final Color focusColor; |
| 98 | + |
| 99 | + /// {@macro flutter.widgets.Focus.focusNode} |
| 100 | + final FocusNode focusNode; |
| 101 | + |
| 102 | + /// {@macro flutter.widgets.Focus.autofocus} |
| 103 | + final bool autofocus; |
| 104 | + |
| 105 | + /// The background color of the dropdown. |
| 106 | + final Color dropdownColor; |
| 107 | + |
| 108 | + /// The border radius of the dropdown. |
| 109 | + final BorderRadius borderRadius; |
| 110 | + |
| 111 | + @override |
| 112 | + _GFDropdownState createState() => _GFDropdownState(); |
| 113 | +} |
| 114 | + |
| 115 | +class _GFDropdownState extends State<GFDropdown> { |
| 116 | + @override |
| 117 | + Widget build(BuildContext context) { |
| 118 | + return Card( |
| 119 | + child: Container( |
| 120 | + padding: widget.padding, |
| 121 | + decoration: BoxDecoration( |
| 122 | + borderRadius: widget.borderRadius, |
| 123 | + border: Border.all( |
| 124 | + color: widget.borderColor != null |
| 125 | + ? widget.borderColor |
| 126 | + : Colors.white)), |
| 127 | + child: DropdownButton( |
| 128 | + |
| 129 | + items: widget.items, |
| 130 | + selectedItemBuilder: widget.selectedItemBuilder, |
| 131 | + value: widget.value, |
| 132 | + hint: widget.hint, |
| 133 | + disabledHint: widget.disabledHint, |
| 134 | + onChanged: widget.onChanged == null ? null : widget.onChanged, |
| 135 | + onTap: widget.onTap, |
| 136 | + elevation: widget.elevation, |
| 137 | + style: widget.style, |
| 138 | + icon: widget.icon, |
| 139 | + iconDisabledColor: widget.iconDisabledColor, |
| 140 | + iconEnabledColor: widget.iconEnabledColor, |
| 141 | + iconSize: widget.iconSize, |
| 142 | + isDense: widget.isDense, |
| 143 | + isExpanded: widget.isExpanded, |
| 144 | + itemHeight: widget.itemHeight, |
| 145 | + focusColor: widget.focusColor, |
| 146 | + focusNode: widget.focusNode, |
| 147 | + autofocus: widget.autofocus, |
| 148 | + dropdownColor: widget.dropdownColor, |
| 149 | + ))); |
| 150 | + } |
| 151 | +} |
0 commit comments