我试着做一个类,如果你点击一个按钮,它会长出来,然后再次收缩。但是动画不起作用
这就是我所拥有的
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,
),
);
}
}如果我给出了buttonHeight和buttonWidth值,并且所有东西都是静态的,那么它可以工作,但是我希望能够这样使用它。
AnimatedShadowButton(
height: 60.0,
width: 100.0,
finalHeight: 90.0,
finalWidth: 150.0,
),发布于 2019-08-14 08:07:49
我认为你必须改变aniButo方法。如下所示:
void aniButo() {
setState(() {
buttonHeight = buttonHeight == widget.height
? widget.finalHeight
: widget.height;
buttonWidth = buttonWidth == widget.width
? widget.finalWidth
: widget.width;
});
}UPD
在_AnimatedShadowButtonState中添加initState方法
@override
void initState() {
buttonHeight = widget.height
buttonWidth = widget.width
super.initState();
}https://stackoverflow.com/questions/57490487
复制相似问题