Skip to content

Commit 7b75ff7

Browse files
authored
Merge pull request #360 from AhmedLSayed9/migrate_to_formatter_page_width_configuration
Migrate to formatter page width configuration
2 parents 46c7d53 + fe1915e commit 7b75ff7

File tree

7 files changed

+107
-193
lines changed

7 files changed

+107
-193
lines changed

analysis_options.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ analyzer:
1919
- "**/*.g.dart"
2020
- "**/*.mocks.dart" # Mockito @GenerateMocks
2121

22+
formatter:
23+
page_width: 100
24+
2225
linter:
2326
rules:
2427
# This list is derived from the list of all available lints located at

packages/dropdown_button2/lib/src/dropdown_button2.dart

Lines changed: 50 additions & 93 deletions
Large diffs are not rendered by default.

packages/dropdown_button2/lib/src/dropdown_menu.dart

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
part of 'dropdown_button2.dart';
22

3-
SearchMatchFn<T> _defaultSearchMatchFn<T>() =>
4-
(DropdownItem<T> item, String searchValue) =>
5-
item.value.toString().toLowerCase().contains(searchValue.toLowerCase());
3+
SearchMatchFn<T> _defaultSearchMatchFn<T>() => (DropdownItem<T> item, String searchValue) =>
4+
item.value.toString().toLowerCase().contains(searchValue.toLowerCase());
65

