Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Implement Flutter animations: implicit, explicit, hero, staggered, and physics-based with a decision tree guide.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/implicit.md
1# Implicit Animations Reference23Implicit animations automatically handle animations when widget properties change. No AnimationController or explicit state management needed.45## Core Concept67Implicitly animated widgets extend `ImplicitlyAnimatedWidget`. When you change a property (color, size, etc.), the widget automatically animates to the new value using a specified duration and curve.89## Available Widgets1011### AnimatedContainer1213Animates multiple properties simultaneously.1415```dart16AnimatedContainer(17duration: const Duration(milliseconds: 300),18curve: Curves.easeInOut,19width: _expanded ? 200 : 100,20height: _expanded ? 200 : 100,21decoration: BoxDecoration(22color: _expanded ? Colors.blue : Colors.red,23borderRadius: BorderRadius.circular(_expanded ? 20 : 8),24),25child: const FlutterLogo(),26)27```2829**Animatable properties:**30- `width`, `height` - Size31- `color` - Background color32- `decoration` - BoxDecoration (includes border, shadow, image, etc.)33- `padding` - Padding34- `margin` - Margin (via decoration)35- `transform` - Matrix4 transformation3637### AnimatedOpacity3839Fades a widget in and out.4041```dart42AnimatedOpacity(43opacity: _visible ? 1.0 : 0.0,44duration: const Duration(milliseconds: 500),45curve: Curves.easeInOut,46onEnd: () => print('Animation complete'),47child: const Text('Hello'),48)49```5051### AnimatedPadding5253Animates padding changes.5455```dart56AnimatedPadding(57padding: EdgeInsets.all(_padded ? 16.0 : 8.0),58duration: const Duration(milliseconds: 300),59curve: Curves.ease,60child: const Card(child: Text('Padded content')),61)62```6364### AnimatedPositioned6566Animates position within a Stack.6768```dart69Stack(70children: [71AnimatedPositioned(72top: _top ? 0 : 100,73left: _left ? 0 : 100,74duration: const Duration(milliseconds: 500),75curve: Curves.easeInOut,76child: const FlutterLogo(),77),78],79)80```8182### AnimatedAlign8384Animates alignment changes.8586```dart87AnimatedAlign(88alignment: _aligned ? Alignment.topLeft : Alignment.bottomRight,89duration: const Duration(milliseconds: 400),90curve: Curves.easeInOut,91child: const Text('Align me'),92)93```9495### AnimatedContainer (Multiple Properties)9697```dart98AnimatedContainer(99duration: const Duration(milliseconds: 300),100curve: Curves.easeInOut,101transform: Matrix4.rotationZ(_rotated ? 0.5 : 0),102child: const FlutterLogo(),103)104```105106### TweenAnimationBuilder107108Custom tween animation without creating a custom widget.109110```dart111TweenAnimationBuilder<double>(112tween: Tween<double>(begin: 0, end: 1),113duration: const Duration(seconds: 1),114curve: Curves.easeInOut,115builder: (context, value, child) {116return Opacity(117opacity: value,118child: Transform.scale(119scale: value,120child: child,121),122);123},124child: const FlutterLogo(),125)126```127128### AnimatedSwitcher129130Cross-fades between two widgets.131132```dart133AnimatedSwitcher(134duration: const Duration(milliseconds: 300),135transitionBuilder: (Widget child, Animation<double> animation) {136return FadeTransition(137opacity: animation,138child: child,139);140},141child: _showFirst142? const Text('First')143: const Text('Second'),144)145```146147With custom size transition:148```dart149AnimatedSwitcher(150duration: const Duration(milliseconds: 500),151transitionBuilder: (Widget child, Animation<double> animation) {152return SizeTransition(153sizeFactor: animation,154axis: Axis.vertical,155child: child,156);157},158child: _expanded159? const Text('Expanded content')160: const Text('Collapsed content'),161)162```163164### AnimatedDefaultTextStyle165166Animates text style changes.167168```dart169AnimatedDefaultTextStyle(170duration: const Duration(milliseconds: 300),171style: TextStyle(172fontSize: _large ? 32 : 16,173color: _colored ? Colors.blue : Colors.black,174fontWeight: _bold ? FontWeight.bold : FontWeight.normal,175),176child: const Text('Animated text'),177)178```179180### AnimatedPhysicalModel181182Animates elevation, color, and shape with physical shadow.183184```dart185AnimatedPhysicalModel(186duration: const Duration(milliseconds: 300),187shape: BoxShape.rectangle,188elevation: _elevated ? 12 : 0,189color: _elevated ? Colors.blue : Colors.grey[300]!,190borderRadius: BorderRadius.circular(12),191child: const Padding(192padding: EdgeInsets.all(16),193child: Text('Physical model'),194),195)196```197198### AnimatedTheme199200Animates theme changes.201202```dart203AnimatedTheme(204duration: const Duration(milliseconds: 500),205data: ThemeData(206brightness: _darkMode ? Brightness.dark : Brightness.light,207primaryColor: _darkMode ? Colors.blue[700] : Colors.blue,208),209child: const MyChildWidget(),210)211```212213## Common Parameters214215All implicit animations support these parameters:216217```dart218AnimatedContainer(219duration: Duration(milliseconds: 300), // Required: Animation duration220curve: Curves.easeInOut, // Optional: Animation curve221onEnd: () {}, // Optional: Completion callback222child: Widget(), // The child widget to animate223)224```225226### Duration227228```dart229const Duration(milliseconds: 300) // 0.3 seconds230const Duration(seconds: 1) // 1 second231const Duration(days: 1) // 1 day232```233234### Curves235236Common curves from `Curves` class:237238```dart239Curves.linear // Linear (no easing)240Curves.ease // Simple ease in and out241Curves.easeIn // Slow start, fast end242Curves.easeOut // Fast start, slow end243Curves.easeInOut // Slow start and end, fast middle244Curves.easeInCubic // Cubic ease in245Curves.easeOutCubic // Cubic ease out246Curves.easeInOutCubic // Cubic ease in and out247Curves.elasticIn // Bounces in248Curves.elasticOut // Bounces out249Curves.elasticInOut // Bounces in and out250Curves.bounceIn // Bounces in (more subtle)251Curves.bounceOut // Bounces out (more subtle)252```253254Custom curve:255```dart256class ShakeCurve extends Curve {257@override258double transform(double t) => sin(t * pi * 2);259}260261// Usage262AnimatedContainer(263curve: ShakeCurve(),264// ...265)266```267268### onEnd Callback269270Trigger action when animation completes:271272```dart273AnimatedOpacity(274opacity: _visible ? 1.0 : 0.0,275duration: const Duration(milliseconds: 300),276onEnd: () {277if (!_visible) {278// Animation finished fading out279// Remove widget, navigate, etc.280}281},282child: const Text('Hello'),283)284```285286## Best Practices287288### DO289290- Use implicit animations for simple, one-off animations291- Set appropriate durations for natural feel (200-500ms typical)292- Use curves to make animations feel natural293- Use `onEnd` callback for post-animation actions294- Test animations on various devices295- Consider `MediaQuery.disableAnimations` for accessibility296297### DON'T298299- Use implicit animations for complex, multi-property sequences (use staggered instead)300- Forget to set duration (defaults to instant change)301- Use extremely long durations (> 1s) without good reason302- Animate too many properties simultaneously (performance impact)303- Nest implicit animations deeply (can cause jank)304305## Performance Tips306307- Implicit animations are optimized but still rebuild during animation308- Avoid animating complex widget trees309- Use `RepaintBoundary` around animated children to isolate repaints310- Test performance with Flutter DevTools Performance overlay311- Consider using `AnimatedBuilder` for complex widgets to minimize rebuilds312313## Debugging314315### Slow Animations316317```dart318// Slow all animations by 10x during development319timeDilation = 10.0;320```321322### Show Repaint Rainbows323324```dart325void main() {326debugPaintSizeEnabled = true;327runApp(MyApp());328}329```330331### Performance Overlay332333```dart334void main() {335runApp(MyApp());336}337338// In debug mode, press 'p' to toggle performance overlay339```340341## Comparison: Implicit vs Explicit342343| Feature | Implicit | Explicit |344|---------|----------|-----------|345| Controller needed | No | Yes (AnimationController) |346| Setup complexity | Low | Medium/High |347| Reusability | Widget-based | Controller-based |348| Multiple properties | AnimatedContainer only | Unlimited |349| Lifecycle control | Limited | Full |350| State monitoring | Limited (onEnd) | Full (addStatusListener) |351| Performance | Good | Excellent (with proper patterns) |352353## When to Use Implicit Animations354355Use implicit animations when:356- Animating a single property357- Animation is triggered by state change358- No need for fine-grained control359- Want simple, declarative code360- Animation is one-time or simple toggle361362Use explicit animations when:363- Need full control over animation lifecycle364- Animating multiple properties365- Need to react to animation state366- Creating reusable animation components367- Need complex timing or sequencing368- Performance is critical369