multi_selected.dart 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import 'package:fis_lib_report/converts/pt_to_px_converter.dart';
  2. import 'package:fis_lib_report/converts/text_size_converter.dart';
  3. import 'package:fis_lib_report/pages/components/input_text.dart';
  4. import 'package:fis_lib_report/report/inputText.dart';
  5. import 'package:fis_lib_report/report/multiSelected.dart';
  6. import 'package:fis_lib_report/report/rt_color.dart';
  7. import 'package:fis_lib_report/report/singleSelected.dart';
  8. import 'package:flutter/material.dart';
  9. import 'package:dropdown_button2/dropdown_button2.dart';
  10. import 'package:fis_lib_report/report/inputText.dart';
  11. import 'package:fis_ui/simple/select.dart';
  12. class RMultiSelected extends StatefulWidget {
  13. final MultiSelected multiSelected;
  14. RMultiSelected(this.multiSelected);
  15. @override
  16. State<StatefulWidget> createState() {
  17. return _RRMultiSelectedState(multiSelected);
  18. }
  19. }
  20. class _RRMultiSelectedState extends State<RMultiSelected> {
  21. MultiSelected multiSelected;
  22. _RRMultiSelectedState(this.multiSelected);
  23. String _value = '';
  24. String? _selectedItemView = '';
  25. List<String>? _items = [];
  26. List<String>? _selectedItems = [];
  27. double _width = 0.0;
  28. TextStyle _textStyle = TextStyle();
  29. @override
  30. initState() {
  31. if (multiSelected.items!.isNotEmpty) {
  32. _items = multiSelected.items;
  33. }
  34. _width = PtToPxConverter.ptToPx(multiSelected.lineWidth);
  35. final fontColor = multiSelected.fontColor ?? RTColor.Black;
  36. _textStyle = TextStyle(
  37. color: Color.fromARGB(
  38. fontColor.a!, fontColor.r!, fontColor.g!, fontColor.b!),
  39. fontSize: PtToPxConverter.ptToPx(multiSelected.fontSize),
  40. );
  41. super.initState();
  42. }
  43. @override
  44. Widget build(BuildContext context) {
  45. final background = multiSelected.background ?? RTColor(255, 255, 255, 255);
  46. return Stack(
  47. children: [
  48. Container(
  49. decoration: BoxDecoration(
  50. border: Border.all(color: Colors.grey, width: 1.0),
  51. borderRadius: BorderRadius.circular(0),
  52. color: Color.fromARGB(
  53. background.a!, background.r!, background.g!, background.b!),
  54. ),
  55. padding: const EdgeInsets.only(right: 25),
  56. width: _width,
  57. height: 22,
  58. child: Text(
  59. _selectedItemView!,
  60. maxLines: 1,
  61. style: _textStyle,
  62. ),
  63. ),
  64. _buildDropdownButton(),
  65. ],
  66. );
  67. }
  68. List<DropdownMenuItem<String>> buildItems() {
  69. return _items!.map((String e) {
  70. final active = e == _value;
  71. return DropdownMenuItem<String>(
  72. child: _OptionRow(
  73. multiSelected,
  74. e,
  75. isActive: active,
  76. height: 22,
  77. onTap: () {
  78. setState(() {
  79. if (_selectedItems!.contains(e)) {
  80. _selectedItems!.remove(e);
  81. } else {
  82. _selectedItems!.add(e);
  83. }
  84. String text = '';
  85. for (var element in _selectedItems!) {
  86. final index = _selectedItems!.indexOf(element);
  87. if (index > 0) {
  88. text = text + ',' + element;
  89. } else if (index == 0) {
  90. text = element;
  91. } else if (_selectedItems!.isEmpty) {
  92. text = '';
  93. }
  94. }
  95. _selectedItemView = text;
  96. final width = TextSizeConvert.getTextSize(text, _textStyle).width;
  97. if (_width < width - 25) {
  98. // _width = width - 25;
  99. }
  100. });
  101. },
  102. isSelected: _selectedItems!.contains(e),
  103. ),
  104. value: e,
  105. );
  106. }).toList();
  107. }
  108. Widget _buildDropdownButton() {
  109. if (_items!.isEmpty) {
  110. return const SizedBox();
  111. }
  112. var buttonDecoration = BoxDecoration(
  113. border: Border.all(color: Colors.transparent, width: 1.0),
  114. borderRadius: BorderRadius.circular(0),
  115. color: Colors.transparent,
  116. );
  117. return SizedBox(
  118. height: 22,
  119. width: _width,
  120. child: DropdownButtonHideUnderline(
  121. child: DropdownButton2<String>(
  122. dropdownMaxHeight: 300,
  123. buttonWidth: _width,
  124. itemPadding: const EdgeInsets.symmetric(horizontal: 1, vertical: 2),
  125. itemHeight: 28,
  126. // itemPadding: const EdgeInsets.symmetric(horizontal: 4, vertical: 2),
  127. isExpanded: false,
  128. dropdownOverButton: false,
  129. scrollbarAlwaysShow: true,
  130. scrollbarThickness: 4,
  131. scrollbarRadius: const Radius.circular(2),
  132. buttonPadding: const EdgeInsets.symmetric(horizontal: 4, vertical: 0),
  133. dropdownDecoration: BoxDecoration(
  134. borderRadius: BorderRadius.circular(4),
  135. ),
  136. buttonDecoration: buttonDecoration,
  137. offset: const Offset(0, -4),
  138. selectedItemHighlightColor: Theme.of(context).secondaryHeaderColor,
  139. selectedItemBuilder: (_) => _selectedItems!.map((e) {
  140. return SizedBox(
  141. width: _width - 34,
  142. child: Text(
  143. e,
  144. maxLines: 1,
  145. ),
  146. );
  147. }).toList(),
  148. value: null,
  149. items: buildItems(),
  150. onChanged: (v) {},
  151. ),
  152. ),
  153. );
  154. }
  155. }
  156. class _OptionRow extends StatefulWidget {
  157. _OptionRow(
  158. this.multiSelected,
  159. this.label, {
  160. Key? key,
  161. this.isActive = false,
  162. this.height,
  163. this.onTap,
  164. this.isSelected,
  165. }) : super(key: key);
  166. final Function? onTap;
  167. final MultiSelected multiSelected;
  168. final String label;
  169. final bool isActive;
  170. final double? height;
  171. bool? isSelected = false;
  172. @override
  173. State<StatefulWidget> createState() {
  174. return _OptionRowState(isSelected!);
  175. }
  176. }
  177. class _OptionRowState extends State<_OptionRow> {
  178. _OptionRowState(this.isSelected);
  179. bool? isSelected = false;
  180. @override
  181. Widget build(BuildContext context) {
  182. final color = widget.multiSelected.fontColor!;
  183. final background = widget.multiSelected.background!;
  184. final text = Text(
  185. widget.label,
  186. style: TextStyle(
  187. backgroundColor: Color.fromARGB(
  188. background.a!, background.r!, background.g!, background.b!),
  189. fontSize: PtToPxConverter.ptToPx(widget.multiSelected.fontSize),
  190. color: Color.fromARGB(color.a!, color.r!, color.g!, color.b!),
  191. ),
  192. );
  193. if (widget.height != null) {
  194. return GestureDetector(
  195. onTap: () {
  196. setState(() {
  197. isSelected = !isSelected!;
  198. });
  199. if (widget.onTap != null) {
  200. widget.onTap!.call();
  201. }
  202. },
  203. child: Container(
  204. color: Colors.white,
  205. alignment: Alignment.centerLeft,
  206. height: widget.height,
  207. width: PtToPxConverter.ptToPx(widget.multiSelected.lineWidth) - 34,
  208. child: Row(
  209. children: [
  210. Checkbox(
  211. onChanged: (bool? value) {
  212. setState(() {
  213. isSelected = value ?? false;
  214. });
  215. if (widget.onTap != null) {
  216. widget.onTap!.call();
  217. }
  218. },
  219. value: isSelected,
  220. ),
  221. Expanded(child: text),
  222. ],
  223. ),
  224. ),
  225. );
  226. } else {
  227. return text;
  228. }
  229. }
  230. }