76
class _MenuLimits {
87
const _MenuLimits(this.top, this.bottom, this.height, this.scrollOffset);
@@ -49,8 +48,7 @@ class _DropdownMenuState<T> extends State<_DropdownMenu<T>> {
4948

5049
DropdownSearchData<T>? get searchData => widget.route.searchData;
5150

52-
_DropdownItemButton<T> dropdownItemButton(int index) =>
53-
_DropdownItemButton<T>(
51+
_DropdownItemButton<T> dropdownItemButton(int index) => _DropdownItemButton<T>(
5452
route: widget.route,
5553
scrollController: widget.scrollController,
5654
textDirection: widget.textDirection,
@@ -83,8 +81,7 @@ class _DropdownMenuState<T> extends State<_DropdownMenu<T>> {
8381
final searchController = searchData?.searchController;
8482
if (searchController == null) {
8583
_children = <Widget>[
86-
for (int index = 0; index < items.length; ++index)
87-
dropdownItemButton(index),
84+
for (int index = 0; index < items.length; ++index) dropdownItemButton(index),
8885
];
8986
} else {
9087
_searchMatchFn = searchData?.searchMatchFn ?? _defaultSearchMatchFn();
@@ -103,8 +100,7 @@ class _DropdownMenuState<T> extends State<_DropdownMenu<T>> {
103100
final String currentSearch = searchData!.searchController!.text;
104101
return <Widget>[
105102
for (int index = 0; index < items.length; ++index)
106-
if (_searchMatchFn(items[index], currentSearch))
107-
dropdownItemButton(index),
103+
if (_searchMatchFn(items[index], currentSearch)) dropdownItemButton(index),
108104
];
109105
}
110106

@@ -125,13 +121,11 @@ class _DropdownMenuState<T> extends State<_DropdownMenu<T>> {
125121

126122
ScrollbarThemeData? get _scrollbarTheme => dropdownStyle.scrollbarTheme;
127123

128-
bool get _iOSThumbVisibility =>
129-
_scrollbarTheme?.thumbVisibility?.resolve(_states) ?? true;
124+
bool get _iOSThumbVisibility => _scrollbarTheme?.thumbVisibility?.resolve(_states) ?? true;
130125

131126
bool get _hasIntrinsicHeight =>
132127
widget.route.items.any((item) => item.intrinsicHeight) ||
133-
(widget.route.dropdownSeparator != null &&
134-
widget.route.dropdownSeparator!.intrinsicHeight);
128+
(widget.route.dropdownSeparator != null && widget.route.dropdownSeparator!.intrinsicHeight);
135129

136130
@override
137131
Widget build(BuildContext context) {
@@ -144,8 +138,7 @@ class _DropdownMenuState<T> extends State<_DropdownMenu<T>> {
144138
// When the menu is dismissed we just fade the entire thing out
145139
// in the first 0.25s.
146140
assert(debugCheckHasMaterialLocalizations(context));
147-
final MaterialLocalizations localizations =
148-
MaterialLocalizations.of(context);
141+
final MaterialLocalizations localizations = MaterialLocalizations.of(context);
149142
final _DropdownRoute<T> route = widget.route;
150143

151144
final separator = widget.route.dropdownSeparator;
@@ -189,24 +182,20 @@ class _DropdownMenuState<T> extends State<_DropdownMenu<T>> {
189182
thumbVisibility:
190183
// ignore: avoid_bool_literals_in_conditional_expressions
191184
_isIOS ? _iOSThumbVisibility : true,
192-
thickness: _isIOS
193-
? _scrollbarTheme?.thickness?.resolve(_states)
194-
: null,
185+
thickness: _isIOS ? _scrollbarTheme?.thickness?.resolve(_states) : null,
195186
radius: _isIOS ? _scrollbarTheme?.radius : null,
196187
child: ListView.custom(
197188
// Ensure this always inherits the PrimaryScrollController
198189
primary: true,
199190
shrinkWrap: true,
200-
padding:
201-
dropdownStyle.padding ?? kMaterialListPadding,
191+
padding: dropdownStyle.padding ?? kMaterialListPadding,
202192
itemExtentBuilder: _hasIntrinsicHeight
203193
? null
204194
: (index, dimensions) {
205195
final childrenLength = separator == null
206196
? _children.length
207197
: SeparatedSliverChildBuilderDelegate
208-
.computeActualChildCount(
209-
_children.length);
198+
.computeActualChildCount(_children.length);
210199
// TODO(Ahmed): Remove this when https://github.com/flutter/flutter/pull/142428
211200
// is supported by the min version of the package [Flutter>=3.22.0].
212201
if (index >= childrenLength) {
@@ -225,13 +214,9 @@ class _DropdownMenuState<T> extends State<_DropdownMenu<T>> {
225214
)
226215
: SeparatedSliverChildBuilderDelegate(
227216
itemCount: _children.length,
228-
itemBuilder: (context, index) =>
229-
_children[index],
230-
separatorBuilder: (context, index) =>
231-
SizedBox(
232-
height: separator.intrinsicHeight
233-
? null
234-
: separator.height,
217+
itemBuilder: (context, index) => _children[index],
218+
separatorBuilder: (context, index) => SizedBox(
219+
height: separator.intrinsicHeight ? null : separator.height,
235220
child: separator,
236221
),
237222
),
@@ -265,13 +250,10 @@ class _DropdownMenuState<T> extends State<_DropdownMenu<T>> {
265250
label: localizations.popupMenuLabel,
266251
child: ClipRRect(
267252
//Prevent scrollbar, ripple effect & items from going beyond border boundaries when scrolling.
268-
clipBehavior: dropdownStyle.decoration?.borderRadius != null
269-
? Clip.antiAlias
270-
: Clip.none,
271-
borderRadius:
272-
dropdownStyle.decoration?.borderRadius ?? BorderRadius.zero,
273-
child: dropdownStyle.dropdownBuilder?.call(context, dropdownMenu) ??
274-
dropdownMenu,
253+
clipBehavior:
254+
dropdownStyle.decoration?.borderRadius != null ? Clip.antiAlias : Clip.none,
255+
borderRadius: dropdownStyle.decoration?.borderRadius ?? BorderRadius.zero,
256+
child: dropdownStyle.dropdownBuilder?.call(context, dropdownMenu) ?? dropdownMenu,
275257
),
276258
),
277259
),
@@ -290,8 +272,7 @@ class _DropdownMenuPainter extends CustomPainter {
290272
}) : _painter = dropdownDecoration
291273
?.copyWith(
292274
color: dropdownDecoration.color ?? color,
293-
boxShadow: dropdownDecoration.boxShadow ??
294-
kElevationToShadow[elevation],
275+
boxShadow: dropdownDecoration.boxShadow ?? kElevationToShadow[elevation],
295276
)
296277
.createBoxPainter(() {}) ??
297278
BoxDecoration(
@@ -323,13 +304,11 @@ class _DropdownMenuPainter extends CustomPainter {
323304
);
324305

325306
final Tween<double> bottom = Tween<double>(
326-
begin: clampDouble(top.begin! + itemHeight,
327-
math.min(itemHeight, size.height), size.height),
307+
begin: clampDouble(top.begin! + itemHeight, math.min(itemHeight, size.height), size.height),
328308
end: size.height,
329309
);
330310

331-
final Rect rect = Rect.fromLTRB(
332-
0.0, top.evaluate(resize), size.width, bottom.evaluate(resize));
311+
final Rect rect = Rect.fromLTRB(0.0, top.evaluate(resize), size.width, bottom.evaluate(resize));
333312

334313
_painter.paint(canvas, rect.topLeft, ImageConfiguration(size: rect.size));
335314
}

packages/dropdown_button2/lib/src/dropdown_menu_item.dart

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,7 @@ class _DropdownItemButtonState<T> extends State<_DropdownItemButton<T>> {
180180
void _setOpacityAnimation() {
181181
final double menuCurveEnd = widget.route.dropdownStyle.openInterval.end;
182182
final double unit = 0.5 / (widget.route.items.length + 1.5);
183-
final double start =
184-
clampDouble(menuCurveEnd + (widget.itemIndex + 1) * unit, 0.0, 1.0);
183+
final double start = clampDouble(menuCurveEnd + (widget.itemIndex + 1) * unit, 0.0, 1.0);
185184
final double end = clampDouble(start + 1.5 * unit, 0.0, 1.0);
186185
_opacityAnimation = CurvedAnimation(
187186
parent: widget.route.animation!,
@@ -190,8 +189,7 @@ class _DropdownItemButtonState<T> extends State<_DropdownItemButton<T>> {
190189
}
191190

192191
void _handleFocusChange(bool focused) {
193-
final bool inTraditionalMode =
194-
switch (FocusManager.instance.highlightMode) {
192+
final bool inTraditionalMode = switch (FocusManager.instance.highlightMode) {
195193
FocusHighlightMode.touch => false,
196194
FocusHighlightMode.traditional => true,
197195
};
@@ -225,29 +223,23 @@ class _DropdownItemButtonState<T> extends State<_DropdownItemButton<T>> {
225223
}
226224
}
227225

228-
static const Map<ShortcutActivator, Intent> _webShortcuts =
229-
<ShortcutActivator, Intent>{
226+
static const Map<ShortcutActivator, Intent> _webShortcuts = <ShortcutActivator, Intent>{
230227
// On the web, up/down don't change focus, *except* in a <select>
231228
// element, which is what a dropdown emulates.
232-
SingleActivator(LogicalKeyboardKey.arrowDown):
233-
DirectionalFocusIntent(TraversalDirection.down),
234-
SingleActivator(LogicalKeyboardKey.arrowUp):
235-
DirectionalFocusIntent(TraversalDirection.up),
229+
SingleActivator(LogicalKeyboardKey.arrowDown): DirectionalFocusIntent(TraversalDirection.down),
230+
SingleActivator(LogicalKeyboardKey.arrowUp): DirectionalFocusIntent(TraversalDirection.up),
236231
};
237232

238233
MenuItemStyleData get _menuItemStyle => widget.route.menuItemStyle;
239-
EdgeInsets? get _inputDecorationPadding =>
240-
widget.route.inputDecorationPadding;
241-
bool get _useDecorationHPadding =>
242-
_menuItemStyle.useDecorationHorizontalPadding;
234+
EdgeInsets? get _inputDecorationPadding => widget.route.inputDecorationPadding;
235+
bool get _useDecorationHPadding => _menuItemStyle.useDecorationHorizontalPadding;
243236

244237
@override
245238
Widget build(BuildContext context) {
246239
final DropdownItem<T> dropdownItem = widget.route.items[widget.itemIndex];
247240

248241
final menuItemPadding =
249-
_menuItemStyle.padding?.resolve(widget.textDirection) ??
250-
_kMenuItemPadding;
242+
_menuItemStyle.padding?.resolve(widget.textDirection) ?? _kMenuItemPadding;
251243

252244
Widget child = Padding(
253245
padding: menuItemPadding.copyWith(
@@ -259,8 +251,8 @@ class _DropdownItemButtonState<T> extends State<_DropdownItemButton<T>> {
259251
// An [InkWell] is added to the item only if it is enabled
260252
// isNoSelectedItem to avoid first item highlight when no item selected
261253
if (dropdownItem.enabled) {
262-
final bool isSelectedItem = !widget.route.isNoSelectedItem &&
263-
widget.itemIndex == widget.route.selectedIndex;
254+
final bool isSelectedItem =
255+
!widget.route.isNoSelectedItem && widget.itemIndex == widget.route.selectedIndex;
264256
child = InkWell(
265257
autofocus: isSelectedItem,
266258
enableFeedback: widget.enableFeedback,
@@ -269,8 +261,7 @@ class _DropdownItemButtonState<T> extends State<_DropdownItemButton<T>> {
269261
borderRadius: _menuItemStyle.borderRadius,
270262
overlayColor: _menuItemStyle.overlayColor,
271263
child: isSelectedItem
272-
? _menuItemStyle.selectedMenuItemBuilder?.call(context, child) ??
273-
child
264+
? _menuItemStyle.selectedMenuItemBuilder?.call(context, child) ?? child
274265
: child,
275266
);
276267
}

packages/dropdown_button2/lib/src/dropdown_route.dart

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ class _DropdownRoute<T> extends PopupRoute<_DropdownRouteResult<T>> {
6969
//This will ensure menu is drawn in the actual available height.
7070
final padding = MediaQuery.paddingOf(context);
7171
final viewInsets = MediaQuery.viewInsetsOf(context);
72-
final BoxConstraints actualConstraints = constraints.copyWith(
73-
maxHeight: constraints.maxHeight - viewInsets.bottom);
72+
final BoxConstraints actualConstraints =
73+
constraints.copyWith(maxHeight: constraints.maxHeight - viewInsets.bottom);
7474
final EdgeInsets mediaQueryPadding =
7575
dropdownStyle.useSafeArea ? padding : EdgeInsets.zero;
7676
return ValueListenableBuilder<Rect?>(
@@ -136,8 +136,7 @@ class _DropdownRoute<T> extends PopupRoute<_DropdownRouteResult<T>> {
136136

137137
if (items.isNotEmpty && index > 0) {
138138
if (searchData?.searchController?.text case final searchText?) {
139-
final searchMatchFn =
140-
searchData?.searchMatchFn ?? _defaultSearchMatchFn();
139+
final searchMatchFn = searchData?.searchMatchFn ?? _defaultSearchMatchFn();
141140
final selectedItemExist = searchMatchFn(items[index], searchText);
142141
if (selectedItemExist) {
143142
offset += _getSearchItemsHeight(index, searchText);
@@ -166,16 +165,14 @@ class _DropdownRoute<T> extends PopupRoute<_DropdownRouteResult<T>> {
166165
EdgeInsets mediaQueryPadding,
167166
int index,
168167
) {
169-
double maxHeight =
170-
getMenuAvailableHeight(availableHeight, mediaQueryPadding);
168+
double maxHeight = getMenuAvailableHeight(availableHeight, mediaQueryPadding);
171169
// If a preferred MaxHeight is set by the user, use it instead of the available maxHeight.
172170
final double? preferredMaxHeight = dropdownStyle.maxHeight;
173171
if (preferredMaxHeight != null) {
174172
maxHeight = math.min(maxHeight, preferredMaxHeight);
175173
}
176174

177-
double actualMenuHeight =
178-
dropdownStyle.padding?.vertical ?? kMaterialListPadding.vertical;
175+
double actualMenuHeight = dropdownStyle.padding?.vertical ?? kMaterialListPadding.vertical;
179176
final double innerWidgetHeight = searchData?.searchBarWidgetHeight ?? 0.0;
180177
actualMenuHeight += innerWidgetHeight;
181178
if (items.isNotEmpty) {
@@ -296,8 +293,7 @@ class _DropdownRoutePageState<T> extends State<_DropdownRoutePage<T>> {
296293
widget.mediaQueryPadding,
297294
widget.selectedIndex,
298295
);
299-
_scrollController =
300-
ScrollController(initialScrollOffset: menuLimits.scrollOffset);
296+
_scrollController = ScrollController(initialScrollOffset: menuLimits.scrollOffset);
301297
}
302298

303299
@override
@@ -362,17 +358,15 @@ class _DropdownMenuRouteLayout<T> extends SingleChildLayoutDelegate {
362358

363359
@override
364360
BoxConstraints getConstraintsForChild(BoxConstraints constraints) {
365-
double maxHeight =
366-
route.getMenuAvailableHeight(availableHeight, mediaQueryPadding);
361+
double maxHeight = route.getMenuAvailableHeight(availableHeight, mediaQueryPadding);
367362
final double? preferredMaxHeight = route.dropdownStyle.maxHeight;
368363
if (preferredMaxHeight != null && preferredMaxHeight <= maxHeight) {
369364
maxHeight = preferredMaxHeight;
370365
}
371366
// The width of a menu should be at most the view width. This ensures that
372367
// the menu does not extend past the left and right edges of the screen.
373368
final double? menuWidth = route.dropdownStyle.width;
374-
final double width =
375-
math.min(constraints.maxWidth, menuWidth ?? buttonRect.width);
369+
final double width = math.min(constraints.maxWidth, menuWidth ?? buttonRect.width);
376370
return BoxConstraints(
377371
minWidth: width,
378372
maxWidth: width,
@@ -438,8 +432,7 @@ class _DropdownMenuRouteLayout<T> extends SingleChildLayoutDelegate {
438432

439433
@override
440434
bool shouldRelayout(_DropdownMenuRouteLayout<T> oldDelegate) {
441-
return buttonRect != oldDelegate.buttonRect ||
442-
textDirection != oldDelegate.textDirection;
435+
return buttonRect != oldDelegate.buttonRect || textDirection != oldDelegate.textDirection;
443436
}
444437
}
445438

@@ -534,8 +527,8 @@ class _DropdownBarrierPainter extends CustomPainter {
534527
@override
535528
void paint(Canvas canvas, Size size) {
536529
if (barrierColor != null && buttonRect != null) {
537-
final Rect rect = Rect.fromLTRB(
538-
-buttonRect!.left, -buttonRect!.top, pageSize.width, pageSize.height);
530+
final Rect rect =
531+
Rect.fromLTRB(-buttonRect!.left, -buttonRect!.top, pageSize.width, pageSize.height);
539532
canvas.saveLayer(rect, Paint());
540533
canvas.drawRect(rect, Paint()..color = barrierColor!);
541534
canvas.drawRect(buttonRect!, Paint()..blendMode = BlendMode.clear);
@@ -545,7 +538,6 @@ class _DropdownBarrierPainter extends CustomPainter {
545538

546539
@override
547540
bool shouldRepaint(_DropdownBarrierPainter oldPainter) {
548-
return oldPainter.buttonRect != buttonRect ||
549-
oldPainter.barrierColor != barrierColor;
541+
return oldPainter.buttonRect != buttonRect || oldPainter.barrierColor != barrierColor;
550542
}
551543
}

packages/dropdown_button2/lib/src/dropdown_style_data.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,7 @@ class MenuItemStyleData {
229229
padding: padding ?? this.padding,
230230
borderRadius: borderRadius ?? this.borderRadius,
231231
overlayColor: overlayColor ?? this.overlayColor,
232-
selectedMenuItemBuilder:
233-
selectedMenuItemBuilder ?? this.selectedMenuItemBuilder,
232+
selectedMenuItemBuilder: selectedMenuItemBuilder ?? this.selectedMenuItemBuilder,
234233
);
235234
}
236235
}
@@ -285,8 +284,7 @@ class DropdownSearchData<T> {
285284
return DropdownSearchData<T>(
286285
searchController: searchController ?? this.searchController,
287286
searchBarWidget: searchBarWidget ?? this.searchBarWidget,
288-
searchBarWidgetHeight:
289-
searchBarWidgetHeight ?? this.searchBarWidgetHeight,
287+
searchBarWidgetHeight: searchBarWidgetHeight ?? this.searchBarWidgetHeight,
290288
noResultsWidget: noResultsWidget ?? this.noResultsWidget,
291289
searchMatchFn: searchMatchFn ?? this.searchMatchFn,
292290
);

0 commit comments

Comments
 (0)