Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | import 'dart:async'; |
Greg Spencer | 9749f59 | 2019-09-25 14:49:22 -0700 | [diff] [blame] | 6 | import 'dart:io'; |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 7 | |
Greg Spencer | 9749f59 | 2019-09-25 14:49:22 -0700 | [diff] [blame] | 8 | import 'package:flutter/foundation.dart'; |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 9 | import 'package:flutter/gestures.dart'; |
| 10 | import 'package:flutter/material.dart'; |
| 11 | |
| 12 | enum _DragTarget { |
| 13 | start, |
| 14 | end |
| 15 | } |
| 16 | |
| 17 | // How close a drag's start position must be to the target point. This is |
| 18 | // a distance squared. |
| 19 | const double _kTargetSlop = 2500.0; |
| 20 | |
| 21 | // Used by the Painter classes. |
| 22 | const double _kPointRadius = 6.0; |
| 23 | |
| 24 | class _DragHandler extends Drag { |
| 25 | _DragHandler(this.onUpdate, this.onCancel, this.onEnd); |
| 26 | |
| 27 | final GestureDragUpdateCallback onUpdate; |
| 28 | final GestureDragCancelCallback onCancel; |
| 29 | final GestureDragEndCallback onEnd; |
| 30 | |
| 31 | @override |
Alexandre Ardhuin | c02b6a8 | 2018-02-02 23:27:29 +0100 | [diff] [blame] | 32 | void update(DragUpdateDetails details) { |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 33 | onUpdate(details); |
| 34 | } |
| 35 | |
| 36 | @override |
Alexandre Ardhuin | c02b6a8 | 2018-02-02 23:27:29 +0100 | [diff] [blame] | 37 | void cancel() { |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 38 | onCancel(); |
| 39 | } |
| 40 | |
| 41 | @override |
Alexandre Ardhuin | c02b6a8 | 2018-02-02 23:27:29 +0100 | [diff] [blame] | 42 | void end(DragEndDetails details) { |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 43 | onEnd(details); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | class _IgnoreDrag extends Drag { |
| 48 | } |
| 49 | |
| 50 | class _PointDemoPainter extends CustomPainter { |
| 51 | _PointDemoPainter({ |
| 52 | Animation<double> repaint, |
Alexandre Ardhuin | 387f885 | 2019-03-01 08:17:55 +0100 | [diff] [blame] | 53 | this.arc, |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 54 | }) : _repaint = repaint, super(repaint: repaint); |
| 55 | |
| 56 | final MaterialPointArcTween arc; |
Adam Barth | 7be9115 | 2017-04-19 09:30:43 -0700 | [diff] [blame] | 57 | final Animation<double> _repaint; |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 58 | |
Ian Hickson | bf017b7 | 2017-04-12 15:06:12 -0700 | [diff] [blame] | 59 | void drawPoint(Canvas canvas, Offset point, Color color) { |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 60 | final Paint paint = Paint() |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 61 | ..color = color.withOpacity(0.25) |
| 62 | ..style = PaintingStyle.fill; |
| 63 | canvas.drawCircle(point, _kPointRadius, paint); |
| 64 | paint |
| 65 | ..color = color |
| 66 | ..style = PaintingStyle.stroke |
| 67 | ..strokeWidth = 2.0; |
| 68 | canvas.drawCircle(point, _kPointRadius + 1.0, paint); |
| 69 | } |
| 70 | |
| 71 | @override |
| 72 | void paint(Canvas canvas, Size size) { |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 73 | final Paint paint = Paint(); |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 74 | |
| 75 | if (arc.center != null) |
Alexandre Ardhuin | 578ca0a | 2017-03-21 23:14:55 +0100 | [diff] [blame] | 76 | drawPoint(canvas, arc.center, Colors.grey.shade400); |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 77 | |
| 78 | paint |
Hans Muller | afc0550 | 2016-09-02 16:13:09 -0700 | [diff] [blame] | 79 | ..isAntiAlias = false // Work-around for github.com/flutter/flutter/issues/5720 |
Alexandre Ardhuin | 578ca0a | 2017-03-21 23:14:55 +0100 | [diff] [blame] | 80 | ..color = Colors.green.withOpacity(0.25) |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 81 | ..strokeWidth = 4.0 |
| 82 | ..style = PaintingStyle.stroke; |
| 83 | if (arc.center != null && arc.radius != null) |
| 84 | canvas.drawCircle(arc.center, arc.radius, paint); |
| 85 | else |
| 86 | canvas.drawLine(arc.begin, arc.end, paint); |
| 87 | |
Alexandre Ardhuin | 578ca0a | 2017-03-21 23:14:55 +0100 | [diff] [blame] | 88 | drawPoint(canvas, arc.begin, Colors.green); |
| 89 | drawPoint(canvas, arc.end, Colors.red); |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 90 | |
| 91 | paint |
Alexandre Ardhuin | 578ca0a | 2017-03-21 23:14:55 +0100 | [diff] [blame] | 92 | ..color = Colors.green |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 93 | ..style = PaintingStyle.fill; |
| 94 | canvas.drawCircle(arc.lerp(_repaint.value), _kPointRadius, paint); |
| 95 | } |
| 96 | |
| 97 | @override |
Ian Hickson | bf017b7 | 2017-04-12 15:06:12 -0700 | [diff] [blame] | 98 | bool hitTest(Offset position) { |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 99 | return (arc.begin - position).distanceSquared < _kTargetSlop |
| 100 | || (arc.end - position).distanceSquared < _kTargetSlop; |
| 101 | } |
| 102 | |
| 103 | @override |
| 104 | bool shouldRepaint(_PointDemoPainter oldPainter) => arc != oldPainter.arc; |
| 105 | } |
| 106 | |
| 107 | class _PointDemo extends StatefulWidget { |
Alexandre Ardhuin | 9541848 | 2017-04-21 23:09:42 +0200 | [diff] [blame] | 108 | const _PointDemo({ Key key, this.controller }) : super(key: key); |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 109 | |
| 110 | final AnimationController controller; |
| 111 | |
| 112 | @override |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 113 | _PointDemoState createState() => _PointDemoState(); |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | class _PointDemoState extends State<_PointDemo> { |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 117 | final GlobalKey _painterKey = GlobalKey(); |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 118 | |
| 119 | CurvedAnimation _animation; |
| 120 | _DragTarget _dragTarget; |
Hans Muller | f27fa0e | 2016-09-01 14:07:22 -0700 | [diff] [blame] | 121 | Size _screenSize; |
Ian Hickson | bf017b7 | 2017-04-12 15:06:12 -0700 | [diff] [blame] | 122 | Offset _begin; |
| 123 | Offset _end; |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 124 | |
| 125 | @override |
| 126 | void initState() { |
| 127 | super.initState(); |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 128 | _animation = CurvedAnimation(parent: widget.controller, curve: Curves.fastOutSlowIn); |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | @override |
| 132 | void dispose() { |
xster | 89a7fdf | 2017-04-10 18:32:24 -0700 | [diff] [blame] | 133 | widget.controller.value = 0.0; |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 134 | super.dispose(); |
| 135 | } |
| 136 | |
Ian Hickson | bf017b7 | 2017-04-12 15:06:12 -0700 | [diff] [blame] | 137 | Drag _handleOnStart(Offset position) { |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 138 | // TODO(hansmuller): allow the user to drag both points at the same time. |
| 139 | if (_dragTarget != null) |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 140 | return _IgnoreDrag(); |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 141 | |
| 142 | final RenderBox box = _painterKey.currentContext.findRenderObject(); |
| 143 | final double startOffset = (box.localToGlobal(_begin) - position).distanceSquared; |
| 144 | final double endOffset = (box.localToGlobal(_end) - position).distanceSquared; |
| 145 | setState(() { |
| 146 | if (startOffset < endOffset && startOffset < _kTargetSlop) |
| 147 | _dragTarget = _DragTarget.start; |
| 148 | else if (endOffset < _kTargetSlop) |
| 149 | _dragTarget = _DragTarget.end; |
| 150 | else |
| 151 | _dragTarget = null; |
| 152 | }); |
| 153 | |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 154 | return _DragHandler(_handleDragUpdate, _handleDragCancel, _handleDragEnd); |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Alexandre Ardhuin | c02b6a8 | 2018-02-02 23:27:29 +0100 | [diff] [blame] | 157 | void _handleDragUpdate(DragUpdateDetails details) { |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 158 | switch (_dragTarget) { |
| 159 | case _DragTarget.start: |
| 160 | setState(() { |
| 161 | _begin = _begin + details.delta; |
| 162 | }); |
| 163 | break; |
| 164 | case _DragTarget.end: |
| 165 | setState(() { |
| 166 | _end = _end + details.delta; |
| 167 | }); |
| 168 | break; |
| 169 | } |
| 170 | } |
| 171 | |
Alexandre Ardhuin | c02b6a8 | 2018-02-02 23:27:29 +0100 | [diff] [blame] | 172 | void _handleDragCancel() { |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 173 | _dragTarget = null; |
xster | 89a7fdf | 2017-04-10 18:32:24 -0700 | [diff] [blame] | 174 | widget.controller.value = 0.0; |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Alexandre Ardhuin | c02b6a8 | 2018-02-02 23:27:29 +0100 | [diff] [blame] | 177 | void _handleDragEnd(DragEndDetails details) { |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 178 | _dragTarget = null; |
| 179 | } |
| 180 | |
| 181 | @override |
| 182 | Widget build(BuildContext context) { |
Hans Muller | d0e72d6 | 2016-08-26 15:19:46 -0700 | [diff] [blame] | 183 | final Size screenSize = MediaQuery.of(context).size; |
Hans Muller | f27fa0e | 2016-09-01 14:07:22 -0700 | [diff] [blame] | 184 | if (_screenSize == null || _screenSize != screenSize) { |
| 185 | _screenSize = screenSize; |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 186 | _begin = Offset(screenSize.width * 0.5, screenSize.height * 0.2); |
| 187 | _end = Offset(screenSize.width * 0.1, screenSize.height * 0.4); |
Hans Muller | f27fa0e | 2016-09-01 14:07:22 -0700 | [diff] [blame] | 188 | } |
Hans Muller | d0e72d6 | 2016-08-26 15:19:46 -0700 | [diff] [blame] | 189 | |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 190 | final MaterialPointArcTween arc = MaterialPointArcTween(begin: _begin, end: _end); |
| 191 | return RawGestureDetector( |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 192 | behavior: _dragTarget == null ? HitTestBehavior.deferToChild : HitTestBehavior.opaque, |
Adam Barth | 8a82332 | 2016-09-29 08:59:43 -0700 | [diff] [blame] | 193 | gestures: <Type, GestureRecognizerFactory>{ |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 194 | ImmediateMultiDragGestureRecognizer: GestureRecognizerFactoryWithHandlers<ImmediateMultiDragGestureRecognizer>( |
| 195 | () => ImmediateMultiDragGestureRecognizer(), |
Ian Hickson | 46b316c | 2017-06-07 18:04:46 -0700 | [diff] [blame] | 196 | (ImmediateMultiDragGestureRecognizer instance) { |
| 197 | instance |
| 198 | ..onStart = _handleOnStart; |
| 199 | }, |
| 200 | ), |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 201 | }, |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 202 | child: ClipRect( |
| 203 | child: CustomPaint( |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 204 | key: _painterKey, |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 205 | foregroundPainter: _PointDemoPainter( |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 206 | repaint: _animation, |
Alexandre Ardhuin | 387f885 | 2019-03-01 08:17:55 +0100 | [diff] [blame] | 207 | arc: arc, |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 208 | ), |
| 209 | // Watch out: if this IgnorePointer is left out, then gestures that |
| 210 | // fail _PointDemoPainter.hitTest() will still be recognized because |
| 211 | // they do overlap this child, which is as big as the CustomPaint. |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 212 | child: IgnorePointer( |
| 213 | child: Padding( |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 214 | padding: const EdgeInsets.all(16.0), |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 215 | child: Text( |
Alexandre Ardhuin | 1fce14a | 2017-10-22 18:11:36 +0200 | [diff] [blame] | 216 | 'Tap the refresh button to run the animation. Drag the green ' |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 217 | "and red points to change the animation's path.", |
Alexandre Ardhuin | 387f885 | 2019-03-01 08:17:55 +0100 | [diff] [blame] | 218 | style: Theme.of(context).textTheme.caption.copyWith(fontSize: 16.0), |
| 219 | ), |
| 220 | ), |
| 221 | ), |
| 222 | ), |
| 223 | ), |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 224 | ); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | class _RectangleDemoPainter extends CustomPainter { |
| 229 | _RectangleDemoPainter({ |
| 230 | Animation<double> repaint, |
Alexandre Ardhuin | 387f885 | 2019-03-01 08:17:55 +0100 | [diff] [blame] | 231 | this.arc, |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 232 | }) : _repaint = repaint, super(repaint: repaint); |
| 233 | |
| 234 | final MaterialRectArcTween arc; |
Adam Barth | 7be9115 | 2017-04-19 09:30:43 -0700 | [diff] [blame] | 235 | final Animation<double> _repaint; |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 236 | |
Ian Hickson | bf017b7 | 2017-04-12 15:06:12 -0700 | [diff] [blame] | 237 | void drawPoint(Canvas canvas, Offset p, Color color) { |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 238 | final Paint paint = Paint() |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 239 | ..color = color.withOpacity(0.25) |
| 240 | ..style = PaintingStyle.fill; |
| 241 | canvas.drawCircle(p, _kPointRadius, paint); |
| 242 | paint |
| 243 | ..color = color |
| 244 | ..style = PaintingStyle.stroke |
| 245 | ..strokeWidth = 2.0; |
| 246 | canvas.drawCircle(p, _kPointRadius + 1.0, paint); |
| 247 | } |
| 248 | |
| 249 | void drawRect(Canvas canvas, Rect rect, Color color) { |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 250 | final Paint paint = Paint() |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 251 | ..color = color.withOpacity(0.25) |
| 252 | ..strokeWidth = 4.0 |
| 253 | ..style = PaintingStyle.stroke; |
| 254 | canvas.drawRect(rect, paint); |
| 255 | drawPoint(canvas, rect.center, color); |
| 256 | } |
| 257 | |
| 258 | @override |
| 259 | void paint(Canvas canvas, Size size) { |
Alexandre Ardhuin | 578ca0a | 2017-03-21 23:14:55 +0100 | [diff] [blame] | 260 | drawRect(canvas, arc.begin, Colors.green); |
| 261 | drawRect(canvas, arc.end, Colors.red); |
| 262 | drawRect(canvas, arc.lerp(_repaint.value), Colors.blue); |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | @override |
Ian Hickson | bf017b7 | 2017-04-12 15:06:12 -0700 | [diff] [blame] | 266 | bool hitTest(Offset position) { |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 267 | return (arc.begin.center - position).distanceSquared < _kTargetSlop |
| 268 | || (arc.end.center - position).distanceSquared < _kTargetSlop; |
| 269 | } |
| 270 | |
| 271 | @override |
| 272 | bool shouldRepaint(_RectangleDemoPainter oldPainter) => arc != oldPainter.arc; |
| 273 | } |
| 274 | |
| 275 | class _RectangleDemo extends StatefulWidget { |
Alexandre Ardhuin | 9541848 | 2017-04-21 23:09:42 +0200 | [diff] [blame] | 276 | const _RectangleDemo({ Key key, this.controller }) : super(key: key); |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 277 | |
| 278 | final AnimationController controller; |
| 279 | |
| 280 | @override |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 281 | _RectangleDemoState createState() => _RectangleDemoState(); |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | class _RectangleDemoState extends State<_RectangleDemo> { |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 285 | final GlobalKey _painterKey = GlobalKey(); |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 286 | |
| 287 | CurvedAnimation _animation; |
| 288 | _DragTarget _dragTarget; |
Hans Muller | f27fa0e | 2016-09-01 14:07:22 -0700 | [diff] [blame] | 289 | Size _screenSize; |
Matt Perry | 82b55c5 | 2016-08-08 11:52:35 -0400 | [diff] [blame] | 290 | Rect _begin; |
| 291 | Rect _end; |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 292 | |
| 293 | @override |
| 294 | void initState() { |
| 295 | super.initState(); |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 296 | _animation = CurvedAnimation(parent: widget.controller, curve: Curves.fastOutSlowIn); |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | @override |
| 300 | void dispose() { |
xster | 89a7fdf | 2017-04-10 18:32:24 -0700 | [diff] [blame] | 301 | widget.controller.value = 0.0; |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 302 | super.dispose(); |
| 303 | } |
| 304 | |
Ian Hickson | bf017b7 | 2017-04-12 15:06:12 -0700 | [diff] [blame] | 305 | Drag _handleOnStart(Offset position) { |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 306 | // TODO(hansmuller): allow the user to drag both points at the same time. |
| 307 | if (_dragTarget != null) |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 308 | return _IgnoreDrag(); |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 309 | |
| 310 | final RenderBox box = _painterKey.currentContext.findRenderObject(); |
| 311 | final double startOffset = (box.localToGlobal(_begin.center) - position).distanceSquared; |
| 312 | final double endOffset = (box.localToGlobal(_end.center) - position).distanceSquared; |
| 313 | setState(() { |
| 314 | if (startOffset < endOffset && startOffset < _kTargetSlop) |
| 315 | _dragTarget = _DragTarget.start; |
| 316 | else if (endOffset < _kTargetSlop) |
| 317 | _dragTarget = _DragTarget.end; |
| 318 | else |
| 319 | _dragTarget = null; |
| 320 | }); |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 321 | return _DragHandler(_handleDragUpdate, _handleDragCancel, _handleDragEnd); |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Alexandre Ardhuin | c02b6a8 | 2018-02-02 23:27:29 +0100 | [diff] [blame] | 324 | void _handleDragUpdate(DragUpdateDetails details) { |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 325 | switch (_dragTarget) { |
| 326 | case _DragTarget.start: |
| 327 | setState(() { |
| 328 | _begin = _begin.shift(details.delta); |
| 329 | }); |
| 330 | break; |
| 331 | case _DragTarget.end: |
| 332 | setState(() { |
| 333 | _end = _end.shift(details.delta); |
| 334 | }); |
| 335 | break; |
| 336 | } |
| 337 | } |
| 338 | |
Alexandre Ardhuin | c02b6a8 | 2018-02-02 23:27:29 +0100 | [diff] [blame] | 339 | void _handleDragCancel() { |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 340 | _dragTarget = null; |
xster | 89a7fdf | 2017-04-10 18:32:24 -0700 | [diff] [blame] | 341 | widget.controller.value = 0.0; |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Alexandre Ardhuin | c02b6a8 | 2018-02-02 23:27:29 +0100 | [diff] [blame] | 344 | void _handleDragEnd(DragEndDetails details) { |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 345 | _dragTarget = null; |
| 346 | } |
| 347 | |
| 348 | @override |
| 349 | Widget build(BuildContext context) { |
Hans Muller | d0e72d6 | 2016-08-26 15:19:46 -0700 | [diff] [blame] | 350 | final Size screenSize = MediaQuery.of(context).size; |
Hans Muller | f27fa0e | 2016-09-01 14:07:22 -0700 | [diff] [blame] | 351 | if (_screenSize == null || _screenSize != screenSize) { |
| 352 | _screenSize = screenSize; |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 353 | _begin = Rect.fromLTWH( |
Hans Muller | f27fa0e | 2016-09-01 14:07:22 -0700 | [diff] [blame] | 354 | screenSize.width * 0.5, screenSize.height * 0.2, |
Alexandre Ardhuin | 387f885 | 2019-03-01 08:17:55 +0100 | [diff] [blame] | 355 | screenSize.width * 0.4, screenSize.height * 0.2, |
Hans Muller | f27fa0e | 2016-09-01 14:07:22 -0700 | [diff] [blame] | 356 | ); |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 357 | _end = Rect.fromLTWH( |
Hans Muller | f27fa0e | 2016-09-01 14:07:22 -0700 | [diff] [blame] | 358 | screenSize.width * 0.1, screenSize.height * 0.4, |
Alexandre Ardhuin | 387f885 | 2019-03-01 08:17:55 +0100 | [diff] [blame] | 359 | screenSize.width * 0.3, screenSize.height * 0.3, |
Hans Muller | f27fa0e | 2016-09-01 14:07:22 -0700 | [diff] [blame] | 360 | ); |
| 361 | } |
Hans Muller | d0e72d6 | 2016-08-26 15:19:46 -0700 | [diff] [blame] | 362 | |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 363 | final MaterialRectArcTween arc = MaterialRectArcTween(begin: _begin, end: _end); |
| 364 | return RawGestureDetector( |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 365 | behavior: _dragTarget == null ? HitTestBehavior.deferToChild : HitTestBehavior.opaque, |
| 366 | gestures: <Type, GestureRecognizerFactory>{ |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 367 | ImmediateMultiDragGestureRecognizer: GestureRecognizerFactoryWithHandlers<ImmediateMultiDragGestureRecognizer>( |
| 368 | () => ImmediateMultiDragGestureRecognizer(), |
Ian Hickson | 46b316c | 2017-06-07 18:04:46 -0700 | [diff] [blame] | 369 | (ImmediateMultiDragGestureRecognizer instance) { |
| 370 | instance |
| 371 | ..onStart = _handleOnStart; |
| 372 | }, |
| 373 | ), |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 374 | }, |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 375 | child: ClipRect( |
| 376 | child: CustomPaint( |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 377 | key: _painterKey, |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 378 | foregroundPainter: _RectangleDemoPainter( |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 379 | repaint: _animation, |
Alexandre Ardhuin | 387f885 | 2019-03-01 08:17:55 +0100 | [diff] [blame] | 380 | arc: arc, |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 381 | ), |
| 382 | // Watch out: if this IgnorePointer is left out, then gestures that |
| 383 | // fail _RectDemoPainter.hitTest() will still be recognized because |
| 384 | // they do overlap this child, which is as big as the CustomPaint. |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 385 | child: IgnorePointer( |
| 386 | child: Padding( |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 387 | padding: const EdgeInsets.all(16.0), |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 388 | child: Text( |
Alexandre Ardhuin | 1fce14a | 2017-10-22 18:11:36 +0200 | [diff] [blame] | 389 | 'Tap the refresh button to run the animation. Drag the rectangles ' |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 390 | "to change the animation's path.", |
Alexandre Ardhuin | 387f885 | 2019-03-01 08:17:55 +0100 | [diff] [blame] | 391 | style: Theme.of(context).textTheme.caption.copyWith(fontSize: 16.0), |
| 392 | ), |
| 393 | ), |
| 394 | ), |
| 395 | ), |
| 396 | ), |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 397 | ); |
| 398 | } |
| 399 | } |
| 400 | |
Alexandre Ardhuin | a07d371 | 2018-09-14 21:06:19 +0200 | [diff] [blame] | 401 | typedef _DemoBuilder = Widget Function(_ArcDemo demo); |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 402 | |
| 403 | class _ArcDemo { |
Alexandre Ardhuin | a9ba0e2 | 2017-03-15 18:30:55 +0100 | [diff] [blame] | 404 | _ArcDemo(this.title, this.builder, TickerProvider vsync) |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 405 | : controller = AnimationController(duration: const Duration(milliseconds: 500), vsync: vsync), |
| 406 | key = GlobalKey(debugLabel: title); |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 407 | |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 408 | final String title; |
| 409 | final _DemoBuilder builder; |
Ian Hickson | 9e67385 | 2016-09-26 10:57:10 -0700 | [diff] [blame] | 410 | final AnimationController controller; |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 411 | final GlobalKey key; |
| 412 | } |
| 413 | |
| 414 | class AnimationDemo extends StatefulWidget { |
Alexandre Ardhuin | 9541848 | 2017-04-21 23:09:42 +0200 | [diff] [blame] | 415 | const AnimationDemo({ Key key }) : super(key: key); |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 416 | |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 417 | @override |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 418 | _AnimationDemoState createState() => _AnimationDemoState(); |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Ian Hickson | 9e67385 | 2016-09-26 10:57:10 -0700 | [diff] [blame] | 421 | class _AnimationDemoState extends State<AnimationDemo> with TickerProviderStateMixin { |
Ian Hickson | 9e67385 | 2016-09-26 10:57:10 -0700 | [diff] [blame] | 422 | List<_ArcDemo> _allDemos; |
| 423 | |
| 424 | @override |
| 425 | void initState() { |
| 426 | super.initState(); |
| 427 | _allDemos = <_ArcDemo>[ |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 428 | _ArcDemo('POINT', (_ArcDemo demo) { |
| 429 | return _PointDemo( |
Ian Hickson | 9e67385 | 2016-09-26 10:57:10 -0700 | [diff] [blame] | 430 | key: demo.key, |
Alexandre Ardhuin | 387f885 | 2019-03-01 08:17:55 +0100 | [diff] [blame] | 431 | controller: demo.controller, |
Ian Hickson | 9e67385 | 2016-09-26 10:57:10 -0700 | [diff] [blame] | 432 | ); |
| 433 | }, this), |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 434 | _ArcDemo('RECTANGLE', (_ArcDemo demo) { |
| 435 | return _RectangleDemo( |
Ian Hickson | 9e67385 | 2016-09-26 10:57:10 -0700 | [diff] [blame] | 436 | key: demo.key, |
Alexandre Ardhuin | 387f885 | 2019-03-01 08:17:55 +0100 | [diff] [blame] | 437 | controller: demo.controller, |
Ian Hickson | 9e67385 | 2016-09-26 10:57:10 -0700 | [diff] [blame] | 438 | ); |
| 439 | }, this), |
| 440 | ]; |
| 441 | } |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 442 | |
Alexandre Ardhuin | d340e2f | 2018-10-04 18:44:23 +0200 | [diff] [blame] | 443 | Future<void> _play(_ArcDemo demo) async { |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 444 | await demo.controller.forward(); |
| 445 | if (demo.key.currentState != null && demo.key.currentState.mounted) |
| 446 | demo.controller.reverse(); |
| 447 | } |
| 448 | |
| 449 | @override |
| 450 | Widget build(BuildContext context) { |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 451 | return DefaultTabController( |
Hans Muller | b23aed7 | 2017-01-09 14:55:36 -0800 | [diff] [blame] | 452 | length: _allDemos.length, |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 453 | child: Scaffold( |
| 454 | appBar: AppBar( |
Ian Hickson | 3eb8783 | 2017-04-07 12:24:32 -0700 | [diff] [blame] | 455 | title: const Text('Animation'), |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 456 | bottom: TabBar( |
Alexandre Ardhuin | f62afdc | 2018-10-01 21:29:08 +0200 | [diff] [blame] | 457 | tabs: _allDemos.map<Tab>((_ArcDemo demo) => Tab(text: demo.title)).toList(), |
Hans Muller | b23aed7 | 2017-01-09 14:55:36 -0800 | [diff] [blame] | 458 | ), |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 459 | ), |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 460 | floatingActionButton: Builder( |
Hans Muller | b23aed7 | 2017-01-09 14:55:36 -0800 | [diff] [blame] | 461 | builder: (BuildContext context) { |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 462 | return FloatingActionButton( |
Alexandre Ardhuin | 610955f | 2017-04-08 08:43:19 +0200 | [diff] [blame] | 463 | child: const Icon(Icons.refresh), |
Hans Muller | b23aed7 | 2017-01-09 14:55:36 -0800 | [diff] [blame] | 464 | onPressed: () { |
| 465 | _play(_allDemos[DefaultTabController.of(context).index]); |
| 466 | }, |
| 467 | ); |
| 468 | }, |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 469 | ), |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 470 | body: TabBarView( |
Alexandre Ardhuin | 387f885 | 2019-03-01 08:17:55 +0100 | [diff] [blame] | 471 | children: _allDemos.map<Widget>((_ArcDemo demo) => demo.builder(demo)).toList(), |
| 472 | ), |
| 473 | ), |
Hans Muller | 36eb4a0 | 2016-07-21 10:48:41 -0700 | [diff] [blame] | 474 | ); |
| 475 | } |
| 476 | } |
Adam Barth | 8a82332 | 2016-09-29 08:59:43 -0700 | [diff] [blame] | 477 | |
| 478 | void main() { |
Greg Spencer | 9749f59 | 2019-09-25 14:49:22 -0700 | [diff] [blame] | 479 | if (Platform.isMacOS) { |
| 480 | // TODO(gspencergoog): Update this when TargetPlatform includes macOS. https://github.com/flutter/flutter/issues/31366 |
| 481 | // See https://github.com/flutter/flutter/wiki/Desktop-shells#target-platform-override |
| 482 | debugDefaultTargetPlatformOverride = TargetPlatform.fuchsia; |
| 483 | } |
| 484 | |
Dan Field | ea5435c | 2018-09-25 13:57:12 -0400 | [diff] [blame] | 485 | runApp(const MaterialApp( |
| 486 | home: AnimationDemo(), |
Adam Barth | 8a82332 | 2016-09-29 08:59:43 -0700 | [diff] [blame] | 487 | )); |
| 488 | } |