简体   繁体   English

动画在颤动中无法正常工作?

[英]Animation not working properly in flutter?

在此处输入图片说明

I want 1st circle to go up and come down when I press the click button!当我按下click按钮时,我希望第一个圆圈上升和下降!

the animation is not working.动画不起作用。 I tried restarting the app many times but the problem still remains.我多次尝试重新启动应用程序,但问题仍然存在。 what is the problem?问题是什么?

import 'package:flutter/material.dart';

    class Pin extends StatefulWidget {
    @override
    _PinState createState() => _PinState();
    }

    class _PinState extends State<Pin> with SingleTickerProviderStateMixin {
    AnimationController _controller;
    Animation<EdgeInsets> _animation;
    @override
    void initState() {
        super.initState();
        _controller = AnimationController(
        vsync: this,
        duration: Duration(milliseconds: 300),
        );

        _animation = EdgeInsetsTween(
        begin: EdgeInsets.only(top: 0, left: 0, right: 0, bottom: 0),
        end: EdgeInsets.only(top: 0, left: 0, right: 0, bottom: 12),
        ).animate(CurvedAnimation(
        parent: _controller,
        curve: Curves.easeInOut,
        ));

        //_controller.forward();
        _controller.addStatusListener((status) {
        if (status == AnimationStatus.completed) {
            _controller.reverse();
        }
        });
    }

    @override
    Widget build(BuildContext context) {
        return Container(
        padding: const EdgeInsets.all(100),
        width: double.infinity,
        height: MediaQuery.of(context).size.height * 0.6,
        color: const Color.fromRGBO(113, 201, 206, 1),
        child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: <Widget>[

            Container(
                width: 233,
                height: 44,
                color: Colors.blue,
                child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                    Container(
                    padding: _animation.value,
                    height: double.infinity,
                    color: Colors.red,
                    alignment: Alignment.bottomCenter,
                    child: Container(
                        width: 32,
                        height: 32,
                        decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(16),
                        ),
                    ),
                    ),
                    Container(
                    height: double.infinity,
                    alignment: Alignment.bottomCenter,
                    child: Container(
                        width: 32,
                        height: 32,
                        decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(16),
                        ),
                    ),
                    ),
                    Container(
                    height: double.infinity,
                    alignment: Alignment.bottomCenter,
                    child: Container(
                        width: 32,
                        height: 32,
                        decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(16),
                        ),
                    ),
                    ),
                    Container(
                    height: double.infinity,
                    alignment: Alignment.bottomCenter,
                    child: Container(
                        width: 32,
                        height: 32,
                        decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(16),
                        ),
                    ),
                    ),
                ],
                ),
            ),
            Container(
                child: RaisedButton(
                onPressed: () {
                    setState(() {
                    _controller.stop();
                    _controller.forward();
                    print("Animation Started");
                    });
                },
                child: Text("Chick!"),
                ),
            ),
            ],
        ),
        );
    }
    }

all the elements are made up of containers where this has one large blue rectangle container and inside it has, small four containers and inside it has a container with a rounded corner.所有元素都由容器组成,其中有一个大的蓝色矩形容器,里面有四个小的容器,里面有一个带圆角的容器。

Your code looks fine, but one piece is missing,您的代码看起来不错,但缺少一块,

That you should listen to each change of your AnimationController and each time rebuild that part of your view that should be animated.您应该聆听 AnimationController 的每次更改,并且每次都重新构建应该设置动画的视图部分。

Just add that in your initState() method:只需在您的initState()方法中添加:

_controller.addListener((){
    setState(() {});
});

Also, you could remove setState from RaisedButton onPressed: , it's not necessary to have it there.此外,您可以从RaisedButton onPressed:删除setState ,没有必要将它RaisedButton onPressed:那里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM