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
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:ui' show lerpDouble;
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'material_state.dart';
import 'theme.dart';
// Examples can assume:
// late BuildContext context;
/// Used to configure how the [PopupMenuButton] positions its popup menu.
enum PopupMenuPosition {
/// Menu is positioned over the anchor.
over,
/// Menu is positioned under the anchor.
under,
}
/// Defines the visual properties of the routes used to display popup menus
/// as well as [PopupMenuItem] and [PopupMenuDivider] widgets.
///
/// Descendant widgets obtain the current [PopupMenuThemeData] object
/// using `PopupMenuTheme.of(context)`. Instances of
/// [PopupMenuThemeData] can be customized with
/// [PopupMenuThemeData.copyWith].
///
/// Typically, a [PopupMenuThemeData] is specified as part of the
/// overall [Theme] with [ThemeData.popupMenuTheme]. Otherwise,
/// [PopupMenuTheme] can be used to configure its own widget subtree.
///
/// All [PopupMenuThemeData] properties are `null` by default.
/// If any of these properties are null, the popup menu will provide its
/// own defaults.
///
/// See also:
///
/// * [ThemeData], which describes the overall theme information for the
/// application.
@immutable
class PopupMenuThemeData with Diagnosticable {
/// Creates the set of properties used to configure [PopupMenuTheme].
const PopupMenuThemeData({
this.color,
this.shape,
this.elevation,
this.shadowColor,
this.surfaceTintColor,
this.textStyle,
this.labelTextStyle,
this.enableFeedback,
this.mouseCursor,
this.position,
this.iconColor,
this.iconSize,
});
/// The background color of the popup menu.
final Color? color;
/// The shape of the popup menu.
final ShapeBorder? shape;
/// The elevation of the popup menu.
final double? elevation;
/// The color used to paint shadow below the popup menu.
final Color? shadowColor;
/// The color used as an overlay on [color] of the popup menu.
final Color? surfaceTintColor;
/// The text style of items in the popup menu.
final TextStyle? textStyle;
/// You can use this to specify a different style of the label
/// when the popup menu item is enabled and disabled.
final MaterialStateProperty<TextStyle?>? labelTextStyle;
/// If specified, defines the feedback property for [PopupMenuButton].
///
/// If [PopupMenuButton.enableFeedback] is provided, [enableFeedback] is ignored.
final bool? enableFeedback;
/// {@macro flutter.material.popupmenu.mouseCursor}
///
/// If specified, overrides the default value of [PopupMenuItem.mouseCursor].
final MaterialStateProperty<MouseCursor?>? mouseCursor;
/// Whether the popup menu is positioned over or under the popup menu button.
///
/// When not set, the position defaults to [PopupMenuPosition.over] which makes the
/// popup menu appear directly over the button that was used to create it.
final PopupMenuPosition? position;
/// The color of the icon in the popup menu button.
final Color? iconColor;
/// The size of the icon in the popup menu button.
final double? iconSize;
/// Creates a copy of this object with the given fields replaced with the
/// new values.
PopupMenuThemeData copyWith({
Color? color,
ShapeBorder? shape,
double? elevation,
Color? shadowColor,
Color? surfaceTintColor,
TextStyle? textStyle,
MaterialStateProperty<TextStyle?>? labelTextStyle,
bool? enableFeedback,
MaterialStateProperty<MouseCursor?>? mouseCursor,
PopupMenuPosition? position,
Color? iconColor,
double? iconSize,
}) {
return PopupMenuThemeData(
color: color ?? this.color,
shape: shape ?? this.shape,
elevation: elevation ?? this.elevation,
shadowColor: shadowColor ?? this.shadowColor,
surfaceTintColor: surfaceTintColor ?? this.surfaceTintColor,
textStyle: textStyle ?? this.textStyle,
labelTextStyle: labelTextStyle ?? this.labelTextStyle,
enableFeedback: enableFeedback ?? this.enableFeedback,
mouseCursor: mouseCursor ?? this.mouseCursor,
position: position ?? this.position,
iconColor: iconColor ?? this.iconColor,
iconSize: iconSize ?? this.iconSize,
);
}
/// Linearly interpolate between two popup menu themes.
///
/// If both arguments are null, then null is returned.
///
/// {@macro dart.ui.shadow.lerp}
static PopupMenuThemeData? lerp(PopupMenuThemeData? a, PopupMenuThemeData? b, double t) {
if (identical(a, b)) {
return a;
}
return PopupMenuThemeData(
color: Color.lerp(a?.color, b?.color, t),
shape: ShapeBorder.lerp(a?.shape, b?.shape, t),
elevation: lerpDouble(a?.elevation, b?.elevation, t),
shadowColor: Color.lerp(a?.shadowColor, b?.shadowColor, t),
surfaceTintColor: Color.lerp(a?.surfaceTintColor, b?.surfaceTintColor, t),
textStyle: TextStyle.lerp(a?.textStyle, b?.textStyle, t),
labelTextStyle: MaterialStateProperty.lerp<TextStyle?>(a?.labelTextStyle, b?.labelTextStyle, t, TextStyle.lerp),
enableFeedback: t < 0.5 ? a?.enableFeedback : b?.enableFeedback,
mouseCursor: t < 0.5 ? a?.mouseCursor : b?.mouseCursor,
position: t < 0.5 ? a?.position : b?.position,
iconColor: Color.lerp(a?.iconColor, b?.iconColor, t),
iconSize: lerpDouble(a?.iconSize, b?.iconSize, t),
);
}
@override
int get hashCode => Object.hash(
color,
shape,
elevation,
shadowColor,
surfaceTintColor,
textStyle,
labelTextStyle,
enableFeedback,
mouseCursor,
position,
iconColor,
iconSize,
);
@override
bool operator ==(Object other) {
if (identical(this, other)) {
return true;
}
if (other.runtimeType != runtimeType) {
return false;
}
return other is PopupMenuThemeData
&& other.color == color
&& other.shape == shape
&& other.elevation == elevation
&& other.shadowColor == shadowColor
&& other.surfaceTintColor == surfaceTintColor
&& other.textStyle == textStyle
&& other.labelTextStyle == labelTextStyle
&& other.enableFeedback == enableFeedback
&& other.mouseCursor == mouseCursor
&& other.position == position
&& other.iconColor == iconColor
&& other.iconSize == iconSize;
}
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(ColorProperty('color', color, defaultValue: null));
properties.add(DiagnosticsProperty<ShapeBorder>('shape', shape, defaultValue: null));
properties.add(DoubleProperty('elevation', elevation, defaultValue: null));
properties.add(ColorProperty('shadowColor', shadowColor, defaultValue: null));
properties.add(ColorProperty('surfaceTintColor', surfaceTintColor, defaultValue: null));
properties.add(DiagnosticsProperty<TextStyle>('text style', textStyle, defaultValue: null));
properties.add(DiagnosticsProperty<MaterialStateProperty<TextStyle?>>('labelTextStyle', labelTextStyle, defaultValue: null));
properties.add(DiagnosticsProperty<bool>('enableFeedback', enableFeedback, defaultValue: null));
properties.add(DiagnosticsProperty<MaterialStateProperty<MouseCursor?>>('mouseCursor', mouseCursor, defaultValue: null));
properties.add(EnumProperty<PopupMenuPosition?>('position', position, defaultValue: null));
properties.add(ColorProperty('iconColor', iconColor, defaultValue: null));
properties.add(DoubleProperty('iconSize', iconSize, defaultValue: null));
}
}
/// An inherited widget that defines the configuration for
/// popup menus in this widget's subtree.
///
/// Values specified here are used for popup menu properties that are not
/// given an explicit non-null value.
class PopupMenuTheme extends InheritedTheme {
/// Creates a popup menu theme that controls the configurations for
/// popup menus in its widget subtree.
const PopupMenuTheme({
super.key,
required this.data,
required super.child,
});
/// The properties for descendant popup menu widgets.
final PopupMenuThemeData data;
/// The closest instance of this class's [data] value that encloses the given
/// context. If there is no ancestor, it returns [ThemeData.popupMenuTheme].
/// Applications can assume that the returned value will not be null.
///
/// Typical usage is as follows:
///
/// ```dart
/// PopupMenuThemeData theme = PopupMenuTheme.of(context);
/// ```
static PopupMenuThemeData of(BuildContext context) {
final PopupMenuTheme? popupMenuTheme = context.dependOnInheritedWidgetOfExactType<PopupMenuTheme>();
return popupMenuTheme?.data ?? Theme.of(context).popupMenuTheme;
}
@override
Widget wrap(BuildContext context, Widget child) {
return PopupMenuTheme(data: data, child: child);
}
@override
bool updateShouldNotify(PopupMenuTheme oldWidget) => data != oldWidget.data;
}