-
Notifications
You must be signed in to change notification settings - Fork 25
/
context_menu_button.dart
170 lines (152 loc) · 5.2 KB
/
context_menu_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
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
import 'package:flutter/material.dart';
typedef Widget ContextMenuButtonBuilder(
BuildContext context,
ContextMenuButtonConfig config, [
ContextMenuButtonStyle? style,
]);
/// The default ContextMenu button. To provide your own, override [ContextMenuOverlay] buttonBuilder.
/// TODO: This should use FocusableControlBuilder?
class ContextMenuButton extends StatefulWidget {
const ContextMenuButton(
this.config, {
this.style,
super.key,
});
final ContextMenuButtonConfig config;
final ContextMenuButtonStyle? style;
@override
_ContextMenuButtonState createState() => _ContextMenuButtonState();
}
class _ContextMenuButtonState extends State<ContextMenuButton> {
bool _isMouseOver = false;
set isMouseOver(bool isMouseOver) {
setState(() => _isMouseOver = isMouseOver);
}
ContextMenuButtonConfig get config => widget.config;
@override
Widget build(BuildContext context) {
bool isDisabled = widget.config.onPressed == null;
bool showMouseOver = _isMouseOver && !isDisabled;
final ThemeData theme = Theme.of(context);
Color defaultTextColor = theme.brightness == Brightness.dark ? Colors.white : Colors.black;
TextStyle? defaultTextStyle = theme.textTheme.bodyMedium?.copyWith(color: theme.colorScheme.onSecondary);
TextStyle? shortcutTextStyle = theme.textTheme.bodySmall?.copyWith(color: theme.colorScheme.onSecondary);
ContextMenuButtonStyle style = ContextMenuButtonStyle(
textStyle: widget.style?.textStyle ?? defaultTextStyle,
shortcutTextStyle: widget.style?.shortcutTextStyle ?? shortcutTextStyle,
fgColor: widget.style?.fgColor ?? defaultTextColor,
bgColor: widget.style?.bgColor ?? Colors.transparent,
hoverBgColor: widget.style?.hoverBgColor ?? theme.colorScheme.primaryContainer,
hoverFgColor: widget.style?.hoverFgColor ?? theme.colorScheme.secondary,
padding: widget.style?.padding ?? EdgeInsets.all(6),
);
/// Handling our own clicks
return GestureDetector(
onTapDown: (_) => isMouseOver = true,
onTapUp: (_) {
isMouseOver = false;
widget.config.onPressed?.call();
},
child: MouseRegion(
onEnter: (_) => isMouseOver = true,
onExit: (_) => isMouseOver = false,
cursor: !isDisabled ? SystemMouseCursors.click : MouseCursor.defer,
child: Opacity(
opacity: isDisabled ? style.disabledOpacity : 1,
child: Container(
padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
width: double.infinity,
color: showMouseOver ? style.hoverBgColor : style.bgColor,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
/// Optional Icon
if (config.icon != null) ...[
SizedBox(
width: 16,
height: 16,
child: (showMouseOver) ? config.iconHover ?? config.icon! : config.icon!,
),
SizedBox(width: 16)
],
/// Main Label
Text(
config.label,
style: style.textStyle!.copyWith(
color: showMouseOver ? style.hoverFgColor : style.fgColor,
),
),
Spacer(),
/// Shortcut Label
if (config.shortcutLabel != null) ...[
Opacity(
opacity: showMouseOver ? 1 : .7,
child: Text(
config.shortcutLabel!,
style: (style.shortcutTextStyle ?? style.textStyle!)
.copyWith(color: showMouseOver ? style.hoverFgColor : style.fgColor),
),
)
]
],
),
),
),
),
);
}
}
class ContextMenuButtonStyle {
const ContextMenuButtonStyle({
this.fgColor,
this.bgColor,
this.hoverFgColor,
this.hoverBgColor,
this.padding,
this.textStyle,
this.shortcutTextStyle,
this.disabledOpacity = 0.7,
});
final Color? fgColor;
final Color? bgColor;
final Color? hoverFgColor;
final Color? hoverBgColor;
final EdgeInsets? padding;
final TextStyle? textStyle;
final TextStyle? shortcutTextStyle;
final double disabledOpacity;
ContextMenuButtonStyle copyWith({
Color? fgColor,
Color? bgColor,
Color? hoverFgColor,
Color? hoverBgColor,
EdgeInsets? padding,
TextStyle? textStyle,
TextStyle? shortcutTextStyle,
double disabledOpacity = 0.7,
}) {
return ContextMenuButtonStyle(
fgColor: fgColor ?? this.fgColor,
bgColor: bgColor ?? this.bgColor,
hoverFgColor: hoverFgColor ?? this.hoverFgColor,
hoverBgColor: hoverBgColor ?? this.hoverBgColor,
padding: padding ?? this.padding,
textStyle: textStyle ?? this.textStyle,
disabledOpacity: disabledOpacity,
);
}
}
class ContextMenuButtonConfig {
ContextMenuButtonConfig(
this.label, {
required this.onPressed,
this.shortcutLabel,
this.icon,
this.iconHover,
});
final String label;
final String? shortcutLabel;
final VoidCallback? onPressed;
final Widget? icon;
final Widget? iconHover;
}