-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjumping_button.dart
78 lines (67 loc) · 1.68 KB
/
jumping_button.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
///[JumpingButton] is a button that has jumping effect.
///It gets scaled down when it is tapped.
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class JumpingButton extends StatefulWidget {
const JumpingButton({
Key key,
@required this.child,
@required this.onTap,
this.upperbound=0.1,
this.lowerbound=0.0,
this.haptic=true,
}) : super(key: key);
final Widget child;
final VoidCallback onTap;
final double upperbound;
final double lowerbound;
final bool haptic;
@override
_JumpingButtonState createState() => _JumpingButtonState();
}
class _JumpingButtonState extends State<JumpingButton> with SingleTickerProviderStateMixin {
double _scale;
AnimationController _controller;
@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: Duration(milliseconds: 100),
lowerBound: widget.lowerbound,
upperBound: widget.upperbound,
);
_controller.addListener(() {
setState(() {});
});
}
@override
void dispose() {
super.dispose();
_controller.dispose();
}
@override
Widget build(BuildContext context) {
_scale = 1 - _controller.value;
return Listener(
onPointerDown: (_) {
if(widget.onTap!=null){
_controller.forward();
if (widget.haptic) HapticFeedback.lightImpact();
}
},
onPointerUp: (_) {
if(widget.onTap!=null){
_controller.reverse();
}
},
child: GestureDetector(
onTap: widget.onTap,
child: Transform.scale(
scale: _scale,
child: widget.child,
),
),
);
}
}