123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- import 'dart:math';
- import 'package:fis_measure/interfaces/date_types/point.dart';
- import 'package:fis_measure/interfaces/process/standard_line/calibration.dart';
- import 'package:fis_measure/utils/canvas.dart';
- import 'package:fis_measure/values/colors.dart';
- import 'package:flutter/material.dart';
- /// 参考线画布
- ///
- /// [controller] 校准线控制器
- class StandardLineCalibrationCanvas extends StatefulWidget {
- const StandardLineCalibrationCanvas(this.controller, {Key? key})
- : super(key: key);
- final IStandardLineCalibrationController controller;
- @override
- State<StandardLineCalibrationCanvas> createState() => _CanvasState();
- }
- class _CanvasState extends State<StandardLineCalibrationCanvas> {
- late IStandardLineCalibrationController controller;
- @override
- void initState() {
- controller = widget.controller;
- controller.updateReady.addListener(_onUpdateReady);
- super.initState();
- }
- @override
- void dispose() {
- controller.updateReady.removeListener(_onUpdateReady);
- super.dispose();
- }
- @override
- Widget build(BuildContext context) {
- return LayoutBuilder(builder: (context, constraints) {
- CustomPainter painter;
- if (controller.isEditing) {
- painter = _EditingPainter(controller.drawPoints);
- } else {
- painter = _ViewLinePainter(
- length: controller.displayLengthInPhysics,
- bottom: 3,
- );
- }
- return ClipRect(
- child: SizedBox(
- width: constraints.maxWidth,
- height: constraints.maxHeight,
- child: RepaintBoundary(
- child: CustomPaint(
- painter: painter,
- ),
- ),
- ),
- );
- });
- }
- void _onUpdateReady(Object sender, void e) {
- if (controller.editState == StandardLineCalibrationEditState.drawing) {
- setState(() {});
- }
- }
- }
- class _EditingPainter extends CustomPainter {
- // ignore: constant_identifier_names
- static const double C_VERTEX_SIZE = 10;
- _EditingPainter(this.points);
- final List<DPoint> points;
- final _paintPan = Paint()
- ..color = MeasureColors.ActiveCaliper
- ..style = PaintingStyle.stroke
- ..strokeWidth = 2;
- @override
- void paint(Canvas canvas, Size size) {
- if (points.length < 2) return;
- final startOffset = points[0].scale2Size(size).toOffset();
- final endOffset = points[1].scale2Size(size).toOffset();
- canvas.drawVertex(startOffset, C_VERTEX_SIZE, active: true);
- canvas.drawLine(startOffset, endOffset, _paintPan);
- canvas.drawVertex(endOffset, C_VERTEX_SIZE, active: true);
- }
- @override
- bool shouldRepaint(covariant _EditingPainter oldDelegate) {
- return this != oldDelegate;
- }
- }
- class _ViewLinePainter extends CustomPainter {
- // ignore: constant_identifier_names
- static const C_MARK_HEIGHT = 2;
- _ViewLinePainter({
- required this.length,
- required this.bottom,
- this.physicalLength = 4,
- });
- final double length;
- final double bottom;
- final int physicalLength;
- final _paintPan = Paint()
- ..color = Colors.red //MeasureColors.Primary
- ..style = PaintingStyle.stroke
- ..strokeWidth = 2;
- /// 刻度文案【4cm】预留的宽度
- static const double _textWidth = 80;
- @override
- void paint(Canvas canvas, Size size) {
- //画布可容纳的最大刻度数
- final fittedPhysicalLength = (size.width - _textWidth) ~/ length;
- //如果画布可容纳的最大刻度数小于预设刻度数,则需要缩刻度数,防止标尺超出图像
- final _physicalLength = min(fittedPhysicalLength, physicalLength);
- final markHeight = (C_MARK_HEIGHT / 100) * size.height;
- final lineLength = length * _physicalLength;
- final left = (size.width - lineLength) / 2;
- final top = (100 - bottom) / 100 * size.height;
- Offset position = Offset(left, top);
- for (var i = 0; i < _physicalLength; i++) {
- final p1 = position.translate(length * i, 0);
- final p2 = p1.translate(length, 0);
- final mark = p2.translate(0, -markHeight);
- canvas.drawLine(p1, p2, _paintPan);
- canvas.drawLine(p2, mark, _paintPan);
- if (i == 0) {
- canvas.drawLine(p1, p1.translate(0, -markHeight), _paintPan);
- }
- }
- final tipSize = markHeight * 1.1;
- final tipPosition = Offset(left + lineLength + markHeight, top - tipSize);
- canvas.drawText(
- '${_physicalLength}cm',
- tipPosition,
- style: TextStyle(
- fontSize: tipSize,
- fontWeight: FontWeight.bold,
- color: Colors.red,
- ),
- );
- }
- @override
- bool shouldRepaint(covariant CustomPainter oldDelegate) {
- return false;
- }
- }
|