Skip to content

Commit 26e379e

Browse files
Refactoring if chains into switch statements (flutter#144905)
Based on issue flutter#144903, this PR aims to bring the codebase more in line with the [Flutter repo style guide](https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#avoid-using-if-chains-or--or--with-enum-values): > #### Avoid using `if` chains or `?:` or `==` with enum values <br> This change unfortunately increases the total line length, but it also improves readability.
1 parent 187ec75 commit 26e379e

File tree

20 files changed

+404
-349
lines changed

20 files changed

+404
-349
lines changed

dev/integration_tests/flutter_gallery/lib/demo/material/bottom_navigation_demo.dart

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,15 @@ class NavigationIconView {
4040

4141
FadeTransition transition(BottomNavigationBarType type, BuildContext context) {
4242
Color? iconColor;
43-
if (type == BottomNavigationBarType.shifting) {
44-
iconColor = _color;
45-
} else {
46-
final ThemeData theme = Theme.of(context);
47-
final ColorScheme colorScheme = theme.colorScheme;
48-
iconColor = theme.brightness == Brightness.light
49-
? colorScheme.primary
50-
: colorScheme.secondary;
43+
switch (type) {
44+
case BottomNavigationBarType.shifting:
45+
iconColor = _color;
46+
case BottomNavigationBarType.fixed:
47+
final ThemeData theme = Theme.of(context);
48+
iconColor = switch (theme.brightness) {
49+
Brightness.light => theme.colorScheme.primary,
50+
Brightness.dark => theme.colorScheme.secondary,
51+
};
5152
}
5253

5354
return FadeTransition(

dev/manual_tests/lib/overlay_geometry.dart

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,16 @@ class _MarkerPainter extends CustomPainter {
3737
..color = const Color(0xFFFFFFFF)
3838
..style = PaintingStyle.stroke
3939
..strokeWidth = 1.0;
40-
if (type == MarkerType.topLeft) {
41-
canvas.drawLine(Offset(r, r), Offset(r + r - 1.0, r), paint);
42-
canvas.drawLine(Offset(r, r), Offset(r, r + r - 1.0), paint);
43-
}
44-
if (type == MarkerType.bottomRight) {
45-
canvas.drawLine(Offset(r, r), Offset(1.0, r), paint);
46-
canvas.drawLine(Offset(r, r), Offset(r, 1.0), paint);
40+
41+
switch (type) {
42+
case MarkerType.topLeft:
43+
canvas.drawLine(Offset(r, r), Offset(r + r - 1.0, r), paint);
44+
canvas.drawLine(Offset(r, r), Offset(r, r + r - 1.0), paint);
45+
case MarkerType.bottomRight:
46+
canvas.drawLine(Offset(r, r), Offset(1.0, r), paint);
47+
canvas.drawLine(Offset(r, r), Offset(r, 1.0), paint);
48+
case MarkerType.touch:
49+
break;
4750
}
4851
}
4952

examples/api/lib/material/bottom_app_bar/bottom_app_bar.2.dart

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,13 @@ class _BottomAppBarDemoState extends State<BottomAppBarDemo> {
4141
_isVisible ? FloatingActionButtonLocation.endContained : FloatingActionButtonLocation.endFloat;
4242

4343
void _listen() {
44-
final ScrollDirection direction = _controller.position.userScrollDirection;
45-
if (direction == ScrollDirection.forward) {
46-
_show();
47-
} else if (direction == ScrollDirection.reverse) {
48-
_hide();
44+
switch (_controller.position.userScrollDirection) {
45+
case ScrollDirection.idle:
46+
break;
47+
case ScrollDirection.forward:
48+
_show();
49+
case ScrollDirection.reverse:
50+
_hide();
4951
}
5052
}
5153

packages/flutter/lib/src/cupertino/date_picker.dart

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,25 +1021,29 @@ class _CupertinoDatePickerDateTimeState extends State<CupertinoDatePicker> {
10211021

10221022
// Adds am/pm column if the picker is not using 24h format.
10231023
if (!widget.use24hFormat) {
1024-
if (localizations.datePickerDateTimeOrder == DatePickerDateTimeOrder.date_time_dayPeriod
1025-
|| localizations.datePickerDateTimeOrder == DatePickerDateTimeOrder.time_dayPeriod_date) {
1026-
pickerBuilders.add(_buildAmPmPicker);
1027-
columnWidths.add(_getEstimatedColumnWidth(_PickerColumnType.dayPeriod));
1028-
} else {
1029-
pickerBuilders.insert(0, _buildAmPmPicker);
1030-
columnWidths.insert(0, _getEstimatedColumnWidth(_PickerColumnType.dayPeriod));
1024+
switch (localizations.datePickerDateTimeOrder) {
1025+
case DatePickerDateTimeOrder.date_time_dayPeriod:
1026+
case DatePickerDateTimeOrder.time_dayPeriod_date:
1027+
pickerBuilders.add(_buildAmPmPicker);
1028+
columnWidths.add(_getEstimatedColumnWidth(_PickerColumnType.dayPeriod));
1029+
case DatePickerDateTimeOrder.date_dayPeriod_time:
1030+
case DatePickerDateTimeOrder.dayPeriod_time_date:
1031+
pickerBuilders.insert(0, _buildAmPmPicker);
1032+
columnWidths.insert(0, _getEstimatedColumnWidth(_PickerColumnType.dayPeriod));
10311033
}
10321034
}
10331035

10341036
// Adds medium date column if the picker's mode is date and time.
10351037
if (widget.mode == CupertinoDatePickerMode.dateAndTime) {
1036-
if (localizations.datePickerDateTimeOrder == DatePickerDateTimeOrder.time_dayPeriod_date
1037-
|| localizations.datePickerDateTimeOrder == DatePickerDateTimeOrder.dayPeriod_time_date) {
1038-
pickerBuilders.add(_buildMediumDatePicker);
1039-
columnWidths.add(_getEstimatedColumnWidth(_PickerColumnType.date));
1040-
} else {
1041-
pickerBuilders.insert(0, _buildMediumDatePicker);
1042-
columnWidths.insert(0, _getEstimatedColumnWidth(_PickerColumnType.date));
1038+
switch (localizations.datePickerDateTimeOrder) {
1039+
case DatePickerDateTimeOrder.time_dayPeriod_date:
1040+
case DatePickerDateTimeOrder.dayPeriod_time_date:
1041+
pickerBuilders.add(_buildMediumDatePicker);
1042+
columnWidths.add(_getEstimatedColumnWidth(_PickerColumnType.date));
1043+
case DatePickerDateTimeOrder.date_time_dayPeriod:
1044+
case DatePickerDateTimeOrder.date_dayPeriod_time:
1045+
pickerBuilders.insert(0, _buildMediumDatePicker);
1046+
columnWidths.insert(0, _getEstimatedColumnWidth(_PickerColumnType.date));
10431047
}
10441048
}
10451049

packages/flutter/lib/src/cupertino/form_section.dart

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -210,24 +210,29 @@ class CupertinoFormSection extends StatelessWidget {
210210
),
211211
child: footer!);
212212

213-
return _type == CupertinoListSectionType.base
214-
? CupertinoListSection(
215-
header: headerWidget,
216-
footer: footerWidget,
217-
margin: margin,
218-
backgroundColor: backgroundColor,
219-
decoration: decoration,
220-
clipBehavior: clipBehavior,
221-
hasLeading: false,
222-
children: children)
223-
: CupertinoListSection.insetGrouped(
224-
header: headerWidget,
225-
footer: footerWidget,
226-
margin: margin,
227-
backgroundColor: backgroundColor,
228-
decoration: decoration,
229-
clipBehavior: clipBehavior,
230-
hasLeading: false,
231-
children: children);
213+
switch (_type) {
214+
case CupertinoListSectionType.base:
215+
return CupertinoListSection(
216+
header: headerWidget,
217+
footer: footerWidget,
218+
margin: margin,
219+
backgroundColor: backgroundColor,
220+
decoration: decoration,
221+
clipBehavior: clipBehavior,
222+
hasLeading: false,
223+
children: children,
224+
);
225+
case CupertinoListSectionType.insetGrouped:
226+
return CupertinoListSection.insetGrouped(
227+
header: headerWidget,
228+
footer: footerWidget,
229+
margin: margin,
230+
backgroundColor: backgroundColor,
231+
decoration: decoration,
232+
clipBehavior: clipBehavior,
233+
hasLeading: false,
234+
children: children,
235+
);
236+
}
232237
}
233238
}

packages/flutter/lib/src/gestures/arena.dart

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -222,22 +222,23 @@ class GestureArenaManager {
222222
if (state == null) {
223223
return; // This arena has already resolved.
224224
}
225-
assert(_debugLogDiagnostic(pointer, '${ disposition == GestureDisposition.accepted ? "Accepting" : "Rejecting" }: $member'));
226225
assert(state.members.contains(member));
227-
if (disposition == GestureDisposition.rejected) {
228-
state.members.remove(member);
229-
member.rejectGesture(pointer);
230-
if (!state.isOpen) {
231-
_tryToResolveArena(pointer, state);
232-
}
233-
} else {
234-
assert(disposition == GestureDisposition.accepted);
235-
if (state.isOpen) {
236-
state.eagerWinner ??= member;
237-
} else {
238-
assert(_debugLogDiagnostic(pointer, 'Self-declared winner: $member'));
239-
_resolveInFavorOf(pointer, state, member);
240-
}
226+
switch (disposition) {
227+
case GestureDisposition.accepted:
228+
assert(_debugLogDiagnostic(pointer, 'Accepting: $member'));
229+
if (state.isOpen) {
230+
state.eagerWinner ??= member;
231+
} else {
232+
assert(_debugLogDiagnostic(pointer, 'Self-declared winner: $member'));
233+
_resolveInFavorOf(pointer, state, member);
234+
}
235+
case GestureDisposition.rejected:
236+
assert(_debugLogDiagnostic(pointer, 'Rejecting: $member'));
237+
state.members.remove(member);
238+
member.rejectGesture(pointer);
239+
if (!state.isOpen) {
240+
_tryToResolveArena(pointer, state);
241+
}
241242
}
242243
}
243244

packages/flutter/lib/src/gestures/monodrag.dart

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -349,17 +349,20 @@ abstract class DragGestureRecognizer extends OneSequenceGestureRecognizer {
349349

350350
void _addPointer(PointerEvent event) {
351351
_velocityTrackers[event.pointer] = velocityTrackerBuilder(event);
352-
if (_state == _DragState.ready) {
353-
_state = _DragState.possible;
354-
_initialPosition = OffsetPair(global: event.position, local: event.localPosition);
355-
_finalPosition = _initialPosition;
356-
_pendingDragOffset = OffsetPair.zero;
357-
_globalDistanceMoved = 0.0;
358-
_lastPendingEventTimestamp = event.timeStamp;
359-
_lastTransform = event.transform;
360-
_checkDown();
361-
} else if (_state == _DragState.accepted) {
362-
resolve(GestureDisposition.accepted);
352+
switch (_state) {
353+
case _DragState.ready:
354+
_state = _DragState.possible;
355+
_initialPosition = OffsetPair(global: event.position, local: event.localPosition);
356+
_finalPosition = _initialPosition;
357+
_pendingDragOffset = OffsetPair.zero;
358+
_globalDistanceMoved = 0.0;
359+
_lastPendingEventTimestamp = event.timeStamp;
360+
_lastTransform = event.transform;
361+
_checkDown();
362+
case _DragState.possible:
363+
break;
364+
case _DragState.accepted:
365+
resolve(GestureDisposition.accepted);
363366
}
364367
}
365368

@@ -421,36 +424,37 @@ abstract class DragGestureRecognizer extends OneSequenceGestureRecognizer {
421424
final Offset position = (event is PointerMoveEvent) ? event.position : (event.position + (event as PointerPanZoomUpdateEvent).pan);
422425
final Offset localPosition = (event is PointerMoveEvent) ? event.localPosition : (event.localPosition + (event as PointerPanZoomUpdateEvent).localPan);
423426
_finalPosition = OffsetPair(local: localPosition, global: position);
424-
if (_state == _DragState.accepted) {
425-
_checkUpdate(
426-
sourceTimeStamp: event.timeStamp,
427-
delta: _getDeltaForDetails(localDelta),
428-
primaryDelta: _getPrimaryValueFromOffset(localDelta),
429-
globalPosition: position,
430-
localPosition: localPosition,
431-
);
432-
} else {
433-
_pendingDragOffset += OffsetPair(local: localDelta, global: delta);
434-
_lastPendingEventTimestamp = event.timeStamp;
435-
_lastTransform = event.transform;
436-
final Offset movedLocally = _getDeltaForDetails(localDelta);
437-
final Matrix4? localToGlobalTransform = event.transform == null ? null : Matrix4.tryInvert(event.transform!);
438-
_globalDistanceMoved += PointerEvent.transformDeltaViaPositions(
439-
transform: localToGlobalTransform,
440-
untransformedDelta: movedLocally,
441-
untransformedEndPosition: localPosition
442-
).distance * (_getPrimaryValueFromOffset(movedLocally) ?? 1).sign;
443-
if (_hasSufficientGlobalDistanceToAccept(event.kind, gestureSettings?.touchSlop)) {
444-
_hasDragThresholdBeenMet = true;
445-
if (_acceptedActivePointers.contains(event.pointer)) {
446-
_checkDrag(event.pointer);
447-
} else {
448-
resolve(GestureDisposition.accepted);
427+
switch (_state) {
428+
case _DragState.ready || _DragState.possible:
429+
_pendingDragOffset += OffsetPair(local: localDelta, global: delta);
430+
_lastPendingEventTimestamp = event.timeStamp;
431+
_lastTransform = event.transform;
432+
final Offset movedLocally = _getDeltaForDetails(localDelta);
433+
final Matrix4? localToGlobalTransform = event.transform == null ? null : Matrix4.tryInvert(event.transform!);
434+
_globalDistanceMoved += PointerEvent.transformDeltaViaPositions(
435+
transform: localToGlobalTransform,
436+
untransformedDelta: movedLocally,
437+
untransformedEndPosition: localPosition
438+
).distance * (_getPrimaryValueFromOffset(movedLocally) ?? 1).sign;
439+
if (_hasSufficientGlobalDistanceToAccept(event.kind, gestureSettings?.touchSlop)) {
440+
_hasDragThresholdBeenMet = true;
441+
if (_acceptedActivePointers.contains(event.pointer)) {
442+
_checkDrag(event.pointer);
443+
} else {
444+
resolve(GestureDisposition.accepted);
445+
}
449446
}
450-
}
447+
case _DragState.accepted:
448+
_checkUpdate(
449+
sourceTimeStamp: event.timeStamp,
450+
delta: _getDeltaForDetails(localDelta),
451+
primaryDelta: _getPrimaryValueFromOffset(localDelta),
452+
globalPosition: position,
453+
localPosition: localPosition,
454+
);
451455
}
452456
}
453-
if (event is PointerUpEvent || event is PointerCancelEvent || event is PointerPanZoomEndEvent) {
457+
if (event case PointerUpEvent() || PointerCancelEvent() || PointerPanZoomEndEvent()) {
454458
_giveUpPointer(event.pointer);
455459
}
456460
}

packages/flutter/lib/src/material/about.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,10 +1152,11 @@ class _MasterDetailFlowState extends State<_MasterDetailFlow> implements _PageOp
11521152
@override
11531153
void openDetailPage(Object arguments) {
11541154
_cachedDetailArguments = arguments;
1155-
if (_builtLayout == _LayoutMode.nested) {
1156-
_navigatorKey.currentState!.pushNamed(_navDetail, arguments: arguments);
1157-
} else {
1158-
focus = _Focus.detail;
1155+
switch (_builtLayout) {
1156+
case _LayoutMode.nested:
1157+
_navigatorKey.currentState!.pushNamed(_navDetail, arguments: arguments);
1158+
case _LayoutMode.lateral || null:
1159+
focus = _Focus.detail;
11591160
}
11601161
}
11611162

packages/flutter/lib/src/material/toggle_buttons.dart

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -455,19 +455,35 @@ class ToggleButtons extends StatelessWidget {
455455
// Determines if this is the first child that is being laid out
456456
// by the render object, _not_ the order of the children in its list.
457457
bool _isFirstButton(int index, int length, TextDirection textDirection) {
458-
return index == 0 && ((direction == Axis.horizontal && textDirection == TextDirection.ltr) ||
459-
(direction == Axis.vertical && verticalDirection == VerticalDirection.down))
460-
|| index == length - 1 && ((direction == Axis.horizontal && textDirection == TextDirection.rtl) ||
461-
(direction == Axis.vertical && verticalDirection == VerticalDirection.up));
458+
switch (direction) {
459+
case Axis.horizontal:
460+
return switch (textDirection) {
461+
TextDirection.rtl => index == length - 1,
462+
TextDirection.ltr => index == 0,
463+
};
464+
case Axis.vertical:
465+
return switch (verticalDirection) {
466+
VerticalDirection.up => index == length - 1,
467+
VerticalDirection.down => index == 0,
468+
};
469+
}
462470
}
463471

464472
// Determines if this is the last child that is being laid out
465473
// by the render object, _not_ the order of the children in its list.
466474
bool _isLastButton(int index, int length, TextDirection textDirection) {
467-
return index == length - 1 && ((direction == Axis.horizontal && textDirection == TextDirection.ltr) ||
468-
(direction == Axis.vertical && verticalDirection == VerticalDirection.down))
469-
|| index == 0 && ((direction == Axis.horizontal && textDirection == TextDirection.rtl) ||
470-
(direction == Axis.vertical && verticalDirection == VerticalDirection.up));
475+
switch (direction) {
476+
case Axis.horizontal:
477+
return switch (textDirection) {
478+
TextDirection.rtl => index == 0,
479+
TextDirection.ltr => index == length - 1,
480+
};
481+
case Axis.vertical:
482+
return switch (verticalDirection) {
483+
VerticalDirection.up => index == 0,
484+
VerticalDirection.down => index == length - 1,
485+
};
486+
}
471487
}
472488

473489
BorderRadius _getEdgeBorderRadius(

0 commit comments

Comments
 (0)