Skip to content

Commit 89f57bc

Browse files
committed
feat(Touchable): add TouchableHightlight widget and TouchaleOpacity widget
1 parent 2e1a9fa commit 89f57bc

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

lib/flutter_weui.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ export 'switch.dart';
1010
export 'weui_icon.dart';
1111
export 'load_more.dart';
1212
export 'dialog.dart';
13+
export 'touchable.dart';

lib/touchable.dart

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import 'package:flutter/material.dart';
2+
3+
/// 按下时高亮
4+
class TouchableHighlight extends StatefulWidget {
5+
TouchableHighlight({Key key, @required this.child, this.onPressed}) : super(key: key);
6+
7+
final Widget child;
8+
final VoidCallback onPressed;
9+
@override
10+
_TouchableHighlightState createState() => _TouchableHighlightState();
11+
}
12+
13+
class _TouchableHighlightState extends State<TouchableHighlight> {
14+
Color bg = Color(0xFFFFFFFF);
15+
16+
_onTapDown(e) {
17+
setState(() {
18+
bg = Color(0xFFDEDEDE);
19+
});
20+
}
21+
22+
_onTapUp(e) {
23+
setState(() {
24+
bg = Color(0xFFFFFFFF);
25+
});
26+
if (widget.onPressed != null) widget.onPressed();
27+
}
28+
29+
@override
30+
Widget build(BuildContext context) {
31+
return GestureDetector(
32+
onTapDown: this._onTapDown,
33+
onTapUp: this._onTapUp,
34+
child: Container(
35+
color: bg,
36+
child: widget.child,
37+
),
38+
);
39+
}
40+
}
41+
42+
/// 按下时不透明度发生变化
43+
class TouchableOpacity extends StatefulWidget {
44+
TouchableOpacity({Key key, @required this.child, this.onPressed}) : super(key: key);
45+
46+
final Widget child;
47+
final VoidCallback onPressed;
48+
49+
@override
50+
_TouchableOpacityState createState() => _TouchableOpacityState();
51+
}
52+
53+
class _TouchableOpacityState extends State<TouchableOpacity> {
54+
double opacity = 1;
55+
56+
_onTapDown(e) {
57+
setState(() {
58+
opacity = 0.2;
59+
});
60+
}
61+
62+
_onTapUp(e) {
63+
setState(() {
64+
opacity = 1;
65+
});
66+
if (widget.onPressed != null) widget.onPressed();
67+
}
68+
69+
@override
70+
Widget build(BuildContext context) {
71+
return GestureDetector(
72+
onTapDown: this._onTapDown,
73+
onTapUp: this._onTapUp,
74+
child: Opacity(
75+
opacity: opacity,
76+
child: widget.child,
77+
),
78+
);
79+
}
80+
}

0 commit comments

Comments
 (0)