HelpAnimations class
Animations
Animation Types:
Drawing-based
- Use external framework and export to flutter (e..g, Flare, Lottie)
Code-based
Implicit (AnimatedFoo)
BuiltIn
Custom: TweenAnimationBuilder
Explicit (FooTransition)
- Requires AnimationController, and managing life cycle inside stateful widget Used if any of the following is true:
- Repeats forever
- Discontinuous animation
- Multiple widgets animating together
Built In
Custom
AnimatedWidget
- Standalone widget
- Use Process:
- Define class to extend
AnimatedWidget
- Override
build
to return desired widget which will be animated - Add listenable argument to constructor and pass it up to Animated widget as well. This tells the widget what to look out for.
- Add getter to make listenable argument usable.
- Add listenable argument to build method to affect widget properties.
- Create
AnimationController
withinStateWidget
where animation will be used. EnsureAnimationController
is initialised ininit
and disposed indispose
. - Extend
StatefulWidget
withTickerProviderStateMixin
to get a ticker listener. The ticker listens to frameCallback and determines the duration between the current and last frame, and passes it to the controller to control the animation. Implement it in the controller with vsync. - Add methods to direct the controller.
EXAMPLE:
/// 1.
class FooTransition extends AnimatedWidget {
/// 3.
const FooTransition(Type listenableArg)
: super(listenable: listenableArg);
/// 4.
Animation<type> get listenableArg => listenable
/// 2.
@override
Widget build(BuildContext context) {
/// 5.
return ...
}
class ExampleStateful extends StatefulWidget {
}
class _ExampleStatefulState extends State<ExampleStateful>
with TickerProviderStateMixin {
late final AnimationController _controller = AnimationController(vsync: this, ...)
..method1()
..method2();
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return FooTransition(controller: _controller);
}
}
}
CustomPainter
- Similar to AnimatedWidget but paints directly to canvas without Widget build paradigm, for complex animations or higher performance. Could cause more performance problems if misused.
- CustomPaint is a widget which provides a canvas and takes a CustomPainter to execute paint commands.
- Implementation:
- Define
CustomPaint
widget with painter and child/size inStateWidget
- Define
myPainter
class as extension ofCustomPainter
, overridepaint
andshouldRepaint
. - Define paint commands within
paint
function withcanvas.method()
. - Define repaint scenarios within
shouldRepaint
if necessary. - For animations, pass the controller into the painter and change parameters with progress value.
/// 1.
class ExampleStateful extends StatefulWidget {
}
class _ExampleStatefulState extends State<ExampleStateful>
with TickerProviderStateMixin {
late final AnimationController _controller = AnimationController(vsync: this, ...)
..method1()
..method2();
final customPaint = CustomPaint(
painter: MyPainter(_controller),
child: myWidget(),
)
@override
Widget build(BuildContext context) {
return customPaint;
}
class MyPainter extends CustomPainter {
@override
void paint(ui.Canvas canvas, ui.Size size) {
/// paint commands
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
/// hook called when CustomPainter is rebuilt
/// when to repaint, set to true if necessary
return false;
}
}
AnimatedBuilder
- Part of parent widget
Constructors
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited