|
| 1 | +// Copyright 2013 The Flutter Authors |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +// #region body |
| 6 | +import 'dart:async'; |
| 7 | + |
| 8 | +import 'package:flutter/physics.dart'; |
| 9 | +import 'package:material_ui/material_ui.dart'; |
| 10 | + |
| 11 | +/// Flutter code sample for [MaterialShapes]. |
| 12 | +
|
| 13 | +void main() { |
| 14 | + runApp(const MaterialShapesExampleApp()); |
| 15 | +} |
| 16 | + |
| 17 | +class MaterialShapesExampleApp extends StatelessWidget { |
| 18 | + const MaterialShapesExampleApp({super.key}); |
| 19 | + |
| 20 | + @override |
| 21 | + Widget build(BuildContext context) { |
| 22 | + return MaterialApp(home: const MaterialShapesExample()); |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +class MaterialShapesExample extends StatefulWidget { |
| 27 | + const MaterialShapesExample({super.key}); |
| 28 | + |
| 29 | + @override |
| 30 | + State<MaterialShapesExample> createState() => _MaterialShapesExampleState(); |
| 31 | +} |
| 32 | + |
| 33 | +class _MaterialShapesExampleState extends State<MaterialShapesExample> |
| 34 | + with SingleTickerProviderStateMixin { |
| 35 | + /// The shapes to morph through, in the order they are listed in |
| 36 | + /// [MaterialShapes.all]. |
| 37 | + static final List<RoundedPolygon> _shapes = MaterialShapes.all; |
| 38 | + |
| 39 | + /// A tween from each shape in [_shapes] to the next one, wrapping around |
| 40 | + /// from the last shape back to the first. |
| 41 | + /// |
| 42 | + /// Lerping between two [MaterialShapeBorder]s morphs one shape into the |
| 43 | + /// other. |
| 44 | + late final List<ShapeBorderTween> _tweens; |
| 45 | + |
| 46 | + /// The tween that is currently animating. |
| 47 | + late ShapeBorderTween _tween; |
| 48 | + |
| 49 | + /// Drives the progress of [_tween]. |
| 50 | + /// |
| 51 | + /// The controller is unbounded so that the springs below can overshoot past a |
| 52 | + /// progress of 1, which carries the morph slightly beyond its end shape. |
| 53 | + late final AnimationController _controller; |
| 54 | + |
| 55 | + int _morphIndex = 0; |
| 56 | + |
| 57 | + Timer? _startTimer; |
| 58 | + |
| 59 | + final _bouncySimulation = SpringSimulation( |
| 60 | + SpringDescription.withDampingRatio(ratio: 0.5, stiffness: 400, mass: 1), |
| 61 | + 0, |
| 62 | + 1, |
| 63 | + 5, |
| 64 | + snapToEnd: true, |
| 65 | + ); |
| 66 | + |
| 67 | + // Overshooting past a progress of 1 extrapolates a morph beyond its end |
| 68 | + // shape, which distorts the heart into sharp spikes. The heart therefore |
| 69 | + // animates with a spring that starts at rest and barely overshoots. |
| 70 | + final _lessBouncySimulation = SpringSimulation( |
| 71 | + SpringDescription.withDampingRatio(ratio: 0.8, stiffness: 300, mass: 1), |
| 72 | + 0, |
| 73 | + 1, |
| 74 | + 0, |
| 75 | + snapToEnd: true, |
| 76 | + ); |
| 77 | + |
| 78 | + /// The shape that the currently animating morph ends on. |
| 79 | + RoundedPolygon get _targetShape => |
| 80 | + _shapes[(_morphIndex + 1) % _shapes.length]; |
| 81 | + |
| 82 | + @override |
| 83 | + void initState() { |
| 84 | + super.initState(); |
| 85 | + |
| 86 | + _tweens = <ShapeBorderTween>[ |
| 87 | + for (var i = 0; i < _shapes.length; i++) |
| 88 | + ShapeBorderTween( |
| 89 | + begin: MaterialShapeBorder(shape: _shapes[i]), |
| 90 | + end: MaterialShapeBorder(shape: _shapes[(i + 1) % _shapes.length]), |
| 91 | + ), |
| 92 | + ]; |
| 93 | + _tween = _tweens.first; |
| 94 | + _controller = AnimationController.unbounded(vsync: this); |
| 95 | + |
| 96 | + // Hold the first shape for a moment so that it can be seen before it starts |
| 97 | + // morphing. |
| 98 | + _startTimer = Timer(const Duration(seconds: 1), _morphThroughShapes); |
| 99 | + } |
| 100 | + |
| 101 | + @override |
| 102 | + void dispose() { |
| 103 | + _startTimer?.cancel(); |
| 104 | + _controller.dispose(); |
| 105 | + super.dispose(); |
| 106 | + } |
| 107 | + |
| 108 | + /// Animates the tweens in [_tweens] one after another, looping back to the |
| 109 | + /// first one after the last, until this widget is disposed. |
| 110 | + /// |
| 111 | + /// Waiting for each spring to settle, rather than advancing on a fixed |
| 112 | + /// interval, keeps a morph from being cut short and popping to the next |
| 113 | + /// shape. |
| 114 | + Future<void> _morphThroughShapes() async { |
| 115 | + while (mounted) { |
| 116 | + setState(() => _tween = _tweens[_morphIndex]); |
| 117 | + _controller.value = 0; |
| 118 | + await _controller.animateWith( |
| 119 | + _targetShape == MaterialShapes.heart |
| 120 | + ? _lessBouncySimulation |
| 121 | + : _bouncySimulation, |
| 122 | + ); |
| 123 | + _morphIndex = (_morphIndex + 1) % _tweens.length; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + @override |
| 128 | + Widget build(BuildContext context) { |
| 129 | + return Scaffold( |
| 130 | + appBar: AppBar(title: const Text('Material 3 Expressive Shapes Sample')), |
| 131 | + body: Center( |
| 132 | + child: ConstrainedBox( |
| 133 | + constraints: const BoxConstraints(maxWidth: 300, maxHeight: 300), |
| 134 | + child: AspectRatio( |
| 135 | + aspectRatio: 1, |
| 136 | + child: AnimatedBuilder( |
| 137 | + animation: _controller, |
| 138 | + builder: (BuildContext context, Widget? child) { |
| 139 | + return DecoratedBox( |
| 140 | + decoration: ShapeDecoration( |
| 141 | + color: Theme.of(context).colorScheme.primary, |
| 142 | + shape: _tween.evaluate(_controller)!, |
| 143 | + ), |
| 144 | + ); |
| 145 | + }, |
| 146 | + ), |
| 147 | + ), |
| 148 | + ), |
| 149 | + ), |
| 150 | + ); |
| 151 | + } |
| 152 | +} |
| 153 | +// #endregion body |
0 commit comments