active_canvas.dart 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. if (feature.checkCanPaint()) {
  61. feature.paint(canvas, size);
  62. }
  63. }
  64. @override
  65. bool shouldRepaint(covariant _PanelPainter oldDelegate) {
  66. return oldDelegate.hashCode != hashCode;
  67. }
  68. }