active_canvas.dart 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import 'package:fis_measure/interfaces/process/items/item_feature.dart';
  2. import 'package:fis_measure/interfaces/process/workspace/application.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:get/get.dart';
  5. class MeasureActiveCanvasPanel extends StatefulWidget {
  6. const MeasureActiveCanvasPanel({Key? key}) : super(key: key);
  7. @override
  8. State<StatefulWidget> createState() => _ActiveCanvasPanelState();
  9. }
  10. class _ActiveCanvasPanelState extends State<MeasureActiveCanvasPanel> {
  11. late final application = Get.find<IApplication>();
  12. IMeasureItemFeature? feature;
  13. @override
  14. void initState() {
  15. feature = application.activeMeasureItem?.feature;
  16. _addListenrs();
  17. super.initState();
  18. }
  19. @override
  20. void dispose() {
  21. _removeListenrs();
  22. super.dispose();
  23. }
  24. @override
  25. Widget build(BuildContext context) {
  26. if (feature == null) return const SizedBox();
  27. return LayoutBuilder(builder: (context, constraints) {
  28. return ClipRect(
  29. child: SizedBox(
  30. width: constraints.maxWidth,
  31. height: constraints.maxHeight,
  32. child: RepaintBoundary(
  33. child: CustomPaint(
  34. painter: _PanelPainter(feature!),
  35. ),
  36. ),
  37. ),
  38. );
  39. });
  40. }
  41. void _onMeasureRerenderReady(Object sender, void e) {
  42. if (mounted) {
  43. setState(() {
  44. feature = application.activeMeasureItem?.feature;
  45. });
  46. }
  47. }
  48. void _addListenrs() {
  49. application.updateRenderReady.addListener(_onMeasureRerenderReady);
  50. }
  51. void _removeListenrs() {
  52. application.updateRenderReady.removeListener(_onMeasureRerenderReady);
  53. }
  54. }
  55. class _PanelPainter extends CustomPainter {
  56. final IMeasureItemFeature feature;
  57. _PanelPainter(this.feature);
  58. @override
  59. void paint(Canvas canvas, Size size) {
  60. feature.paint(canvas, size);
  61. }
  62. @override
  63. bool shouldRepaint(covariant _PanelPainter oldDelegate) {
  64. return oldDelegate.hashCode != hashCode;
  65. }
  66. }