Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Flutter Animation Fundamentals

Flutter Animation Fundamentals

This section will introduce animation implementation in Flutter, including implicit and explicit animations.


Implicit Animations

Implicit animations are animation Widgets whose transition effects are handled automatically by Flutter.

Example: AnimatedContainer

class

AnimatedContainerExample

extends

StatefulWidget

{

 

const

AnimatedContainerExample

(

{

super

.

key

}

)

;

  @override

  State

<

AnimatedContainerExample

createState

(

)

=>

_AnimatedContainerExampleState

(

)

;

}

class

_AnimatedContainerExampleState

   

extends

State

<

AnimatedContainerExample

{

bool

_isLarge

=

false

;

  @override

  Widget build

(

BuildContext context

)

{

   

return

Column

(

      children

:

[

       

// Tap to toggle size

        GestureDetector

(

          onTap

:

(

)

=>

setState

(

(

)

=>

_isLarge

=

!

_isLarge

)

,

          child

:

AnimatedContainer

(

            duration

:

const

Duration

(

milliseconds

:

500

)

,

 

// Animation duration

            curve

:

Curves.

easeInOut

,

 

// Animation curve

            width

:

_isLarge

?

200

:

100

,

            height

:

_isLarge

?

200

:

100

,

            color

:

_isLarge

?

Colors.

blue

:

Colors.

red

,

            child

:

Center

(

              child

:

Text

(

                _isLarge

?

'Large'

:

'Small'

,

                style

:

const

TextStyle

(

color

:

Colors.

white

,

fontSize

:

24

)

,

             

)

,

           

)

,

         

)

,

       

)

,

       

const

SizedBox

(

height

:

20

)

,

        ElevatedButton

(

          onPressed

:

(

)

=>

setState

(

(

)

=>

_isLarge

=

!

_isLarge

)

,

          child

:

const

Text

(

'Toggle'

)

,

       

)

,

     

]

,

   

)

;

 

}

}

Common Implicit Animation Widgets

Widget Description
AnimatedContainer Animation when container size, color, and other properties change
AnimatedOpacity Opacity change animation
AnimatedPadding Padding change animation
AnimatedAlign Alignment change animation
AnimatedSwitcher Animation when switching child Widgets
AnimatedDefaultTextStyle Text style change animation

Explicit Animations

Explicit animations use AnimationController to fully control the animation.

Example: Rotation Animation

class

RotationAnimation

extends

StatefulWidget

{

 

const

RotationAnimation

(

{

super

.

key

}

)

;

  @override

  State

<

RotationAnimation

createState

(

)

=>

_RotationAnimationState

(

)

;

}

class

_RotationAnimationState

extends

State

<

RotationAnimation

with

SingleTickerProviderStateMixin

{

  late AnimationController _controller

;

  late Animation

<

double

_animation

;

  @override

 

void

initState

(

)

{

   

super

.

initState

(

)

;

   

// Create an animation controller

    _controller

=

AnimationController

(

      duration

:

const

Duration

(

seconds

:

2

)

,

      vsync

:

this

,

   

)

;

   

// Create a rotation animation (0 to 1 maps to 0 to 2π)

    _animation

=

Tween

<

double

(

begin

:

0

,

end

:

6.28

)

.

animate

(

      CurvedAnimation

(

parent

:

_controller

,

curve

:

Curves.

linear

)

,

   

)

;

   

// Start the animation

    _controller.

repeat

(

)

;

 

}

  @override

 

void

dispose

(

)

{

    _controller.

dispose

(

)

;

   

super

.

dispose

(

)

;

 

}

  @override

  Widget build

(

BuildContext context

)

{

   

return

Center

(

      child

:

AnimatedBuilder

(

        animation

:

_animation

,

        builder

:

(

context

,

child

)

{

         

return

Transform.

rotate

(

            angle

:

_animation.

value

,

 

// Rotation angle

            child

:

const

Icon

(

Icons.

refresh

,

size

:

100

)

,

         

)

;

       

}

,

     

)

,

   

)

;

 

}

}


Staggered Animations

Example: Staggered Animation

class

StaggeredAnimation

extends

StatefulWidget

{

 

const

StaggeredAnimation

(

{

super

.

key

}

)

;

  @override

  State

<

StaggeredAnimation

createState

(

)

=>

_StaggeredAnimationState

(

)

;

}

class

