代码之家  ›  专栏  ›  技术社区  ›  DreamsInHD

颤振动画:如何保持父控件的位置

  •  0
  • DreamsInHD  · 技术社区  · 6 年前

    我在拍《颤栗》的动画时遇到了一些麻烦。 有三个圆圈彼此相邻。当其中一个被按下时,我希望它膨胀和折叠。动画可以工作,但当发生这种情况时,整排圆向下移动,以保持圆所在行的上边距。如何确保行保持其原始位置?

    我只是将动画应用到中心圆来测试它。很抱歉,如果代码很混乱,那是因为我在测试这个。这是动画和动画控制器:

        _animationController =
            AnimationController(vsync: this, duration: Duration(milliseconds: 200));
        _animation = Tween(begin: 0.25, end: 0.35).animate(
            CurvedAnimation(parent: _animationController, curve: Curves.elasticIn))
          ..addStatusListener((status) {
            if (status == AnimationStatus.completed) {
              _animationController.reverse();
            }
          });
    

    Column(
            children: <Widget>[
    Container(
                margin: EdgeInsets.only(top: 16.0),
                child: Center(
                  child: Text(
                    'Circles',
                    style: TextStyle(fontSize: 18),
                  ),
                ),
              ),
    Container(
                margin: EdgeInsets.only(top: 8.0),
                child: Row(
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    Container(
                      width: MediaQuery.of(context).size.width * 0.25,
                      height: MediaQuery.of(context).size.height * 0.2,
                      margin: EdgeInsets.symmetric(horizontal: 8.0),
                      child: Center(child: Text('Circle')),
                      decoration: BoxDecoration(
                          shape: BoxShape.circle,
                          border: Border.all(color: AppColors.primaryBlue)),
                    ),
                    AnimatedBuilder(
                      animation: _animation,
                      builder: (context, child) {
                        return GestureDetector(
                          onTap: () {
                            _animationController.forward();
                          },
                          child: Container(
                            width: MediaQuery.of(context).size.width *
                                _animation.value,
                            height: MediaQuery.of(context).size.height *
                                _animation.value,
                            margin: EdgeInsets.symmetric(horizontal: 8.0),
                            child: Center(child: Text('Circle')),
                            decoration: BoxDecoration(
                                shape: BoxShape.circle,
                                border: Border.all(color: AppColors.primaryBlue)),
                          ),
                        );
                      },
                    ),
                    Container(
                      width: MediaQuery.of(context).size.width * 0.25,
                      height: MediaQuery.of(context).size.height * 0.2,
                      margin: EdgeInsets.symmetric(horizontal: 8.0),
                      child: Center(child: Text('Circle')),
                      decoration: BoxDecoration(
                          shape: BoxShape.circle,
                          border: Border.all(color: AppColors.primaryBlue)),
                    )
                  ],
                ),
              ),
    

    你可能会注意到我对颤振中的动画还很陌生,所以其他的反馈也是非常欢迎的。谢谢!

    0 回复  |  直到 6 年前
        1
  •  1
  •   CopsOnRoad    6 年前

    输出:

    enter image description here

    完整代码:

    Column(
      children: <Widget>[
        Container(
          margin: EdgeInsets.only(top: 16.0),
          child: Center(
            child: Text(
              'Circles',
              style: TextStyle(fontSize: 18),
            ),
          ),
        ),
        Container(
          height: 200,
          margin: EdgeInsets.only(top: 8.0),
          child: Row(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              Container(
                width: MediaQuery.of(context).size.width * 0.25,
                height: MediaQuery.of(context).size.height * 0.2,
                margin: EdgeInsets.symmetric(horizontal: 8.0),
                child: Center(child: Text('Circle')),
                decoration: BoxDecoration(shape: BoxShape.circle, border: Border.all(color: AppColors.primaryBlue)),
              ),
              Spacer(),
              AnimatedBuilder(
                animation: _animation,
                builder: (context, child) {
                  return GestureDetector(
                    onTap: () {
                      _animationController.forward();
                    },
                    child: Container(
                      width: MediaQuery.of(context).size.width * _animation.value,
                      height: MediaQuery.of(context).size.height * _animation.value,
                      margin: EdgeInsets.symmetric(horizontal: 8.0),
                      child: Center(child: Text('Circle')),
                      decoration: BoxDecoration(shape: BoxShape.circle, border: Border.all(color: AppColors.primaryBlue)),
                    ),
                  );
                },
              ),
              Spacer(),
              Container(
                width: MediaQuery.of(context).size.width * 0.25,
                height: MediaQuery.of(context).size.height * 0.2,
                margin: EdgeInsets.symmetric(horizontal: 8.0),
                child: Center(child: Text('Circle')),
                decoration: BoxDecoration(shape: BoxShape.circle, border: Border.all(color: AppColors.primaryBlue)),
              )
            ],
          ),
        ),
      ],
    );
    
        2
  •  0
  •   Fethi    6 年前

    首先我建议你删除 mainAxisSize: MainAxisSize.min,