multi_selected.dart 7.8 KB

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