_StaggeredAnimationState

extends

State

<

StaggeredAnimation

with

TickerProviderStateMixin

{

  late AnimationController _controller

;

 

// Individual animations

  late Animation

<

double

_fadeAnimation

;

  late Animation

<

double

_scaleAnimation

;

  late Animation

<

Offset

_slideAnimation

;

  @override

 

void

initState

(

)

{

   

super

.

initState

(

)

;

    _controller

=

AnimationController

(

      duration

:

const

Duration

(

milliseconds

:

1500

)

,

      vsync

:

this

,

   

)

;

   

// Fade-in animation (0-0.5)

    _fadeAnimation

=

Tween

<

double

(

begin

:

0

,

end

:

1

)

.

animate

(

      CurvedAnimation

(

        parent

:

_controller

,

        curve

:

const

Interval

(

0

,

0.5

,

curve

:

Curves.

easeOut

)

,

     

)

,

   

)

;

   

// Scale animation (0.3-0.7)

    _scaleAnimation

=

Tween

<

double

(

begin

:

0.5

,

end

:

1

)

.

animate

(

      CurvedAnimation

(

        parent

:

_controller

,

        curve

:

const

Interval

(

0.3

,

0.7

,

curve

:

Curves.

elasticOut

)

,

     

)

,

   

)

;

   

// Slide-in animation (0.5-1.0)

    _slideAnimation

=

Tween

<

Offset

(

begin

:

const

Offset

(

0

,

0.5

)

,

      end

:

Offset.

zero

,

   

)

.

animate

(

      CurvedAnimation

(

        parent

:

_controller

,

        curve

:

const

Interval

(

0.5

,

1

,

curve

:

Curves.

easeOutCubic

)

,

     

)

,

   

)

;

    _controller.

forward

(

)

;

 

}

  @override

 

void

dispose

(

)

{

    _controller.

dispose

(

)

;

   

super

.

dispose

(

)

;

 

}

  @override

  Widget build

(

BuildContext context

)

{

   

return

AnimatedBuilder

(

      animation

:

_controller

,

      builder

:

(

context

,

child

)

{

       

return

FadeTransition

(

          opacity

:

_fadeAnimation

,

          child

:

ScaleTransition

(

            scale

:

_scaleAnimation

,

            child

:

SlideTransition

(

              position

:

_slideAnimation

,

              child

:

const

Card

(

                child

:

Padding

(

                  padding

:

EdgeInsets.

all

(

24

)

,

                  child

:

Text

(

'Welcome'

)

,

               

)

,

             

)

,

           

)

,

         

)

,

       

)

;

     

}

,

   

)

;

 

}

}


Hero Animation

Hero animations are used to create shared element transition effects during page switches.

Example: Hero Animation

// Home page - image with Hero

class

HomePage

extends

StatelessWidget

{

 

const

HomePage

(

{

super

.

key

}

)

;

  @override

  Widget build

(

BuildContext context

)

{

   

return

Scaffold

(

      appBar

:

AppBar

(

title

:

const

Text

(

'Home'

)

)

,

      body

:

Center

(

        child

:

GestureDetector

(

          onTap

:

(

)

=>

Navigator.

push

(

            context

,

            MaterialPageRoute

(

builder

:

(

_

)

=>

const

DetailPage

(

)

)

,

         

)

,

          child

:

Hero

(

            tag

:

'myHero'

,

 

// Same tag

            child

:

Image.

network

(

             

'https://picsum.photos/200'

,

              width

:

200

,

              height

:

200

,

           

)

,

         

)

,

       

)

,

     

)

,

   

)

;

 

}

}

// Detail page - image with Hero

class

DetailPage

extends

StatelessWidget

{

 

const

DetailPage

(

{

super

.

key

}

)

;

  @override

  Widget build

(

BuildContext context

)

{

   

return

Scaffold

(

      appBar

:

AppBar

(

title

:

const

Text

(

'Details'

)

)

,

      body

:

Center

(

        child

:

Hero

(

          tag

:

'myHero'

,

 

// Same tag

          child

:

Image.

network

(

           

'https://picsum.photos/200'

,

            width

:

300

,

            height

:

300

,

         

)

,

       

)

,

     

)

,

   

)

;

 

}

}

Implicit animations are suitable for simple scenarios, while explicit animations are suitable for complex control.

Hero animations are the best choice for page switches.