setStateWithAnimation method

  1. @protected
void setStateWithAnimation(
  1. AnimationSpec animation,
  2. VoidCallback fn
)

A version of setState that applies an animation to the state changes caused by calling fn.

See Animated for a widget that applies an animation only to the state changes in its descendants.

This is simply a convenience method for calling setState within withAnimation

withAnimation(const AnimationSpec(), () {
  setState(() {
    // Change state...
  });
});

But it is usually more convenient to use this method.

Only widgets that support animating with Fleet will animate changes. To implement support for this in your own widgets use AnimatableStatelessWidget or AnimatableSingleChildRenderObjectWidgetMixin.

The following provided widgets support animating with Fleet:

The following provided widgets are specific to Fleet:

Examples

import 'package:flutter/material.dart';

class MyWidget extends StatefulWidget {
  const MyWidget({super.key});

  @override
  State<MyWidget> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> with AnimatingStateMixin {
  var _active = false;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        setStateWithAnimation(Curves.ease.animation(250.ms), () {
          _active = !_active;
        });
      },
      child: FleetColoredBox(color: _active ? Colors.blue : Colors.grey),
    );
  }
}

See also:

  • withAnimation for a static function that applies an animation to the state changes caused by calling a callback.
  • Animated for a widget that applies an animation to the state changes in its descendants.

Implementation

@protected
void setStateWithAnimation(AnimationSpec animation, VoidCallback fn) {
  withAnimation(animation, () => super.setState(fn));
}