12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- import 'package:fis_lib_business_components/components/white_board/structure.dart';
- import 'package:flutter/material.dart';
- /// 白板 Canvas
- class WhiteBoardPainter extends CustomPainter {
- final WhiteBoardPen pen;
- final double width;
- final double height;
- WhiteBoardPainter({required this.pen, this.width = 0.0, this.height = 0.0})
- : super(repaint: pen);
- final Paint _paint = Paint();
- @override
- void paint(Canvas canvas, Size size) {
- /// 遍历每一根线,绘制
- for (var line in pen.lines) {
- line.paint(
- canvas,
- _paint,
- width,
- height,
- );
- }
- }
- @override
- bool shouldRepaint(covariant WhiteBoardPainter oldDelegate) {
- bool needRepaint = oldDelegate.pen != pen;
- return needRepaint;
- }
- }
- /// 白板画笔
- class WhiteBoardPen extends ChangeNotifier {
- final List<Line> _lines = [];
- List<Line> get lines => _lines;
- Line get activeLine => _lines.singleWhere(
- (element) => element.state == PaintState.doing,
- orElse: () => Line(userId: ''),
- );
- void pushLine(Line line) {
- _lines.add(line);
- }
- void pushPoint(Point point) {
- activeLine.points.add(point);
- if (activeLine.paintType == PaintType.straightLine) {
- activeLine.points = [activeLine.points.first, activeLine.points.last];
- }
- notifyListeners();
- }
- void doneLine() {
- activeLine.state = PaintState.done;
- notifyListeners();
- }
- void clear() {
- for (var line in _lines) {
- line.points.clear();
- }
- _lines.clear();
- notifyListeners();
- }
- void clearCurrectUserLines(String userId) {
- for (int i = 0; i < _lines.length; i++) {
- if (_lines[i].userId == userId) {
- _lines[i].points.clear();
- }
- }
- _lines.removeWhere((element) => element.userId == userId);
- notifyListeners();
- }
- void removeEmpty() {
- _lines.removeWhere((element) => element.points.isEmpty);
- }
- }
|