要解决布尔控制器在动画容器内不起作用的问题,可以尝试以下几种方法:
AnimationController _controller;
@override
void initState() {
super.initState();
_controller = AnimationController(
duration: const Duration(seconds: 1),
vsync: this,
);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
void _startAnimation() {
_controller.forward();
}
void _stopAnimation() {
_controller.reset();
}
@override
Widget build(BuildContext context) {
return Column(
children: [
AnimatedBuilder(
animation: _controller,
builder: (BuildContext context, Widget child) {
return Container(
height: _controller.value * 100,
width: _controller.value * 100,
color: Colors.blue,
);
},
),
RaisedButton(
onPressed: _startAnimation,
child: Text('Start Animation'),
),
RaisedButton(
onPressed: _stopAnimation,
child: Text('Stop Animation'),
),
],
);
}
bool _isAnimating = false;
void _startAnimation() {
setState(() {
_isAnimating = true;
});
}
void _stopAnimation() {
setState(() {
_isAnimating = false;
});
}
@override
Widget build(BuildContext context) {
return Column(
children: [
AnimatedContainer(
duration: const Duration(seconds: 1),
curve: Curves.easeIn,
height: _isAnimating ? 100 : 0,
width: _isAnimating ? 100 : 0,
color: Colors.blue,
),
RaisedButton(
onPressed: _startAnimation,
child: Text('Start Animation'),
),
RaisedButton(
onPressed: _stopAnimation,
child: Text('Stop Animation'),
),
],
);
}
在上述示例中,布尔控制器 _isAnimating
用来决定是否执行动画。通过点击按钮来改变 _isAnimating
的值,从而控制动画的开始和停止。
这些示例代码可以在Flutter中使用,用于解决布尔控制器在动画容器内不起作用的问题。
上一篇:布尔空引用异常
下一篇:布尔括号化问题的代码不起作用。