|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:getwidget/getwidget.dart'; |
| 3 | + |
| 4 | +class GFMultiSelect extends StatefulWidget { |
| 5 | + /// |
| 6 | + const GFMultiSelect({ |
| 7 | + this.titleText, |
| 8 | + this.selectedTextStyle = |
| 9 | + const TextStyle(fontSize: 16, fontWeight: FontWeight.w500), |
| 10 | + this.color, |
| 11 | + this.avatar, |
| 12 | + this.padding = const EdgeInsets.all(0), |
| 13 | + this.margin = const EdgeInsets.all(5), |
| 14 | + this.size = GFSize.MEDIUM, |
| 15 | + this.type = GFCheckboxType.basic, |
| 16 | + this.checkColor = GFColors.WHITE, |
| 17 | + this.activebgColor = GFColors.WHITE, |
| 18 | + this.inactivebgColor = GFColors.WHITE, |
| 19 | + this.activeBorderColor = GFColors.WHITE, |
| 20 | + this.inactiveBorderColor = GFColors.WHITE, |
| 21 | + this.items, |
| 22 | + this.submitbutton, |
| 23 | + this.expandedicon = Icons.keyboard_arrow_down, |
| 24 | + this.collapsedicon = Icons.keyboard_arrow_up, |
| 25 | + this.dropdownbgColor = Colors.white, |
| 26 | + this.activeIcon = const Icon( |
| 27 | + Icons.check, |
| 28 | + size: 20, |
| 29 | + color: GFColors.DARK, |
| 30 | + ), |
| 31 | + this.inactiveIcon, |
| 32 | + this.custombgColor = GFColors.SUCCESS, |
| 33 | + this.selected = false, |
| 34 | + Key key, |
| 35 | + }) : assert(selected != null), |
| 36 | + super(key: key); |
| 37 | + |
| 38 | + ///type of [String] used to pass text, alternative to title property and gets higher priority than title |
| 39 | + final String titleText; |
| 40 | + |
| 41 | + /// The GFListTile's background color. Can be given [Color] or [GFColors] |
| 42 | + final Color color; |
| 43 | + |
| 44 | + /// type of [Widget] or [GFAvatar] used to create rounded user profile |
| 45 | + final Widget avatar; |
| 46 | + |
| 47 | + /// defines the margin of GFListTile |
| 48 | + final EdgeInsets margin; |
| 49 | + |
| 50 | + /// defines the padding of GFListTile |
| 51 | + final EdgeInsets padding; |
| 52 | + |
| 53 | + /// type of [GFCheckboxType] which is of four type is basic, sqaure, circular and custom |
| 54 | + final GFCheckboxType type; |
| 55 | + |
| 56 | + /// type of [double] which is GFSize ie, small, medium and large and can use any double value |
| 57 | + final double size; |
| 58 | + |
| 59 | + /// type pf [Color] used to change the checkcolor when the checkbox is active |
| 60 | + final Color checkColor; |
| 61 | + |
| 62 | + /// type of [Color] used to change the backgroundColor of the active checkbox |
| 63 | + final Color activebgColor; |
| 64 | + |
| 65 | + /// type of [Color] used to change the backgroundColor of the inactive checkbox |
| 66 | + final Color inactivebgColor; |
| 67 | + |
| 68 | + /// type of [Color] used to change the border color of the active checkbox |
| 69 | + final Color activeBorderColor; |
| 70 | + |
| 71 | + /// type of [Color] used to change the border color of the inactive checkbox |
| 72 | + final Color inactiveBorderColor; |
| 73 | + |
| 74 | + /// Called when the user checks or unchecks the checkbox. |
| 75 | + // final ValueChanged<bool> onChanged; |
| 76 | + |
| 77 | + ///Used to set the current state of the checkbox |
| 78 | + // final bool value; |
| 79 | + |
| 80 | + ///type of Widget used to change the checkbox's active icon |
| 81 | + final Widget activeIcon; |
| 82 | + |
| 83 | + ///type of [Widget] used to change the checkbox's inactive icon |
| 84 | + final Widget inactiveIcon; |
| 85 | + |
| 86 | + /// type of [Color] used to change the background color of the custom active checkbox only |
| 87 | + final Color custombgColor; |
| 88 | + |
| 89 | + /// To have the list tile appear selected when the checkbox is checked, pass the same value to both. |
| 90 | + /// Normally, this property is left to its default value, false. |
| 91 | + final bool selected; |
| 92 | + |
| 93 | + final List items; |
| 94 | + |
| 95 | + final Widget submitbutton; |
| 96 | + |
| 97 | + final Color dropdownbgColor; |
| 98 | + |
| 99 | + final TextStyle selectedTextStyle; |
| 100 | + |
| 101 | + final IconData expandedicon; |
| 102 | + |
| 103 | + final IconData collapsedicon; |
| 104 | + |
| 105 | + @override |
| 106 | + _GFMultiSelectState createState() => _GFMultiSelectState(); |
| 107 | +} |
| 108 | + |
| 109 | +class _GFMultiSelectState extends State<GFMultiSelect> { |
| 110 | + bool check = true; |
| 111 | + bool isdrop = false; |
| 112 | + bool check1 = false; |
| 113 | + |
| 114 | + final _controller = TextEditingController(); |
| 115 | + |
| 116 | + final List _selectedTitles = []; |
| 117 | + |
| 118 | + void _onItemSelect(bool selected, String title) { |
| 119 | + if (selected == true) { |
| 120 | + setState(() { |
| 121 | + _selectedTitles.add(title); |
| 122 | + }); |
| 123 | + } else { |
| 124 | + setState(() { |
| 125 | + _selectedTitles.remove(title); |
| 126 | + }); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + @override |
| 131 | + void dispose() { |
| 132 | + if (_controller != null) { |
| 133 | + _controller.dispose(); |
| 134 | + } |
| 135 | + super.dispose(); |
| 136 | + } |
| 137 | + |
| 138 | + @override |
| 139 | + Widget build(BuildContext context) => Column( |
| 140 | + children: [ |
| 141 | + InkWell( |
| 142 | + onTap: () { |
| 143 | + setState(() { |
| 144 | + isdrop = !isdrop; |
| 145 | + }); |
| 146 | + }, |
| 147 | + child: Container( |
| 148 | + |
| 149 | + // height: 40, |
| 150 | + width: MediaQuery.of(context).size.width, |
| 151 | + decoration: const BoxDecoration( |
| 152 | + border: |
| 153 | + Border(bottom: BorderSide(color: Colors.black12))), |
| 154 | + child: Container( |
| 155 | + child: Row( |
| 156 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 157 | + mainAxisAlignment: MainAxisAlignment.spaceEvenly, |
| 158 | + children: <Widget>[ |
| 159 | + Expanded( |
| 160 | + child: Container( |
| 161 | + padding: const EdgeInsets.only(bottom: 10), |
| 162 | + child: Text(_selectedTitles.join(' , ').toString(), |
| 163 | + style: widget.selectedTextStyle), |
| 164 | + )), |
| 165 | + Icon( |
| 166 | + !isdrop ? widget.expandedicon : widget.collapsedicon, |
| 167 | + color: Colors.black87, |
| 168 | + size: 30, |
| 169 | + ), |
| 170 | + ], |
| 171 | + ), |
| 172 | + ))), |
| 173 | + isdrop |
| 174 | + ? Container( |
| 175 | + decoration: BoxDecoration( |
| 176 | + color: widget.dropdownbgColor, |
| 177 | + boxShadow: const [ |
| 178 | + BoxShadow( |
| 179 | + color: Colors.black12, |
| 180 | + blurRadius: 3, |
| 181 | + ) |
| 182 | + ], |
| 183 | + ), |
| 184 | + height: 300, |
| 185 | + child: SingleChildScrollView( |
| 186 | + child: Column( |
| 187 | + children: [ |
| 188 | + Column( |
| 189 | + children: List.generate( |
| 190 | + widget.items.length, |
| 191 | + (index) => GFCheckboxListTile( |
| 192 | + value: _selectedTitles |
| 193 | + .contains(widget.items[index]), |
| 194 | + onChanged: (bool selected) { |
| 195 | + _controller.text; |
| 196 | + print(selected); |
| 197 | + _onItemSelect( |
| 198 | + selected, widget.items[index]); |
| 199 | + }, |
| 200 | + selected: widget.selected, |
| 201 | + avatar: widget.avatar, |
| 202 | + titleText: widget.items[index], |
| 203 | + color: widget.color, |
| 204 | + padding: widget.padding, |
| 205 | + margin: widget.margin, |
| 206 | + size: widget.size, |
| 207 | + activebgColor: widget.activebgColor, |
| 208 | + inactiveIcon: widget.inactiveIcon, |
| 209 | + activeBorderColor: |
| 210 | + widget.activeBorderColor, |
| 211 | + inactivebgColor: widget.inactivebgColor, |
| 212 | + activeIcon: widget.activeIcon, |
| 213 | + inactiveBorderColor: |
| 214 | + widget.inactiveBorderColor, |
| 215 | + custombgColor: widget.custombgColor, |
| 216 | + checkColor: widget.checkColor, |
| 217 | + type: widget.type, |
| 218 | + ))), |
| 219 | + GFButton( |
| 220 | + onPressed: () { |
| 221 | + setState(() { |
| 222 | + isdrop = !isdrop; |
| 223 | + }); |
| 224 | + }, |
| 225 | + child: widget.submitbutton, |
| 226 | + ) |
| 227 | + ], |
| 228 | + ), |
| 229 | + ), |
| 230 | + ) |
| 231 | + : Container(), |
| 232 | + ], |
| 233 | + ); |
| 234 | +} |
0 commit comments