|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:page_list_viewport/page_list_viewport.dart'; |
| 3 | + |
| 4 | +class VelocityPlotter extends StatefulWidget { |
| 5 | + const VelocityPlotter({ |
| 6 | + super.key, |
| 7 | + required this.controller, |
| 8 | + required this.max, |
| 9 | + }); |
| 10 | + |
| 11 | + final PageListViewportController controller; |
| 12 | + final Offset max; |
| 13 | + |
| 14 | + @override |
| 15 | + State<VelocityPlotter> createState() => _VelocityPlotterState(); |
| 16 | +} |
| 17 | + |
| 18 | +class _VelocityPlotterState extends State<VelocityPlotter> { |
| 19 | + final _velocityLogicalPoint = <Offset>[]; |
| 20 | + final _velocityVisiblePoints = <Offset>[]; |
| 21 | + final _accelerationLogicalPoints = <Offset>[]; |
| 22 | + final _accelerationVisiblePoints = <Offset>[]; |
| 23 | + final _sampleCount = ValueNotifier(0); |
| 24 | + |
| 25 | + @override |
| 26 | + void initState() { |
| 27 | + super.initState(); |
| 28 | + widget.controller.addListener(_onControllerChanged); |
| 29 | + } |
| 30 | + |
| 31 | + @override |
| 32 | + void didUpdateWidget(covariant VelocityPlotter oldWidget) { |
| 33 | + super.didUpdateWidget(oldWidget); |
| 34 | + if (oldWidget.controller != widget.controller) { |
| 35 | + oldWidget.controller.removeListener(_onControllerChanged); |
| 36 | + widget.controller.addListener(_onControllerChanged); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + @override |
| 41 | + void dispose() { |
| 42 | + super.dispose(); |
| 43 | + widget.controller.removeListener(_onControllerChanged); |
| 44 | + } |
| 45 | + |
| 46 | + Offset? _lastVelocity; |
| 47 | + final _stopwatch = Stopwatch(); |
| 48 | + |
| 49 | + void _onControllerChanged() { |
| 50 | + if (_stopwatch.isRunning == false) { |
| 51 | + _stopwatch.start(); |
| 52 | + } |
| 53 | + if (_stopwatch.elapsedMilliseconds == 0) { |
| 54 | + // No time has passed. We don't want to divide things by zero. |
| 55 | + return; |
| 56 | + } |
| 57 | + final velocity = widget.controller.velocity; |
| 58 | + _velocityLogicalPoint.add(velocity); |
| 59 | + if (_lastVelocity != null) { |
| 60 | + // final acceleration = (velocity - _lastVelocity!) / (_stopwatch.elapsedMilliseconds / 1000); |
| 61 | + final acceleration = widget.controller.acceleration * 100; |
| 62 | + _accelerationLogicalPoints.add(acceleration); |
| 63 | + } |
| 64 | + |
| 65 | + // Increment sample count so that we cause a repaint in the CustomPainter |
| 66 | + _sampleCount.value = _velocityLogicalPoint.length; |
| 67 | + |
| 68 | + _lastVelocity = velocity; |
| 69 | + _stopwatch.reset(); |
| 70 | + } |
| 71 | + |
| 72 | + void _clearPlot() { |
| 73 | + _velocityLogicalPoint.clear(); |
| 74 | + _velocityVisiblePoints.clear(); |
| 75 | + _accelerationLogicalPoints.clear(); |
| 76 | + _accelerationVisiblePoints.clear(); |
| 77 | + _sampleCount.value = 0; |
| 78 | + } |
| 79 | + |
| 80 | + @override |
| 81 | + Widget build(BuildContext context) { |
| 82 | + return RepaintBoundary( |
| 83 | + child: GestureDetector( |
| 84 | + onDoubleTap: _clearPlot, |
| 85 | + child: Stack( |
| 86 | + children: [ |
| 87 | + Positioned.fill( |
| 88 | + child: CustomPaint( |
| 89 | + painter: _PlotterPainter( |
| 90 | + logicalPoints: _accelerationLogicalPoints, |
| 91 | + visiblePoints: _accelerationVisiblePoints, |
| 92 | + max: widget.max, |
| 93 | + color: Colors.red.withOpacity(0.5), |
| 94 | + repaint: _sampleCount, |
| 95 | + ), |
| 96 | + ), |
| 97 | + ), |
| 98 | + Positioned.fill( |
| 99 | + child: CustomPaint( |
| 100 | + painter: _PlotterPainter( |
| 101 | + logicalPoints: _velocityLogicalPoint, |
| 102 | + visiblePoints: _velocityVisiblePoints, |
| 103 | + max: widget.max, |
| 104 | + color: Colors.greenAccent, |
| 105 | + repaint: _sampleCount, |
| 106 | + ), |
| 107 | + ), |
| 108 | + ), |
| 109 | + ], |
| 110 | + ), |
| 111 | + ), |
| 112 | + ); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +class _PlotterPainter extends CustomPainter { |
| 117 | + static const _maxSampleDisplayCount = 300; |
| 118 | + |
| 119 | + _PlotterPainter({ |
| 120 | + required this.max, |
| 121 | + required this.logicalPoints, |
| 122 | + required this.visiblePoints, |
| 123 | + required this.color, |
| 124 | + super.repaint, |
| 125 | + }) { |
| 126 | + pointPainter.color = color; |
| 127 | + linePaint |
| 128 | + ..color = color |
| 129 | + ..strokeWidth = 1 |
| 130 | + ..style = PaintingStyle.stroke; |
| 131 | + } |
| 132 | + |
| 133 | + final Offset max; |
| 134 | + final List<Offset> logicalPoints; |
| 135 | + final List<Offset> visiblePoints; |
| 136 | + final Color color; |
| 137 | + |
| 138 | + final pointPainter = Paint(); |
| 139 | + final linePaint = Paint(); |
| 140 | + |
| 141 | + @override |
| 142 | + void paint(Canvas canvas, Size size) { |
| 143 | + _convertLogicalPointsToPlotPoints(size); |
| 144 | + |
| 145 | + canvas.clipRect(Offset.zero & size); |
| 146 | + |
| 147 | + final horizontalStepSize = size.width / _maxSampleDisplayCount; |
| 148 | + for (var i = 0; i < visiblePoints.length; i += 1) { |
| 149 | + final plotPoint = visiblePoints[i].translate(i * horizontalStepSize, 0); |
| 150 | + final previousPlotPoint = i > 0 ? visiblePoints[i - 1].translate((i - 1) * horizontalStepSize, 0) : null; |
| 151 | + |
| 152 | + // Draw a line connecting previous and current plot point. |
| 153 | + if (previousPlotPoint != null) { |
| 154 | + canvas.drawLine(previousPlotPoint, plotPoint, linePaint); |
| 155 | + } |
| 156 | + |
| 157 | + // Draw the current plot point. |
| 158 | + canvas.drawCircle(plotPoint, 2, pointPainter); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + void _convertLogicalPointsToPlotPoints(Size size) { |
| 163 | + final scaleY = size.height / (max.dy * 2); |
| 164 | + |
| 165 | + for (var i = 0; i < logicalPoints.length; i += 1) { |
| 166 | + final logicalPoint = logicalPoints[i]; |
| 167 | + |
| 168 | + final plotPoint = Offset( |
| 169 | + 0, |
| 170 | + size.height - ((logicalPoint.dy + max.dy) * scaleY), |
| 171 | + ); |
| 172 | + |
| 173 | + visiblePoints.add(plotPoint); |
| 174 | + } |
| 175 | + |
| 176 | + if (visiblePoints.length > _maxSampleDisplayCount) { |
| 177 | + visiblePoints.removeRange(0, visiblePoints.length - _maxSampleDisplayCount); |
| 178 | + } |
| 179 | + |
| 180 | + logicalPoints.clear(); |
| 181 | + } |
| 182 | + |
| 183 | + @override |
| 184 | + bool shouldRepaint(covariant CustomPainter oldDelegate) { |
| 185 | + return false; |
| 186 | + } |
| 187 | +} |
0 commit comments