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
part of cool_ui;
class CupertinoPopoverMenuList extends StatelessWidget {
final List<Widget> children;
const CupertinoPopoverMenuList({this.children = const []});
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: children.length * 2 - 1,
shrinkWrap: true,
itemBuilder: (context, int i) {
if (i.isOdd) {
// 在每一列之前,添加一个1像素高的分隔线widget
return const Divider(
height: 1.0,
);
}
final int index = i ~/ 2;
return children[index];
},
padding: EdgeInsets.all(0.0),
);
}
}
class CupertinoPopoverMenuItem extends StatefulWidget {
final Widget? leading;
final Widget child;
final BoolCallback? onTap;
final bool isTapClosePopover;
final Color activeBackground;
final Color background;
const CupertinoPopoverMenuItem(
{this.leading,
required this.child,
this.onTap,
this.background = Colors.white,
this.activeBackground = const Color(0xFFd9d9d9),
this.isTapClosePopover = true});
@override
State<StatefulWidget> createState() => CupertinoPopoverMenuItemState();
}
class CupertinoPopoverMenuItemState extends State<CupertinoPopoverMenuItem> {
bool isDown = false;
@override
Widget build(BuildContext context) {
List<Widget> widgets = [];
if (widget.leading != null) {
widgets.add(Container(
padding: EdgeInsets.only(left: 5.0, right: 5.0),
width: 35.0,
height: 35.0,
child: IconTheme(
data: IconThemeData(color: Color(0xff007aff), size: 20.0),
child: widget.leading!),
));
}
widgets.add(Expanded(
child: DefaultTextStyle(
style: TextStyle(color: Color(0xff007aff), fontSize: 17.0),
child: widget.child)));
return GestureDetector(
onTapDown: (detail) {
setState(() {
isDown = true;
});
},
onTapUp: (detail) {
if (isDown) {
setState(() {
isDown = false;
});
if (widget.onTap != null && widget.onTap!()) {
return;
}
if (widget.isTapClosePopover) {
Navigator.of(context).pop();
}
}
},
onTapCancel: () {
if (isDown) {
setState(() {
isDown = false;
});
}
},
child: Container(
color: isDown ? widget.activeBackground : widget.background,
child: Padding(
padding: EdgeInsets.only(top: 2.5, bottom: 2.5),
child: Row(children: widgets),
),
),
);
}
}