AnimatedContainer 按钮 class 未设置动画
AnimatedContainer Button class not being animated
我正在尝试制作一个 class 如果你点击一个按钮它会变大,然后再次点击会缩小。但是动画不工作
这就是我的
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,
),
我认为您必须更改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();
}
我正在尝试制作一个 class 如果你点击一个按钮它会变大,然后再次点击会缩小。但是动画不工作
这就是我的
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,
),
我认为您必须更改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();
}