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