white_board_painter.dart 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import 'package:fis_lib_business_components/components/white_board/structure.dart';
  2. import 'package:flutter/material.dart';
  3. /// 白板 Canvas
  4. class WhiteBoardPainter extends CustomPainter {
  5. final WhiteBoardPen pen;
  6. final double width;
  7. final double height;
  8. WhiteBoardPainter({required this.pen, this.width = 0.0, this.height = 0.0})
  9. : super(repaint: pen);
  10. final Paint _paint = Paint();
  11. @override
  12. void paint(Canvas canvas, Size size) {
  13. /// 遍历每一根线,绘制
  14. for (var line in pen.lines) {
  15. line.paint(
  16. canvas,
  17. _paint,
  18. width,
  19. height,
  20. );
  21. }
  22. }
  23. @override
  24. bool shouldRepaint(covariant WhiteBoardPainter oldDelegate) {
  25. bool needRepaint = oldDelegate.pen != pen;
  26. return needRepaint;
  27. }
  28. }
  29. /// 白板画笔
  30. class WhiteBoardPen extends ChangeNotifier {
  31. final List<Line> _lines = [];
  32. List<Line> get lines => _lines;
  33. Line get activeLine => _lines.singleWhere(
  34. (element) => element.state == PaintState.doing,
  35. orElse: () => Line(userId: ''),
  36. );
  37. void pushLine(Line line) {
  38. _lines.add(line);
  39. }
  40. void pushPoint(Point point) {
  41. activeLine.points.add(point);
  42. if (activeLine.paintType == PaintType.straightLine) {
  43. activeLine.points = [activeLine.points.first, activeLine.points.last];
  44. }
  45. notifyListeners();
  46. }
  47. void doneLine() {
  48. activeLine.state = PaintState.done;
  49. notifyListeners();
  50. }
  51. void clear() {
  52. for (var line in _lines) {
  53. line.points.clear();
  54. }
  55. _lines.clear();
  56. notifyListeners();
  57. }
  58. void clearCurrectUserLines(String userId) {
  59. for (int i = 0; i < _lines.length; i++) {
  60. if (_lines[i].userId == userId) {
  61. _lines[i].points.clear();
  62. }
  63. }
  64. _lines.removeWhere((element) => element.userId == userId);
  65. notifyListeners();
  66. }
  67. void removeEmpty() {
  68. _lines.removeWhere((element) => element.points.isEmpty);
  69. }
  70. }