gpt4 book ai didi

flutter - AnimatedContainer 按钮类未设置动画

转载 作者:IT王子 更新时间:2023-10-29 07:15:31 26 4
gpt4 key购买 nike

我正在尝试制作一个类,如果您单击一个按钮,它会变大,然后再次单击会缩小。但是动画不工作

这是我的

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,
),
);
}
}

如果我给 buttonHeightbuttonWidth 值,并且一切都是静态的,它就可以工作,但我希望能够这样使用它

AnimatedShadowButton(
height: 60.0,
width: 100.0,
finalHeight: 90.0,
finalWidth: 150.0,
),

最佳答案

我认为您必须更改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();
}

关于flutter - AnimatedContainer 按钮类未设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57490487/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com