perfect IconToggle

master
lmy 5 years ago
parent 4e74c20352
commit ac95bee85f

@ -41,6 +41,7 @@ class _IconToggleState extends State<IconToggle>
with SingleTickerProviderStateMixin { with SingleTickerProviderStateMixin {
AnimationController _controller; AnimationController _controller;
Animation<double> _position; Animation<double> _position;
bool _cancel = false;
@override @override
void initState() { void initState() {
@ -51,7 +52,9 @@ class _IconToggleState extends State<IconToggle>
reverseDuration: Duration(milliseconds: 50)); reverseDuration: Duration(milliseconds: 50));
_position = CurvedAnimation(parent: _controller, curve: Curves.linear); _position = CurvedAnimation(parent: _controller, curve: Curves.linear);
_position.addStatusListener((status) { _position.addStatusListener((status) {
if (status == AnimationStatus.dismissed && widget.onChanged != null) { if (status == AnimationStatus.dismissed &&
widget.onChanged != null &&
_cancel == false) {
widget.onChanged(!widget.value); widget.onChanged(!widget.value);
} }
}); });
@ -66,12 +69,20 @@ class _IconToggleState extends State<IconToggle>
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return GestureDetector( return GestureDetector(
behavior: HitTestBehavior.opaque,
onTapDown: (event) { onTapDown: (event) {
_cancel = false;
_controller?.forward(); _controller?.forward();
}, },
onTapUp: (event) { onTapUp: (event) {
_controller?.reverse(); _controller?.reverse();
}, },
onTapCancel: () {
_cancel = true;
_controller?.reverse();
},
child: Padding(
padding: const EdgeInsets.all(10.0),
child: _IconToggleable<double>( child: _IconToggleable<double>(
listenable: _position, listenable: _position,
activeColor: widget.activeColor, 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) { void paint(Canvas canvas, Size size) {
final Paint paint = Paint() final Paint paint = Paint()
..color = Color.lerp(inactiveColor, activeColor, _value) ..color = Color.lerp(inactiveColor, activeColor, _value)
.withOpacity(math.min(_value, 0.1)) .withOpacity(math.min(_value, 0.15))
..style = PaintingStyle.fill ..style = PaintingStyle.fill
..strokeWidth = 2.0; ..strokeWidth = 2.0;
canvas.drawCircle( canvas.drawCircle(

Loading…
Cancel
Save