multi_selected.dart 7.7 KB

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