input_imageList.dart 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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/input_image_list.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/index.dart';
  7. import 'package:flutter/material.dart';
  8. import 'package:fis_jsonrpc/rpc.dart';
  9. import 'package:fis_components/index.dart';
  10. class FInputImageList extends StatefulWidget implements FWidget {
  11. final InputImageList inputImageList;
  12. const FInputImageList(this.inputImageList, {Key? key}) : super(key: key);
  13. @override
  14. State<StatefulWidget> createState() {
  15. return _FInputImageListState();
  16. }
  17. }
  18. class _FInputImageListState extends State<FInputImageList> {
  19. InputImageList? inputImageList;
  20. InputImageListInfo? _inputImageListInfo;
  21. Color _borderColor = Colors.grey;
  22. bool _isSelected = false;
  23. List<String> _images = [];
  24. double _height = 0.0;
  25. FWidget _child = FText(ReportInfo.instance.selectImageHint);
  26. @override
  27. initState() {
  28. _initDatas();
  29. super.initState();
  30. }
  31. @override
  32. FWidget build(BuildContext context) {
  33. _checkInputImageListInfo();
  34. return FSizedBox(
  35. width: PtToPxConverter.ptToPx(
  36. inputImageList!.imageWidth! * inputImageList!.column!),
  37. child: FMouseRegion(
  38. cursor: SystemMouseCursors.click,
  39. child: FGestureDetector(
  40. onTap: () {
  41. if (_isSelected) {
  42. setState(() {
  43. _borderColor = Colors.grey;
  44. _isSelected = false;
  45. _inputImageListInfo!.isSelected = false;
  46. });
  47. } else {
  48. setState(() {
  49. _borderColor = const Color.fromARGB(255, 64, 159, 248);
  50. _isSelected = true;
  51. _inputImageListInfo!.isSelected = true;
  52. });
  53. }
  54. },
  55. child: FContainer(
  56. padding: const EdgeInsets.symmetric(vertical: 3),
  57. height: _height,
  58. width: PtToPxConverter.ptToPx(
  59. inputImageList!.imageWidth! * inputImageList!.column!),
  60. alignment: Alignment.center,
  61. margin: MarginConvert.marginConvert(inputImageList!.margin),
  62. decoration: BoxDecoration(
  63. border: _isSelected
  64. ? Border.all(
  65. width: 0.5,
  66. color: _borderColor,
  67. )
  68. : null,
  69. color: Colors.transparent),
  70. child: _child,
  71. ),
  72. ),
  73. ),
  74. );
  75. }
  76. @override
  77. void dispose() {
  78. _inputImageListInfo!.onSelect!.dispose();
  79. super.dispose();
  80. }
  81. FWidget _getChild() {
  82. if (_images.isNotEmpty) {
  83. final rowCount = (_images.length / inputImageList!.column!).ceil();
  84. _height = rowCount *
  85. (PtToPxConverter.ptToPx(inputImageList!.imageHeight! + 2)) +
  86. rowCount;
  87. return FGridView.builder(
  88. primary: true,
  89. gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
  90. crossAxisCount: inputImageList!.column!,
  91. crossAxisSpacing: 1,
  92. mainAxisSpacing: 1,
  93. childAspectRatio:
  94. inputImageList!.imageWidth! / inputImageList!.imageHeight!,
  95. ),
  96. itemCount: _images.length,
  97. itemBuilder: (BuildContext context, int index) {
  98. return ContentImage(
  99. fileDataType: RemedicalFileDataTypeEnum.Image,
  100. terminalImage: TerminalImage(
  101. coverImageUrl: _images[index],
  102. previewUrl: _images[index],
  103. imageUrl: _images[index],
  104. ),
  105. onChangeImage: () {
  106. _inputImageListInfo!.onSelect!.emit(this, _images[index]);
  107. },
  108. );
  109. },
  110. );
  111. } else {
  112. _height = PtToPxConverter.ptToPx(inputImageList!.imageHeight!) + 9;
  113. }
  114. return FText(ReportInfo.instance.selectImageHint);
  115. }
  116. void _addSelectImageListening() {
  117. try {
  118. _inputImageListInfo!.onSelectedChange.addListener((sender, e) {
  119. if (mounted) {
  120. setState(() {
  121. _images = e;
  122. _child = _getChild();
  123. });
  124. }
  125. });
  126. } catch (e) {
  127. print(e);
  128. }
  129. }
  130. void _addOnSelectedListening() {
  131. _inputImageListInfo!.onSelect!.dispose();
  132. _inputImageListInfo!.onSelect!.addListener((sender, e) {
  133. if (mounted) {
  134. setState(() {
  135. if (_images.contains(e)) {
  136. _images.remove(e);
  137. if (_inputImageListInfo!.selectedImages.contains(e)) {
  138. _inputImageListInfo!.selectedImages.remove(e);
  139. }
  140. } else {
  141. _images.add(e);
  142. if (!_inputImageListInfo!.selectedImages.contains(e)) {
  143. _inputImageListInfo!.selectedImages.add(e);
  144. }
  145. }
  146. _child = _getChild();
  147. });
  148. }
  149. });
  150. }
  151. void _initDatas() {
  152. inputImageList = widget.inputImageList;
  153. _height = PtToPxConverter.ptToPx(inputImageList!.imageHeight) + 6;
  154. final element = ReportInfo.instance.getBlockElement(inputImageList!);
  155. if (element != null) {
  156. _inputImageListInfo = element as InputImageListInfo;
  157. // _addOnSelectedListening();
  158. _addSelectImageListening();
  159. }
  160. }
  161. void _checkInputImageListInfo() {
  162. if (_inputImageListInfo != null) {
  163. final imageInfo = ReportInfo.instance
  164. .getBlockElement(widget.inputImageList) as InputImageListInfo;
  165. if (imageInfo != _inputImageListInfo) {
  166. if (_inputImageListInfo != null) {
  167. _inputImageListInfo!.onSelectedChange.dispose();
  168. }
  169. _inputImageListInfo = imageInfo;
  170. _addSelectImageListening();
  171. _addOnSelectedListening();
  172. if (_images != _inputImageListInfo!.selectedImages) {
  173. _images = _inputImageListInfo!.selectedImages;
  174. }
  175. _isSelected = false;
  176. _child = _getChild();
  177. } else {
  178. _addOnSelectedListening();
  179. if (_images != _inputImageListInfo!.selectedImages) {
  180. setState(() {
  181. _images = _inputImageListInfo!.selectedImages;
  182. _child = _getChild();
  183. });
  184. }
  185. }
  186. }
  187. }
  188. }