input_imageList.dart 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import 'package:fis_lib_report/converts/margin_convert.dart';
  2. import 'package:fis_lib_report/converts/pt_to_px_converter.dart';
  3. import 'package:fis_lib_report/report/inputImageList.dart';
  4. import 'package:fis_lib_report/report_info/input_image_list_info.dart';
  5. import 'package:fis_lib_report/report_info/report_info.dart';
  6. import 'package:fis_ui/business/vid_img_view/index.dart';
  7. import 'package:flutter/material.dart';
  8. class RInputImageList extends StatefulWidget {
  9. final InputImageList inputImageList;
  10. const RInputImageList(this.inputImageList);
  11. @override
  12. State<StatefulWidget> createState() {
  13. return _RInputImageListState();
  14. }
  15. }
  16. class _RInputImageListState extends State<RInputImageList> {
  17. InputImageList? inputImageList;
  18. InputImageListInfo? _inputImageListInfo;
  19. Color _borderColor = Colors.grey;
  20. bool _isSelected = false;
  21. List<String> _images = [];
  22. double _height = 0.0;
  23. Widget _child = const Text('请点击此处后选择图片');
  24. @override
  25. initState() {
  26. _initDatas();
  27. super.initState();
  28. }
  29. @override
  30. Widget build(BuildContext context) {
  31. _checkInputImageListInfo();
  32. return SizedBox(
  33. width: PtToPxConverter.ptToPx(
  34. inputImageList!.imageWidth! * inputImageList!.column!),
  35. child: MouseRegion(
  36. cursor: SystemMouseCursors.click,
  37. child: GestureDetector(
  38. onTap: () {
  39. if (_isSelected) {
  40. setState(() {
  41. _borderColor = Colors.grey;
  42. _isSelected = false;
  43. _inputImageListInfo!.isSelected = false;
  44. });
  45. } else {
  46. setState(() {
  47. _borderColor = const Color.fromARGB(255, 64, 159, 248);
  48. _isSelected = true;
  49. _inputImageListInfo!.isSelected = true;
  50. });
  51. }
  52. },
  53. child: Container(
  54. padding: const EdgeInsets.symmetric(vertical: 3),
  55. height: _height,
  56. width: PtToPxConverter.ptToPx(
  57. inputImageList!.imageWidth! * inputImageList!.column!),
  58. alignment: Alignment.center,
  59. margin: MarginConvert.marginConvert(inputImageList!.margin),
  60. decoration: BoxDecoration(
  61. border: _isSelected
  62. ? Border.all(
  63. width: 0.5,
  64. color: _borderColor,
  65. )
  66. : null,
  67. color: Colors.transparent),
  68. child: _child,
  69. ),
  70. ),
  71. ),
  72. );
  73. }
  74. @override
  75. void dispose() {
  76. _inputImageListInfo!.onSelect!.dispose();
  77. super.dispose();
  78. }
  79. Widget _getChild() {
  80. if (_images.isNotEmpty) {
  81. final rowCount = (_images.length / inputImageList!.column!).ceil();
  82. _height = rowCount *
  83. (PtToPxConverter.ptToPx(inputImageList!.imageHeight! + 2)) +
  84. rowCount;
  85. return GridView.builder(
  86. primary: true,
  87. gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
  88. crossAxisCount: inputImageList!.column!,
  89. crossAxisSpacing: 1,
  90. mainAxisSpacing: 1,
  91. childAspectRatio:
  92. inputImageList!.imageWidth! / inputImageList!.imageHeight!,
  93. ),
  94. itemCount: _images.length,
  95. itemBuilder: (BuildContext context, int index) {
  96. return VidImageView(
  97. url: _images[index],
  98. fit: BoxFit.fill,
  99. width: PtToPxConverter.ptToPx(inputImageList!.imageWidth!) - 20,
  100. );
  101. },
  102. );
  103. } else {
  104. _height = PtToPxConverter.ptToPx(inputImageList!.imageHeight!) + 9;
  105. }
  106. return const Text('请点击此处后选择图片');
  107. }
  108. void _addSelectImageListening() {
  109. try {
  110. _inputImageListInfo!.onSelectedChange.addListener((sender, e) {
  111. if (mounted) {
  112. setState(() {
  113. _images = e;
  114. _child = _getChild();
  115. });
  116. }
  117. });
  118. } catch (e) {
  119. print(e);
  120. }
  121. }
  122. void _addOnSelectedListening() {
  123. _inputImageListInfo!.onSelect!.addListener((sender, e) {
  124. setState(() {
  125. if (_images.contains(e)) {
  126. _images.remove(e);
  127. if (_inputImageListInfo!.selectedImages.contains(e)) {
  128. _inputImageListInfo!.selectedImages.remove(e);
  129. }
  130. } else {
  131. _images.add(e);
  132. if (!_inputImageListInfo!.selectedImages.contains(e)) {
  133. _inputImageListInfo!.selectedImages.add(e);
  134. }
  135. }
  136. _child = _getChild();
  137. });
  138. });
  139. }
  140. void _initDatas() {
  141. inputImageList = widget.inputImageList;
  142. _height = PtToPxConverter.ptToPx(inputImageList!.imageHeight) + 6;
  143. final element = ReportInfo.instance.getBlockElement(inputImageList!);
  144. if (element != null) {
  145. _inputImageListInfo = element as InputImageListInfo;
  146. _addOnSelectedListening();
  147. _addSelectImageListening();
  148. }
  149. }
  150. void _checkInputImageListInfo() {
  151. if (_inputImageListInfo != null) {
  152. final imageInfo = ReportInfo.instance
  153. .getBlockElement(widget.inputImageList) as InputImageListInfo;
  154. if (imageInfo != _inputImageListInfo) {
  155. if (_inputImageListInfo != null) {
  156. _inputImageListInfo!.onSelectedChange.dispose();
  157. }
  158. _inputImageListInfo = imageInfo;
  159. _addSelectImageListening();
  160. setState(() {
  161. _images.clear();
  162. _isSelected = false;
  163. _child = _getChild();
  164. });
  165. _addOnSelectedListening();
  166. }
  167. }
  168. }
  169. }