perfect IconToggle

master
lmy 5 years ago
parent 4e74c20352
commit ac95bee85f

@ -41,6 +41,7 @@ class _IconToggleState extends State<IconToggle>
with SingleTickerProviderStateMixin {
AnimationController _controller;
Animation<double> _position;
bool _cancel = false;
@override
void initState() {
@ -51,7 +52,9 @@ class _IconToggleState extends State<IconToggle>
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,12 +69,20 @@ class _IconToggleState extends State<IconToggle>
@override
Widget build(BuildContext context) {
return GestureDetector(
behavior: HitTestBehavior.opaque,
onTapDown: (event) {
_cancel = false;
_controller?.forward();
},
onTapUp: (event) {
_controller?.reverse();
},
onTapCancel: () {
_cancel = true;
_controller?.reverse();
},
child: Padding(
padding: const EdgeInsets.all(10.0),
child: _IconToggleable<double>(
listenable: _position,
activeColor: widget.activeColor,
@ -88,6 +99,7 @@ class _IconToggleState extends State<IconToggle>
),
),
),
),
);
}
}
@ -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(

Loading…
Cancel
Save