Skip to content

Commit 2e3af4d

Browse files
authored
Merge pull request #355 from AhmedLSayed9/enhance/improve_dropdown_formfield_overlays
Improve DropdownButtonFormField overlays and surroundings
2 parents 6b078ac + b316a8b commit 2e3af4d

File tree

5 files changed

+368
-375
lines changed

5 files changed

+368
-375
lines changed
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
part of 'dropdown_button2.dart';
2+
3+
class _ButtonStyleDataBase {
4+
const _ButtonStyleDataBase({
5+
required this.height,
6+
required this.width,
7+
required this.padding,
8+
required this.decoration,
9+
required this.foregroundDecoration,
10+
required this.elevation,
11+
});
12+
13+
/// The height of the button
14+
final double? height;
15+
16+
/// The width of the button
17+
final double? width;
18+
19+
/// The inner padding of the Button
20+
final EdgeInsetsGeometry? padding;
21+
22+
/// The decoration of the Button
23+
final BoxDecoration? decoration;
24+
25+
/// The decoration to paint in front of the Button
26+
final BoxDecoration? foregroundDecoration;
27+
28+
/// The elevation of the Button
29+
final int? elevation;
30+
}
31+
32+
/// A class to configure the theme of the button.
33+
class ButtonStyleData extends _ButtonStyleDataBase {
34+
/// Creates a ButtonStyleData.
35+
/// It's a class to configure the theme of the button.
36+
const ButtonStyleData({
37+
super.height,
38+
super.width,
39+
super.padding,
40+
super.decoration,
41+
super.foregroundDecoration,
42+
super.elevation,
43+
this.overlayColor,
44+
});
45+
46+
/// Defines the ink response focus, hover, and splash colors.
47+
///
48+
/// This default null property can be used as an alternative to
49+
/// [focusColor], [hoverColor], [highlightColor], and
50+
/// [splashColor]. If non-null, it is resolved against one of
51+
/// [MaterialState.focused], [MaterialState.hovered], and
52+
/// [MaterialState.pressed]. It's convenient to use when the parent
53+
/// widget can pass along its own MaterialStateProperty value for
54+
/// the overlay color.
55+
///
56+
/// [MaterialState.pressed] triggers a ripple (an ink splash), per
57+
/// the current Material Design spec. The [overlayColor] doesn't map
58+
/// a state to [highlightColor] because a separate highlight is not
59+
/// used by the current design guidelines. See
60+
/// https://material.io/design/interaction/states.html#pressed
61+
///
62+
/// If the overlay color is null or resolves to null, then [focusColor],
63+
/// [hoverColor], [splashColor] and their defaults are used instead.
64+
///
65+
/// See also:
66+
///
67+
/// * The Material Design specification for overlay colors and how they
68+
/// match a component's state:
69+
/// <https://material.io/design/interaction/states.html#anatomy>.
70+
final MaterialStateProperty<Color?>? overlayColor;
71+
72+
/// Create a clone of the current [ButtonStyleData] but with the provided
73+
/// parameters overridden.
74+
ButtonStyleData copyWith({
75+
double? height,
76+
double? width,
77+
EdgeInsetsGeometry? padding,
78+
BoxDecoration? decoration,
79+
BoxDecoration? foregroundDecoration,
80+
int? elevation,
81+
MaterialStateProperty<Color?>? overlayColor,
82+
}) {
83+
return ButtonStyleData(
84+
height: height ?? this.height,
85+
width: width ?? this.width,
86+
padding: padding ?? this.padding,
87+
decoration: decoration ?? this.decoration,
88+
foregroundDecoration: foregroundDecoration ?? this.foregroundDecoration,
89+
elevation: elevation ?? this.elevation,
90+
overlayColor: overlayColor ?? this.overlayColor,
91+
);
92+
}
93+
}
94+
95+
/// A class to configure the theme of the button when using DropdownButtonFormField2.
96+
///
97+
/// Note: To configure the button's overlay colors when using DropdownButtonFormField2,
98+
/// use [InputDecoration] with `filled: true`. This works similarly to TextField.
99+
///
100+
/// i.e:
101+
/// ```dart
102+
/// decoration: InputDecoration(
103+
/// filled: true,
104+
/// fillColor: Colors.green,
105+
/// hoverColor: Colors.red,
106+
/// focusColor: Colors.blue,
107+
/// ),
108+
/// // Or
109+
/// decoration: InputDecoration(
110+
/// filled: true,
111+
/// fillColor: WidgetStateColor.fromMap({
112+
/// WidgetState.hovered: Colors.red,
113+
/// WidgetState.focused: Colors.blue,
114+
/// WidgetState.any: Colors.green,
115+
/// }),
116+
/// ),
117+
/// ```
118+
class FormFieldButtonStyleData extends _ButtonStyleDataBase {
119+
/// Creates a FormFieldButtonStyleData.
120+
/// It's a class to configure the theme of the button when using DropdownButtonFormField2.
121+
const FormFieldButtonStyleData({
122+
super.height,
123+
super.width,
124+
super.padding,
125+
super.decoration,
126+
super.foregroundDecoration,
127+
super.elevation,
128+
});
129+
130+
/// Create a clone of the current [FormFieldButtonStyleData] but with the provided
131+
/// parameters overridden.
132+
FormFieldButtonStyleData copyWith({
133+
double? height,
134+
double? width,
135+
EdgeInsetsGeometry? padding,
136+
BoxDecoration? decoration,
137+
BoxDecoration? foregroundDecoration,
138+
int? elevation,
139+
}) {
140+
return FormFieldButtonStyleData(
141+
height: height ?? this.height,
142+
width: width ?? this.width,
143+
padding: padding ?? this.padding,
144+
decoration: decoration ?? this.decoration,
145+
foregroundDecoration: foregroundDecoration ?? this.foregroundDecoration,
146+
elevation: elevation ?? this.elevation,
147+
);
148+
}
149+
150+
ButtonStyleData get _toButtonStyleData {
151+
return ButtonStyleData(
152+
height: height ?? height,
153+
width: width ?? width,
154+
padding: padding ?? padding,
155+
decoration: decoration ?? decoration,
156+
foregroundDecoration: foregroundDecoration ?? foregroundDecoration,
157+
elevation: elevation ?? elevation,
158+
);
159+
}
160+
}
161+
162+
/// A class to configure the theme of the button's icon.
163+
class IconStyleData {
164+
/// Creates an IconStyleData.
165+
/// It's a class to configure the theme of the button's icon.
166+
const IconStyleData({
167+
this.icon = const Icon(Icons.arrow_drop_down),
168+
this.iconDisabledColor,
169+
this.iconEnabledColor,
170+
this.iconSize = 24,
171+
this.openMenuIcon,
172+
});
173+
174+
/// The widget to use for the drop-down button's suffix icon.
175+
///
176+
/// Defaults to an [Icon] with the [Icons.arrow_drop_down] glyph.
177+
final Widget icon;
178+
179+
/// The color of any [Icon] descendant of [icon] if this button is disabled,
180+
/// i.e. if [onChanged] is null.
181+
///
182+
/// Defaults to [MaterialColor.shade400] of [Colors.grey] when the theme's
183+
/// [ThemeData.brightness] is [Brightness.light] and to
184+
/// [Colors.white10] when it is [Brightness.dark]
185+
final Color? iconDisabledColor;
186+
187+
/// The color of any [Icon] descendant of [icon] if this button is enabled,
188+
/// i.e. if [onChanged] is defined.
189+
///
190+
/// Defaults to [MaterialColor.shade700] of [Colors.grey] when the theme's
191+
/// [ThemeData.brightness] is [Brightness.light] and to
192+
/// [Colors.white70] when it is [Brightness.dark]
193+
final Color? iconEnabledColor;
194+
195+
/// The size to use for the drop-down button's icon.
196+
///
197+
/// Defaults to 24.0.
198+
final double iconSize;
199+
200+
/// Shows different icon when dropdown menu is open
201+
final Widget? openMenuIcon;
202+
203+
/// Create a clone of the current [IconStyleData] but with the provided
204+
/// parameters overridden.
205+
IconStyleData copyWith({
206+
Widget? icon,
207+
Color? iconDisabledColor,
208+
Color? iconEnabledColor,
209+
double? iconSize,
210+
Widget? openMenuIcon,
211+
}) {
212+
return IconStyleData(
213+
icon: icon ?? this.icon,
214+
iconDisabledColor: iconDisabledColor ?? this.iconDisabledColor,
215+
iconEnabledColor: iconEnabledColor ?? this.iconEnabledColor,
216+
iconSize: iconSize ?? this.iconSize,
217+
openMenuIcon: openMenuIcon ?? this.openMenuIcon,
218+
);
219+
}
220+
}

0 commit comments

Comments
 (0)