Skip to content

Commit 8dabea8

Browse files
authored
Merge pull request #315 from AhmedLSayed9/add_copy_with_method_for_style_data_classes
Add copyWith method for style data classes
2 parents b474e80 + 5c14cfe commit 8dabea8

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

packages/dropdown_button2/lib/src/dropdown_style_data.dart

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,28 @@ class ButtonStyleData {
5656
/// match a component's state:
5757
/// <https://material.io/design/interaction/states.html#anatomy>.
5858
final MaterialStateProperty<Color?>? overlayColor;
59+
60+
/// Create a clone of the current [ButtonStyleData] but with the provided
61+
/// parameters overridden.
62+
ButtonStyleData copyWith({
63+
double? height,
64+
double? width,
65+
EdgeInsetsGeometry? padding,
66+
BoxDecoration? decoration,
67+
BoxDecoration? foregroundDecoration,
68+
int? elevation,
69+
MaterialStateProperty<Color?>? overlayColor,
70+
}) {
71+
return ButtonStyleData(
72+
height: height ?? this.height,
73+
width: width ?? this.width,
74+
padding: padding ?? this.padding,
75+
decoration: decoration ?? this.decoration,
76+
foregroundDecoration: foregroundDecoration ?? this.foregroundDecoration,
77+
elevation: elevation ?? this.elevation,
78+
overlayColor: overlayColor ?? this.overlayColor,
79+
);
80+
}
5981
}
6082

6183
/// A class to configure the theme of the button's icon.
@@ -97,6 +119,24 @@ class IconStyleData {
97119

98120
/// Shows different icon when dropdown menu is open
99121
final Widget? openMenuIcon;
122+
123+
/// Create a clone of the current [IconStyleData] but with the provided
124+
/// parameters overridden.
125+
IconStyleData copyWith({
126+
Widget? icon,
127+
Color? iconDisabledColor,
128+
Color? iconEnabledColor,
129+
double? iconSize,
130+
Widget? openMenuIcon,
131+
}) {
132+
return IconStyleData(
133+
icon: icon ?? this.icon,
134+
iconDisabledColor: iconDisabledColor ?? this.iconDisabledColor,
135+
iconEnabledColor: iconEnabledColor ?? this.iconEnabledColor,
136+
iconSize: iconSize ?? this.iconSize,
137+
openMenuIcon: openMenuIcon ?? this.openMenuIcon,
138+
);
139+
}
100140
}
101141

102142
/// A class to configure the theme of the dropdown menu.
@@ -205,6 +245,44 @@ class DropdownStyleData {
205245
/// },
206246
/// ```
207247
final DropdownBuilder? dropdownBuilder;
248+
249+
/// Create a clone of the current [DropdownStyleData] but with the provided
250+
/// parameters overridden.
251+
DropdownStyleData copyWith({
252+
double? maxHeight,
253+
double? width,
254+
EdgeInsetsGeometry? padding,
255+
EdgeInsetsGeometry? scrollPadding,
256+
BoxDecoration? decoration,
257+
int? elevation,
258+
DropdownDirection? direction,
259+
Offset? offset,
260+
bool? isOverButton,
261+
bool? useSafeArea,
262+
bool? isFullScreen,
263+
bool? useRootNavigator,
264+
ScrollbarThemeData? scrollbarTheme,
265+
Interval? openInterval,
266+
DropdownBuilder? dropdownBuilder,
267+
}) {
268+
return DropdownStyleData(
269+
maxHeight: maxHeight ?? this.maxHeight,
270+
width: width ?? this.width,
271+
padding: padding ?? this.padding,
272+
scrollPadding: scrollPadding ?? this.scrollPadding,
273+
decoration: decoration ?? this.decoration,
274+
elevation: elevation ?? this.elevation,
275+
direction: direction ?? this.direction,
276+
offset: offset ?? this.offset,
277+
isOverButton: isOverButton ?? this.isOverButton,
278+
useSafeArea: useSafeArea ?? this.useSafeArea,
279+
isFullScreen: isFullScreen ?? this.isFullScreen,
280+
useRootNavigator: useRootNavigator ?? this.useRootNavigator,
281+
scrollbarTheme: scrollbarTheme ?? this.scrollbarTheme,
282+
openInterval: openInterval ?? this.openInterval,
283+
dropdownBuilder: dropdownBuilder ?? this.dropdownBuilder,
284+
);
285+
}
208286
}
209287

210288
/// A class to configure the theme of the dropdown menu items.
@@ -267,6 +345,23 @@ class MenuItemStyleData {
267345
/// },
268346
/// ```
269347
final SelectedMenuItemBuilder? selectedMenuItemBuilder;
348+
349+
/// Create a clone of the current [MenuItemStyleData] but with the provided
350+
/// parameters overridden.
351+
MenuItemStyleData copyWith({
352+
EdgeInsetsGeometry? padding,
353+
BorderRadius? borderRadius,
354+
MaterialStateProperty<Color?>? overlayColor,
355+
SelectedMenuItemBuilder? selectedMenuItemBuilder,
356+
}) {
357+
return MenuItemStyleData(
358+
padding: padding ?? this.padding,
359+
borderRadius: borderRadius ?? this.borderRadius,
360+
overlayColor: overlayColor ?? this.overlayColor,
361+
selectedMenuItemBuilder:
362+
selectedMenuItemBuilder ?? this.selectedMenuItemBuilder,
363+
);
364+
}
270365
}
271366

272367
/// A class to configure searchable dropdowns.
@@ -306,4 +401,23 @@ class DropdownSearchData<T> {
306401
/// item.value.toString().toLowerCase().contains(searchValue.toLowerCase());
307402
/// ```
308403
final SearchMatchFn<T>? searchMatchFn;
404+
405+
/// Create a clone of the current [DropdownSearchData] but with the provided
406+
/// parameters overridden.
407+
DropdownSearchData<T> copyWith({
408+
TextEditingController? searchController,
409+
Widget? searchBarWidget,
410+
double? searchBarWidgetHeight,
411+
Widget? noResultsWidget,
412+
SearchMatchFn<T>? searchMatchFn,
413+
}) {
414+
return DropdownSearchData<T>(
415+
searchController: searchController ?? this.searchController,
416+
searchBarWidget: searchBarWidget ?? this.searchBarWidget,
417+
searchBarWidgetHeight:
418+
searchBarWidgetHeight ?? this.searchBarWidgetHeight,
419+
noResultsWidget: noResultsWidget ?? this.noResultsWidget,
420+
searchMatchFn: searchMatchFn ?? this.searchMatchFn,
421+
);
422+
}
309423
}

0 commit comments

Comments
 (0)