本文主要解决快速按下松开后,仍然可以看到背景色的变化。
在flutter中有个AnimatedContainer,其可以实现按下时背景色变化,松开后背景色还原。但是快速按下松开后,其背景色无变化。原因是,背景色变化需要一个过程,而快速按下松开后就立即结束动画响应事件了。
自定义实现代码如下:
class StateButton extends StatefulWidget {
final double width;
final double height;
final double radius;
final Color normal;
final Color press;
final Widget fore;
final VoidCallback onPressed;
StateButton(
{Key key,
this.width,
this.height,
this.radius,
this.normal,
this.press,
this.fore,
this.onPressed})
: super(key: key);
@override
State<StatefulWidget> createState() => _StateButton(normal, press);
}
enum _State { None, Down, Up, Cancel }
class _StateButton extends State<StateButton>
with SingleTickerProviderStateMixin {
var _state = _State.None;
AnimationController _controller;
Animation<Color> _animati