multi_selected.dart 8.1 KB

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