Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
505 views
in Technique[技术] by (71.8m points)

flutter - How do I animate a container when the page is initializing;

class _MaintenanceState extends State<Maintenance> {

Function below is supposed to initialise the animation

    @override
    void initState() {
      super.initState();
        _animate();
    }

var loginWidth = 0.0;
Curve _curve = Curves.fastOutSlowIn;
_animate() {
  setState(() {
    loginWidth == 0.0 ? loginWidth = 130 : loginWidth = 0.0;
  });
}

I tried placing the width of the animated container in the initState but it still doesn't animate.

                  AnimatedContainer(
                    duration: Duration(seconds: 1),
                    width: loginWidth,
                    height: 60.0,
                    decoration: BoxDecoration(

                      color: Colors.green[600],
                      borderRadius: BorderRadius.circular(20.0),
                    ),
                    child: Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Row(
                        children: [
                          Text("Add 
Project"),
                              )),
                          Spacer(),
                          Image(
                            image: AssetImage(
                              "assets/images/add_icon.png",
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),

Any suggestions would be much appreciated

question from:https://stackoverflow.com/questions/65643022/how-do-i-animate-a-container-when-the-page-is-initializing

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are several things you have to consider to animate.

  1. use AnimationController to track the currentstate of animation, to set the duration till which you want to animate and other several properties if you want to add in animation.
  2. if you are using animation you have to use SingleTickerProviderStateMixin or MultiTickerProviderStateMixin mixin in your class.

I am sharing my GitHub project link in which I used a simple animation you can refer to it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...