withAnimation function wraps callbacks to execute state changes within an animation context, allowing smooth transitions when UI state updates.
Parameters
animation(optional) - Animation configuration created with Spring(), InterpolatingSpring(), EaseIn(), EaseInOut(), EaseOut(), Bouncy(), Snappy(), or Linear() constructors, or an AnimationComponentbody- Function to execute within the animation context
Returns
Returns a Component that executes the body function within the animation context.Usage
Basic animated state change
With animation configuration
Using animation constructors
Chaining animations
Animation Constructors
You can create animations using these constructor functions:Spring Animations
-
Spring(options?)- Creates a spring animationresponse- Spring response time (default: 0.5)dampingFraction- Damping fraction (default: 1.0)blendDuration- Blend duration (default: 0)
-
InterpolatingSpring(options)- Creates an interpolating spring animationstiffness- Spring stiffness (required)damping- Spring damping (required)mass- Spring mass (required)
-
Bouncy(options?)- Creates a bouncy spring animationduration- Animation durationextraBounce- Extra bounce amount
-
Snappy(options?)- Creates a snappy spring animationresponse- Spring response timedampingFraction- Damping fractionblendDuration- Blend duration
Easing Animations
EaseIn(options?)- Ease-in animationduration- Animation duration
EaseOut(options?)- Ease-out animationduration- Animation duration
EaseInOut(options?)- Ease-in-out animationduration- Animation duration
Linear(options?)- Linear animationduration- Animation duration
Animation Modifiers
All animation constructors return an AnimationComponent that supports chaining:.delay(seconds)- Delays the animation start.speed(multiplier)- Adjusts animation speed.repeatCount(count)- Repeats the animation a specific number of times.repeatForever(enabled)- Repeats the animation indefinitely
Notes
- Animation behavior depends on the runtime implementation
- Default implementation executes the callback immediately if no animation system is available
- Animation options are passed through to the native animation system
- Multiple state changes within the same
withAnimationblock are batched - The animation context applies to all state changes within the callback
- Renderer-specific animation systems may support different configuration options
