-
Notifications
You must be signed in to change notification settings - Fork 3
/
touch_feedback.dart
114 lines (104 loc) · 2.93 KB
/
touch_feedback.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import 'package:flutter/material.dart';
import 'package:icapps_architecture/icapps_architecture.dart';
/// Helper class for platform-specific touch feedback
///
/// On devices running with the android theme, this will create a ripple effect,
/// on other devices, this will create a scaling touch down effect
class TouchFeedBack extends StatelessWidget {
final Widget child;
final VoidCallback? onClick;
final Color? androidSplashColor;
final Color color;
final BorderRadius? borderRadius;
final double elevation;
final ShapeBorder? shapeBorder;
const TouchFeedBack({
required this.child,
required this.onClick,
this.borderRadius,
this.androidSplashColor,
this.color = Colors.transparent,
this.elevation = 0,
this.shapeBorder,
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
if (context.isAndroidTheme) {
return _buildAndroid();
}
return TouchFeedBackIOS(
child: child,
onClick: onClick,
color: color,
borderRadius: borderRadius,
elevation: elevation,
shapeBorder: shapeBorder,
);
}
Widget _buildAndroid() {
return Material(
borderRadius: borderRadius,
color: color,
elevation: elevation,
shape: shapeBorder,
child: onClick == null
? child
: InkWell(
customBorder: shapeBorder,
borderRadius: borderRadius,
splashColor: androidSplashColor,
onTap: onClick,
child: child,
),
);
}
}
class TouchFeedBackIOS extends StatefulWidget {
final Widget child;
final VoidCallback? onClick;
final Color color;
final BorderRadius? borderRadius;
final ShapeBorder? shapeBorder;
final double elevation;
const TouchFeedBackIOS({
required this.child,
required this.onClick,
this.borderRadius,
this.color = Colors.transparent,
this.shapeBorder,
this.elevation = 0,
Key? key,
}) : super(key: key);
@override
_TouchFeedBackIOSState createState() => _TouchFeedBackIOSState();
}
class _TouchFeedBackIOSState extends State<TouchFeedBackIOS> {
static const touchScale = 0.98;
static const defaultScale = 1.0;
var touched = false;
@override
Widget build(BuildContext context) {
return GestureDetector(
excludeFromSemantics: widget.onClick == null,
onTapDown: (details) => _setTouched(true),
onTap: widget.onClick,
onTapCancel: () => _setTouched(false),
onTapUp: (details) => _setTouched(false),
child: Transform.scale(
scale: touched ? touchScale : defaultScale,
child: Material(
borderRadius: widget.borderRadius,
color: widget.color,
child: widget.child,
shape: widget.shapeBorder,
elevation: widget.elevation,
),
),
);
}
void _setTouched(bool touched) {
if (widget.onClick == null) return;
setState(() => this.touched = touched);
}
}