Flutter 未设置动画的AnimatedContainer按钮类

Flutter 未设置动画的AnimatedContainer按钮类,flutter,flutter-animation,Flutter,Flutter Animation,我试图创建一个类,如果你点击一个按钮,它会增长,然后再次点击会收缩。但是动画不起作用 这就是我所拥有的 import 'package:flutter/material.dart'; import 'package:flutter_button_collection/flutter_button_collection.dart'; class AnimatedShadowButton extends StatefulWidget { final double height; final

我试图创建一个类,如果你点击一个按钮,它会增长,然后再次点击会收缩。但是动画不起作用

这就是我所拥有的

import 'package:flutter/material.dart';
import 'package:flutter_button_collection/flutter_button_collection.dart';

class AnimatedShadowButton extends StatefulWidget {
  final double height;
  final double width;
  final double finalHeight;
  final double finalWidth;

  const AnimatedShadowButton(
      {Key key, this.height, this.width, this.finalHeight, this.finalWidth})
      : assert(height < finalHeight),
        assert(width < finalWidth),
        super(key: key);

  @override
  _AnimatedShadowButtonState createState() => _AnimatedShadowButtonState();
}

class _AnimatedShadowButtonState extends State<AnimatedShadowButton> {
  double buttonHeight;
  double buttonWidth;

  void aniButo() {
    setState(() {
      buttonHeight = widget.height <= widget.finalHeight
          ? widget.finalHeight
          : widget.height;
      buttonWidth =
          widget.width <= widget.finalWidth ? widget.finalWidth : widget.width;
    });
  }

  Widget build(BuildContext context) {
    return AnimatedContainer(
      duration: Duration(milliseconds: 300),
      curve: Curves.easeInBack,
      width: buttonWidth,
      height: buttonHeight,
      child: AVLButton(
        onPressed: () {
          aniButo();
        },
        child: Text("This is a text"),
        elevation: 30.0,
      ),
    );
  }
}


我想你必须改变aniButo的方法。像这样:

void aniButo() {
  setState(() {
    buttonHeight = buttonHeight == widget.height
        ? widget.finalHeight
        : widget.height;
    buttonWidth = buttonWidth == widget.width 
        ? widget.finalWidth 
        : widget.width;
  });
}
UPD

内部
\u AnimatedShadowButtonState
add
initState
方法

@override
void initState() {
  buttonHeight = widget.height
  buttonWidth = widget.width
  super.initState();
}

我想你必须改变aniButo的方法。像这样:

void aniButo() {
  setState(() {
    buttonHeight = buttonHeight == widget.height
        ? widget.finalHeight
        : widget.height;
    buttonWidth = buttonWidth == widget.width 
        ? widget.finalWidth 
        : widget.width;
  });
}
UPD

内部
\u AnimatedShadowButtonState
add
initState
方法

@override
void initState() {
  buttonHeight = widget.height
  buttonWidth = widget.width
  super.initState();
}

它有帮助,但它并不完全是它应该做的,在第一次点击它跳到给定的按钮大小,然后动画,我可以不知何故使它有小部件。从乞讨高度或至少不跳?似乎是因为你没有初始化按钮大小。我已经更新了答案。它有帮助,但它并不完全是它应该做的,在第一次点击它跳到给定的按钮大小,然后动画,我可以不知何故使它有小部件。从乞讨高度或至少不跳?似乎是因为你没有初始化按钮大小。我已经更新了答案