-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathcontext_menu.dart
258 lines (228 loc) · 6.08 KB
/
context_menu.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import 'package:flutter/material.dart';
class ContextMenu extends StatefulWidget {
final List<BaseContextMenuItem> entries;
final Widget child;
final bool openOnLongPress;
final bool openOnSecondaryPress;
const ContextMenu({
required this.entries,
required this.child,
this.openOnLongPress = true,
this.openOnSecondaryPress = true,
super.key,
});
@override
State<ContextMenu> createState() => _ContextMenuState();
}
class _ContextMenuState extends State<ContextMenu> {
late Offset lastPosition;
@override
Widget build(BuildContext context) {
return MenuAnchor(
menuChildren: widget.entries.map((e) => e.buildWrapper(context)).toList(),
builder: (context, controller, child) {
return GestureDetector(
onSecondaryTapUp: (details) =>
controller.open(position: details.localPosition),
onLongPressDown: (details) => lastPosition = details.localPosition,
onLongPress: () => controller.open(position: lastPosition),
child: child,
);
},
child: widget.child,
);
}
}
class _EnabledBuilder extends StatelessWidget {
final bool enabled;
final Widget child;
const _EnabledBuilder({
required this.enabled,
required this.child,
});
@override
Widget build(BuildContext context) {
return IgnorePointer(
ignoring: !enabled,
child: Opacity(
opacity: enabled ? 1 : 0.4,
child: child,
),
);
}
}
abstract class BaseContextMenuItem {
final Widget child;
final Widget? leading;
final Widget? trailing;
final bool enabled;
const BaseContextMenuItem({
required this.child,
this.enabled = true,
this.leading,
this.trailing,
});
Widget buildWrapper(BuildContext context) =>
_EnabledBuilder(enabled: enabled, child: build(context));
Widget build(BuildContext context);
Widget? buildLeading(BuildContext context) => leading;
Widget? buildTrailing(BuildContext context) => trailing;
}
class SubmenuMenuItem extends BaseContextMenuItem {
final List<BaseContextMenuItem> menuChildren;
const SubmenuMenuItem({
required super.child,
required this.menuChildren,
super.leading,
super.enabled,
}) : super(trailing: null);
@override
Widget? buildTrailing(BuildContext context) {
return const Icon(Icons.chevron_right, size: 16);
}
@override
Widget build(BuildContext context) {
return SubmenuButton(
menuChildren: menuChildren.map((e) => e.buildWrapper(context)).toList(),
leadingIcon: buildLeading(context),
trailingIcon: buildTrailing(context),
style: const ButtonStyle(
padding: MaterialStatePropertyAll(
EdgeInsets.symmetric(horizontal: 16),
),
),
child: child,
);
}
}
class ContextMenuItem extends BaseContextMenuItem {
final VoidCallback? onTap;
final MenuSerializableShortcut? shortcut;
const ContextMenuItem({
required super.child,
this.onTap,
super.leading,
super.trailing,
this.shortcut,
super.enabled,
});
@override
Widget build(BuildContext context) {
final Widget? leading = buildLeading(context);
return MenuItemButton(
leadingIcon: leading != null
? IconTheme.merge(
data: Theme.of(context).iconTheme.copyWith(size: 16),
child: leading,
)
: null,
trailingIcon: buildTrailing(context),
onPressed: onTap,
shortcut: shortcut,
style: const ButtonStyle(
padding: MaterialStatePropertyAll(
EdgeInsets.symmetric(horizontal: 16),
),
),
child: child,
);
}
}
class RadioMenuItem<T> extends ContextMenuItem {
final T value;
final T? groupValue;
final ValueChanged<T?>? onChanged;
final bool toggleable;
const RadioMenuItem({
required this.value,
required this.groupValue,
this.onChanged,
this.toggleable = false,
required super.child,
super.trailing,
super.shortcut,
super.enabled,
}) : super(leading: null, onTap: null);
@override
VoidCallback? get onTap => onChanged == null
? null
: () {
if (toggleable && groupValue == value) {
onChanged!.call(null);
return;
}
onChanged!.call(value);
};
@override
Widget? buildTrailing(BuildContext context) {
return ExcludeFocus(
child: IgnorePointer(
child: ConstrainedBox(
constraints: const BoxConstraints(
maxHeight: Checkbox.width,
maxWidth: Checkbox.width,
),
child: Radio<T>(
groupValue: groupValue,
value: value,
onChanged: onChanged,
toggleable: toggleable,
),
),
),
);
}
}
class CheckboxMenuItem extends ContextMenuItem {
final bool? value;
final ValueChanged<bool?>? onChanged;
final bool tristate;
const CheckboxMenuItem({
required this.value,
this.onChanged,
this.tristate = false,
required super.child,
super.trailing,
super.shortcut,
super.enabled,
}) : super(leading: null, onTap: null);
@override
VoidCallback? get onTap => onChanged == null
? null
: () {
switch (value) {
case false:
onChanged!.call(true);
break;
case true:
onChanged!.call(tristate ? null : false);
break;
case null:
onChanged!.call(false);
break;
}
};
@override
Widget? buildTrailing(BuildContext context) {
return ExcludeFocus(
child: IgnorePointer(
child: ConstrainedBox(
constraints: const BoxConstraints(
maxHeight: Checkbox.width,
maxWidth: Checkbox.width,
),
child: Checkbox(
value: value,
onChanged: onChanged,
tristate: tristate,
),
),
),
);
}
}
class ContextMenuDivider extends BaseContextMenuItem {
const ContextMenuDivider() : super(child: const SizedBox());
@override
Widget build(BuildContext context) => const Divider();
}