annotation_canvas.dart 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import 'package:fis_measure/interfaces/process/annotations/annotation.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 AnnotationCanvas extends StatefulWidget {
  6. const AnnotationCanvas({Key? key}) : super(key: key);
  7. @override
  8. State<StatefulWidget> createState() => _AnnotationCanvasState();
  9. }
  10. class _AnnotationCanvasState extends State<AnnotationCanvas> {
  11. late final application = Get.find<IApplication>();
  12. IAnnotationItemFeature? feature;
  13. @override
  14. void initState() {
  15. feature = application.activeAnnotationItem?.feature;
  16. application.updateRenderReady.addListener(_onUpdateRenderReady);
  17. super.initState();
  18. }
  19. @override
  20. void dispose() {
  21. application.updateRenderReady.removeListener(_onUpdateRenderReady);
  22. super.dispose();
  23. }
  24. @override
  25. Widget build(BuildContext context) {
  26. final feature = application.activeAnnotationItem?.feature;
  27. if (feature == null) return const SizedBox();
  28. return LayoutBuilder(builder: (context, constraints) {
  29. return 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. void _onUpdateRenderReady(Object sender, void e) {
  41. if (mounted) {
  42. setState(() {
  43. feature = application.activeAnnotationItem?.feature;
  44. });
  45. }
  46. }
  47. }
  48. class _PanelPainter extends CustomPainter {
  49. final IAnnotationItemFeature feature;
  50. _PanelPainter(this.feature);
  51. @override
  52. void paint(Canvas canvas, Size size) {
  53. feature.paint(canvas, size);
  54. }
  55. @override
  56. bool shouldRepaint(covariant _PanelPainter oldDelegate) {
  57. return oldDelegate.hashCode != hashCode;
  58. }
  59. }