From ac95bee85f2aec4d34fdec21ebaddb2d14127487 Mon Sep 17 00:00:00 2001 From: lmy <2534290808@qq.com> Date: Thu, 5 Sep 2019 14:41:16 +0800 Subject: [PATCH] perfect IconToggle --- lib/icon_toggle.dart | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/lib/icon_toggle.dart b/lib/icon_toggle.dart index c11e1a5..d6bf74a 100644 --- a/lib/icon_toggle.dart +++ b/lib/icon_toggle.dart @@ -41,6 +41,7 @@ class _IconToggleState extends State with SingleTickerProviderStateMixin { AnimationController _controller; Animation _position; + bool _cancel = false; @override void initState() { @@ -51,7 +52,9 @@ class _IconToggleState extends State reverseDuration: Duration(milliseconds: 50)); _position = CurvedAnimation(parent: _controller, curve: Curves.linear); _position.addStatusListener((status) { - if (status == AnimationStatus.dismissed && widget.onChanged != null) { + if (status == AnimationStatus.dismissed && + widget.onChanged != null && + _cancel == false) { widget.onChanged(!widget.value); } }); @@ -66,25 +69,34 @@ class _IconToggleState extends State @override Widget build(BuildContext context) { return GestureDetector( + behavior: HitTestBehavior.opaque, onTapDown: (event) { + _cancel = false; _controller?.forward(); }, onTapUp: (event) { _controller?.reverse(); }, - child: _IconToggleable( - listenable: _position, - activeColor: widget.activeColor, - inactiveColor: widget.inactiveColor, - child: AnimatedSwitcher( - duration: widget.duration, - reverseDuration: widget.reverseDuration, - transitionBuilder: widget.transitionBuilder, - child: Icon( - widget.value ? widget.checkedIconData : widget.uncheckedIconData, - color: widget.value ? widget.activeColor : widget.inactiveColor, - size: 22, - key: ValueKey(widget.value), + onTapCancel: () { + _cancel = true; + _controller?.reverse(); + }, + child: Padding( + padding: const EdgeInsets.all(10.0), + child: _IconToggleable( + listenable: _position, + activeColor: widget.activeColor, + inactiveColor: widget.inactiveColor, + child: AnimatedSwitcher( + duration: widget.duration, + reverseDuration: widget.reverseDuration, + transitionBuilder: widget.transitionBuilder, + child: Icon( + widget.value ? widget.checkedIconData : widget.uncheckedIconData, + color: widget.value ? widget.activeColor : widget.inactiveColor, + size: 22, + key: ValueKey(widget.value), + ), ), ), ), @@ -131,7 +143,7 @@ class _IconPainter extends CustomPainter { void paint(Canvas canvas, Size size) { final Paint paint = Paint() ..color = Color.lerp(inactiveColor, activeColor, _value) - .withOpacity(math.min(_value, 0.1)) + .withOpacity(math.min(_value, 0.15)) ..style = PaintingStyle.fill ..strokeWidth = 2.0; canvas.drawCircle(