stock_points_widget.dart 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/services.dart';
  3. import 'package:lszlgl/base/base_lifecycle_state.dart';
  4. import 'dart:ui' as ui;
  5. import 'package:lszlgl/model/rsp/stock_points_rsp.dart';
  6. import 'package:lszlgl/network/my_api.dart';
  7. class StockPointsWidget extends StatefulWidget {
  8. final double width;
  9. final double length;
  10. final double height;
  11. final double kWidth;
  12. final double kHeight;
  13. const StockPointsWidget(
  14. {super.key,
  15. required this.width,
  16. required this.length,
  17. required this.height,
  18. required this.kWidth, required this.kHeight});
  19. @override
  20. State<StockPointsWidget> createState() => _StockPointsWidgetState();
  21. }
  22. class _StockPointsWidgetState extends BaseLifecycleState<StockPointsWidget> {
  23. List<StockPointsRsp>? listData;
  24. double kLength = 0; // 画布长度
  25. double bili = 0; // 实际坐标米数 转换到屏幕上的坐标点 比例
  26. double sWidth = 0;
  27. @override
  28. void onInit() {
  29. // 强制横屏
  30. SystemChrome.setPreferredOrientations([
  31. DeviceOrientation.landscapeLeft,
  32. DeviceOrientation.landscapeRight,
  33. ]);
  34. getStockPoints();
  35. }
  36. @override
  37. void onDestroy() {
  38. SystemChrome.setPreferredOrientations([
  39. DeviceOrientation.portraitUp,
  40. DeviceOrientation.portraitDown,
  41. ]);
  42. }
  43. void getStockPoints() async {
  44. var res = await MyApi.get().stockPoints(
  45. width: widget.width,
  46. length: widget.length,
  47. height: widget.height,
  48. // partitionSize: 200,
  49. randomOffset: true,
  50. offsetRange: 0.5,
  51. );
  52. if(widget.width<=30){
  53. kLength = (widget.length / widget.width) * widget.kWidth;
  54. bili = widget.kWidth / widget.width;
  55. sWidth = widget.kWidth;
  56. }else{
  57. kLength = (widget.length / widget.width) * widget.kHeight;
  58. bili = widget.kHeight / widget.width;
  59. sWidth = widget.kHeight;
  60. }
  61. setState(() {
  62. listData = res.data;
  63. });
  64. }
  65. @override
  66. Widget build(BuildContext context) {
  67. return myScaffold(
  68. child: Column(
  69. children: [
  70. myAppBar(title: '扦样布点示意图',toolbarHeight: 32),
  71. Expanded(
  72. child: Container(
  73. color: Colors.white,
  74. width: double.infinity,
  75. child: listData != null
  76. ? SingleChildScrollView(
  77. padding: const EdgeInsets.symmetric(vertical: 20,horizontal: 10),
  78. child: Column(
  79. mainAxisSize: MainAxisSize.min,
  80. children: [
  81. CustomPaint(
  82. size: Size(sWidth, kLength),
  83. painter: CustomXYPainter(bili: bili, xyzList: listData!),
  84. ),
  85. // const SizedBox(height: 12),
  86. Text('房式仓 ${widget.width}m * ${widget.length}m',style:const TextStyle(fontSize: 22),),
  87. const SizedBox(height: 42),
  88. CustomPaint(
  89. size: Size((widget.kWidth/3)*2, (widget.kWidth/3)*2),
  90. painter: CustomZPainter(xyzList: listData!,height: widget.height),
  91. ),
  92. const SizedBox(height: 12),
  93. Text('房式仓分层取样示意图-高度${widget.height}m',style:const TextStyle(fontSize: 22)),
  94. ],
  95. ),
  96. )
  97. :const Center(child: CircularProgressIndicator()),
  98. )
  99. ),
  100. ],
  101. )
  102. );
  103. }
  104. }
  105. class CustomXYPainter extends CustomPainter {
  106. final double bili;
  107. final List<StockPointsRsp> xyzList;
  108. CustomXYPainter({super.repaint, required this.bili, required this.xyzList});
  109. @override
  110. void paint(Canvas canvas, Size size) {
  111. Paint p = Paint();
  112. p.style = PaintingStyle.stroke;
  113. p.strokeWidth = 1.6;
  114. p.color = Colors.black;
  115. // 实际仓库
  116. Rect rect = Rect.fromLTRB(0, 0, size.width, size.height);
  117. canvas.drawRect(rect, p);
  118. // 内部灰色 0.5米 扦样区域
  119. p.strokeWidth = 1;
  120. p.color = Colors.grey;
  121. double pad = 0.5 * bili;
  122. Rect rectSmall = Rect.fromLTWH(pad, pad, size.width - bili, size.height - bili);
  123. canvas.drawRect(rectSmall, p);
  124. StockPointsRsp first = xyzList.first;
  125. // int c = first.partitionsY!.toInt();
  126. int c = first.partitionY!.toInt();
  127. if(c>1){ // y方向分区
  128. for(int i=1; i< c; i++){
  129. canvas.drawLine(Offset(first.partitionWidth! *i *bili , 0), Offset(first.partitionWidth! *i *bili, size.height), p);
  130. }
  131. }
  132. // 随机点
  133. List<Offset> pl = [];
  134. for (int i = 0; i < xyzList.length; i++) {
  135. StockPointsRsp point = xyzList[i];
  136. double offX = point.x! * bili;
  137. double offY = point.y! * bili;
  138. pl.add(Offset(offX, offY));
  139. var textPainter = TextPainter(
  140. text: TextSpan(text: '$i', style: const TextStyle(fontSize: 8, color: Colors.black)),
  141. textDirection: TextDirection.ltr,
  142. );
  143. textPainter.layout(); // 进行布局
  144. if (offX + 16 >= size.width - 0.5 * bili) {
  145. textPainter.paint(canvas, Offset(offX - 14, offY - 4));
  146. } else {
  147. textPainter.paint(canvas, Offset(offX + 4, offY - 4));
  148. }
  149. }
  150. p.strokeWidth = 4;
  151. p.color = Colors.red;
  152. canvas.drawPoints(ui.PointMode.points, pl, p);
  153. }
  154. @override
  155. bool shouldRepaint(covariant CustomPainter oldDelegate) => this != oldDelegate;
  156. }
  157. ///
  158. class CustomZPainter extends CustomPainter {
  159. final List<StockPointsRsp> xyzList;
  160. final double height; // 仓库高度
  161. CustomZPainter({super.repaint, required this.xyzList, required this.height});
  162. @override
  163. void paint(Canvas canvas, Size size) {
  164. Paint p = Paint();
  165. p.style = PaintingStyle.stroke;
  166. p.strokeWidth = 1.6;
  167. p.color = Colors.black;
  168. canvas.drawLine(const Offset(0, 0), Offset(size.width, 0), p);
  169. canvas.drawLine(const Offset(54, 0), Offset(54, size.height), p);
  170. canvas.drawLine(Offset(0, size.height), Offset(size.width, size.height), p);
  171. // 右边高度
  172. p.strokeWidth = 1;
  173. p.color = Colors.grey;
  174. canvas.drawLine(Offset(size.width-20, 0), Offset(size.width-20, size.height), p);
  175. var textPainter = TextPainter(
  176. text: TextSpan(text: '$height米', style: const TextStyle(fontSize: 10, color: Colors.black)),
  177. textDirection: TextDirection.ltr,
  178. );
  179. textPainter.layout();
  180. textPainter.paint(canvas, Offset(size.width-12, (size.height/2)-7));
  181. var textPainterBot = TextPainter(
  182. text:const TextSpan(text: '地坪', style: TextStyle(fontSize: 10, color: Colors.black)),
  183. textDirection: TextDirection.ltr,
  184. );
  185. textPainterBot.layout();
  186. textPainterBot.paint(canvas, Offset(size.width+2, size.height-8));
  187. var textPainterTop = TextPainter(
  188. text:const TextSpan(text: '粮面', style: TextStyle(fontSize: 10, color: Colors.black)),
  189. textDirection: TextDirection.ltr,
  190. );
  191. textPainterTop.layout();
  192. textPainterTop.paint(canvas, Offset(size.width+2, -8));
  193. p.strokeCap= StrokeCap.round;
  194. p.strokeWidth = 6;
  195. p.color = Colors.black;
  196. List<Offset> pl = [];
  197. double bili = size.height / height ;
  198. List layers = xyzList.first.layers!;
  199. layers.sort();
  200. for(int i = 0; i < layers.length; i++){
  201. double lay = layers[i]!;
  202. String mi = (height-lay).toStringAsFixed(2);
  203. double offY = double.parse((lay *bili).toStringAsFixed(2));
  204. pl.add(Offset(54, offY));
  205. var textPainterTop = TextPainter(
  206. text:TextSpan(text: '第${i+1}层$mi米', style:const TextStyle(fontSize: 8, color: Colors.black)),
  207. textDirection: TextDirection.ltr,
  208. );
  209. textPainterTop.layout();
  210. textPainterTop.paint(canvas, Offset(0, offY-7));
  211. }
  212. canvas.drawPoints(ui.PointMode.points, pl, p);
  213. }
  214. @override
  215. bool shouldRepaint(covariant CustomPainter oldDelegate) {
  216. return false;
  217. }
  218. }