From 944cd11d87da00ede81127f183737cf99e4520cc Mon Sep 17 00:00:00 2001 From: Nate Date: Fri, 16 Feb 2024 13:19:34 -0700 Subject: [PATCH] Implementing `switch` expressions [refactoring `flutter/lib/src/`] (#143496) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR is the 8ᵗʰ step in the journey to solve issue #136139 and make the entire Flutter repo more readable. (previous pull requests: #139048, #139882, #141591, #142279, #142634, #142793, #143293) I did a pass through all of `packages/flutter/lib/src/` and found a whole bunch of `switch` statements to improve: most of them were really simple, but many involved some thorough refactoring. This pull request is just the complicated stuff. 😎 I'll make comments to describe the changes, and then in the future there will be another PR (and it'll be much easier to review than this one). --- .../flutter/lib/src/cupertino/colors.dart | 41 +++++------ packages/flutter/lib/src/material/chip.dart | 24 +++---- .../flutter/lib/src/material/date_picker.dart | 30 ++++---- packages/flutter/lib/src/material/drawer.dart | 21 ++---- .../flutter/lib/src/material/list_tile.dart | 68 ++++++------------- .../flutter/lib/src/rendering/sliver.dart | 21 ++---- .../lib/src/rendering/sliver_group.dart | 13 ++-- .../flutter/lib/src/rendering/viewport.dart | 60 ++++++---------- .../lib/src/services/raw_keyboard_ios.dart | 26 ++++--- .../lib/src/services/raw_keyboard_macos.dart | 26 ++++--- .../src/services/raw_keyboard_windows.dart | 30 ++++---- .../lib/src/widgets/overscroll_indicator.dart | 28 +++----- .../flutter/lib/src/widgets/scrollable.dart | 21 ++---- .../lib/src/widgets/scrollable_helpers.dart | 46 ++----------- 14 files changed, 151 insertions(+), 304 deletions(-) diff --git a/packages/flutter/lib/src/cupertino/colors.dart b/packages/flutter/lib/src/cupertino/colors.dart index b54e96b6911..cb17ce2fd03 100644 --- a/packages/flutter/lib/src/cupertino/colors.dart +++ b/packages/flutter/lib/src/cupertino/colors.dart @@ -1013,36 +1013,27 @@ class CupertinoDynamicColor extends Color with Diagnosticable { /// brightness, normal contrast, [CupertinoUserInterfaceLevelData.base] /// elevation level). CupertinoDynamicColor resolveFrom(BuildContext context) { - Brightness brightness = Brightness.light; - if (_isPlatformBrightnessDependent) { - brightness = CupertinoTheme.maybeBrightnessOf(context) ?? Brightness.light; - } - bool isHighContrastEnabled = false; - if (_isHighContrastDependent) { - isHighContrastEnabled = MediaQuery.maybeHighContrastOf(context) ?? false; - } + final Brightness brightness = _isPlatformBrightnessDependent + ? CupertinoTheme.maybeBrightnessOf(context) ?? Brightness.light + : Brightness.light; final CupertinoUserInterfaceLevelData level = _isInterfaceElevationDependent ? CupertinoUserInterfaceLevel.maybeOf(context) ?? CupertinoUserInterfaceLevelData.base : CupertinoUserInterfaceLevelData.base; - final Color resolved; - switch (brightness) { - case Brightness.light: - switch (level) { - case CupertinoUserInterfaceLevelData.base: - resolved = isHighContrastEnabled ? highContrastColor : color; - case CupertinoUserInterfaceLevelData.elevated: - resolved = isHighContrastEnabled ? highContrastElevatedColor : elevatedColor; - } - case Brightness.dark: - switch (level) { - case CupertinoUserInterfaceLevelData.base: - resolved = isHighContrastEnabled ? darkHighContrastColor : darkColor; - case CupertinoUserInterfaceLevelData.elevated: - resolved = isHighContrastEnabled ? darkHighContrastElevatedColor : darkElevatedColor; - } - } + final bool highContrast = _isHighContrastDependent + && (MediaQuery.maybeHighContrastOf(context) ?? false); + + final Color resolved = switch ((brightness, level, highContrast)) { + (Brightness.light, CupertinoUserInterfaceLevelData.base, false) => color, + (Brightness.light, CupertinoUserInterfaceLevelData.base, true) => highContrastColor, + (Brightness.light, CupertinoUserInterfaceLevelData.elevated, false) => elevatedColor, + (Brightness.light, CupertinoUserInterfaceLevelData.elevated, true) => highContrastElevatedColor, + (Brightness.dark, CupertinoUserInterfaceLevelData.base, false) => darkColor, + (Brightness.dark, CupertinoUserInterfaceLevelData.base, true) => darkHighContrastColor, + (Brightness.dark, CupertinoUserInterfaceLevelData.elevated, false) => darkElevatedColor, + (Brightness.dark, CupertinoUserInterfaceLevelData.elevated, true) => darkHighContrastElevatedColor, + }; Element? debugContext; assert(() { diff --git a/packages/flutter/lib/src/material/chip.dart b/packages/flutter/lib/src/material/chip.dart index ea269dcc48c..8ea38018f9f 100644 --- a/packages/flutter/lib/src/material/chip.dart +++ b/packages/flutter/lib/src/material/chip.dart @@ -1925,10 +1925,11 @@ class _RenderChip extends RenderBox with SlottedContainerRenderObjectMixin<_Chip assert(sizes.content >= boxSize.height); switch (textDirection!) { case TextDirection.rtl: - return Offset(x - boxSize.width, (sizes.content - boxSize.height + sizes.densityAdjustment.dy) / 2.0); + x -= boxSize.width; case TextDirection.ltr: - return Offset(x, (sizes.content - boxSize.height + sizes.densityAdjustment.dy) / 2.0); + break; } + return Offset(x, (sizes.content - boxSize.height + sizes.densityAdjustment.dy) / 2.0); } // These are the offsets to the upper left corners of the boxes (including @@ -2036,20 +2037,11 @@ class _RenderChip extends RenderBox with SlottedContainerRenderObjectMixin<_Chip if (enableAnimation.isCompleted) { return Colors.white; } - final ColorTween enableTween; - switch (theme.brightness) { - case Brightness.light: - enableTween = ColorTween( - begin: Colors.white.withAlpha(_kDisabledAlpha), - end: Colors.white, - ); - case Brightness.dark: - enableTween = ColorTween( - begin: Colors.black.withAlpha(_kDisabledAlpha), - end: Colors.black, - ); - } - return enableTween.evaluate(enableAnimation)!; + final Color color = switch (theme.brightness) { + Brightness.light => Colors.white, + Brightness.dark => Colors.black, + }; + return ColorTween(begin: color.withAlpha(_kDisabledAlpha), end: color).evaluate(enableAnimation)!; } void _paintCheck(Canvas canvas, Offset origin, double size) { diff --git a/packages/flutter/lib/src/material/date_picker.dart b/packages/flutter/lib/src/material/date_picker.dart index 3056c7480cd..8b5aadbe911 100644 --- a/packages/flutter/lib/src/material/date_picker.dart +++ b/packages/flutter/lib/src/material/date_picker.dart @@ -482,26 +482,20 @@ class _DatePickerDialogState extends State with RestorationMix Size _dialogSize(BuildContext context) { final bool useMaterial3 = Theme.of(context).useMaterial3; + final bool isCalendar = switch (_entryMode.value) { + DatePickerEntryMode.calendar || DatePickerEntryMode.calendarOnly => true, + DatePickerEntryMode.input || DatePickerEntryMode.inputOnly => false, + }; final Orientation orientation = MediaQuery.orientationOf(context); - switch (_entryMode.value) { - case DatePickerEntryMode.calendar: - case DatePickerEntryMode.calendarOnly: - switch (orientation) { - case Orientation.portrait: - return useMaterial3 ? _calendarPortraitDialogSizeM3 : _calendarPortraitDialogSizeM2; - case Orientation.landscape: - return _calendarLandscapeDialogSize; - } - case DatePickerEntryMode.input: - case DatePickerEntryMode.inputOnly: - switch (orientation) { - case Orientation.portrait: - return useMaterial3 ? _inputPortraitDialogSizeM3 : _inputPortraitDialogSizeM2; - case Orientation.landscape: - return _inputLandscapeDialogSize; - } - } + return switch ((isCalendar, orientation)) { + (true, Orientation.portrait) when useMaterial3 => _calendarPortraitDialogSizeM3, + (false, Orientation.portrait) when useMaterial3 => _inputPortraitDialogSizeM3, + (true, Orientation.portrait) => _calendarPortraitDialogSizeM2, + (false, Orientation.portrait) => _inputPortraitDialogSizeM2, + (true, Orientation.landscape) => _calendarLandscapeDialogSize, + (false, Orientation.landscape) => _inputLandscapeDialogSize, + }; } static const Map _formShortcutMap = { diff --git a/packages/flutter/lib/src/material/drawer.dart b/packages/flutter/lib/src/material/drawer.dart index abb67e32858..243f9895abd 100644 --- a/packages/flutter/lib/src/material/drawer.dart +++ b/packages/flutter/lib/src/material/drawer.dart @@ -662,8 +662,6 @@ class DrawerControllerState extends State with SingleTickerPro } Widget _buildDrawer(BuildContext context) { - final bool drawerIsStart = widget.alignment == DrawerAlignment.start; - final TextDirection textDirection = Directionality.of(context); final bool isDesktop; switch (Theme.of(context).platform) { case TargetPlatform.android: @@ -676,18 +674,13 @@ class DrawerControllerState extends State with SingleTickerPro isDesktop = true; } - double? dragAreaWidth = widget.edgeDragWidth; - if (widget.edgeDragWidth == null) { - final EdgeInsets padding = MediaQuery.paddingOf(context); - switch (textDirection) { - case TextDirection.ltr: - dragAreaWidth = _kEdgeDragWidth + - (drawerIsStart ? padding.left : padding.right); - case TextDirection.rtl: - dragAreaWidth = _kEdgeDragWidth + - (drawerIsStart ? padding.right : padding.left); - } - } + final double dragAreaWidth = widget.edgeDragWidth + ?? _kEdgeDragWidth + switch ((widget.alignment, Directionality.of(context))) { + (DrawerAlignment.start, TextDirection.ltr) => MediaQuery.paddingOf(context).left, + (DrawerAlignment.start, TextDirection.rtl) => MediaQuery.paddingOf(context).right, + (DrawerAlignment.end, TextDirection.rtl) => MediaQuery.paddingOf(context).left, + (DrawerAlignment.end, TextDirection.ltr) => MediaQuery.paddingOf(context).right, + }; if (_controller.status == AnimationStatus.dismissed) { if (widget.enableOpenDragGesture && !isDesktop) { diff --git a/packages/flutter/lib/src/material/list_tile.dart b/packages/flutter/lib/src/material/list_tile.dart index 3b1bcde4051..dc3cb8bbefe 100644 --- a/packages/flutter/lib/src/material/list_tile.dart +++ b/packages/flutter/lib/src/material/list_tile.dart @@ -1377,55 +1377,27 @@ class _RenderListTile extends RenderBox with SlottedContainerRenderObjectMixin<_ } } - final double leadingY; - final double trailingY; + final double leadingDiff = tileHeight - leadingSize.height; + final double trailingDiff = tileHeight - trailingSize.height; - switch (titleAlignment) { - case ListTileTitleAlignment.threeLine: { - if (isThreeLine) { - leadingY = _minVerticalPadding; - trailingY = _minVerticalPadding; - } else { - leadingY = (tileHeight - leadingSize.height) / 2.0; - trailingY = (tileHeight - trailingSize.height) / 2.0; - } - break; - } - case ListTileTitleAlignment.titleHeight: { - // This attempts to implement the redlines for the vertical position of the - // leading and trailing icons on the spec page: - // https://m2.material.io/components/lists#specs - // The interpretation for these redlines is as follows: - // - For large tiles (> 72dp), both leading and trailing controls should be - // a fixed distance from top. As per guidelines this is set to 16dp. - // - For smaller tiles, trailing should always be centered. Leading can be - // centered or closer to the top. It should never be further than 16dp - // to the top. - if (tileHeight > 72.0) { - leadingY = 16.0; - trailingY = 16.0; - } else { - leadingY = math.min((tileHeight - leadingSize.height) / 2.0, 16.0); - trailingY = (tileHeight - trailingSize.height) / 2.0; - } - break; - } - case ListTileTitleAlignment.top: { - leadingY = _minVerticalPadding; - trailingY = _minVerticalPadding; - break; - } - case ListTileTitleAlignment.center: { - leadingY = (tileHeight - leadingSize.height) / 2.0; - trailingY = (tileHeight - trailingSize.height) / 2.0; - break; - } - case ListTileTitleAlignment.bottom: { - leadingY = tileHeight - leadingSize.height - _minVerticalPadding; - trailingY = tileHeight - trailingSize.height - _minVerticalPadding; - break; - } - } + final (double leadingY, double trailingY) = switch (titleAlignment) { + ListTileTitleAlignment.threeLine when isThreeLine => (_minVerticalPadding, _minVerticalPadding), + ListTileTitleAlignment.threeLine => (leadingDiff / 2.0, trailingDiff / 2.0), + // This attempts to implement the redlines for the vertical position of the + // leading and trailing icons on the spec page: + // https://m2.material.io/components/lists#specs + // + // For large tiles (> 72dp), both leading and trailing controls should be + // a fixed distance from top. As per guidelines this is set to 16dp. + ListTileTitleAlignment.titleHeight when tileHeight > 72.0 => (16.0, 16.0), + // For smaller tiles, trailing should always be centered. Leading can be + // centered or closer to the top. It should never be further than 16dp + // to the top. + ListTileTitleAlignment.titleHeight => (math.min(leadingDiff / 2.0, 16.0), trailingDiff / 2.0), + ListTileTitleAlignment.top => (_minVerticalPadding, _minVerticalPadding), + ListTileTitleAlignment.center => (leadingDiff / 2.0, trailingDiff / 2.0), + ListTileTitleAlignment.bottom => (leadingDiff - _minVerticalPadding, trailingDiff - _minVerticalPadding), + }; switch (textDirection) { case TextDirection.rtl: { diff --git a/packages/flutter/lib/src/rendering/sliver.dart b/packages/flutter/lib/src/rendering/sliver.dart index 8c197447ed2..a060d75af69 100644 --- a/packages/flutter/lib/src/rendering/sliver.dart +++ b/packages/flutter/lib/src/rendering/sliver.dart @@ -1729,22 +1729,11 @@ abstract class RenderSliver extends RenderObject { /// Mixin for [RenderSliver] subclasses that provides some utility functions. mixin RenderSliverHelpers implements RenderSliver { bool _getRightWayUp(SliverConstraints constraints) { - bool rightWayUp; - switch (constraints.axisDirection) { - case AxisDirection.up: - case AxisDirection.left: - rightWayUp = false; - case AxisDirection.down: - case AxisDirection.right: - rightWayUp = true; - } - switch (constraints.growthDirection) { - case GrowthDirection.forward: - break; - case GrowthDirection.reverse: - rightWayUp = !rightWayUp; - } - return rightWayUp; + final bool reversed = axisDirectionIsReversed(constraints.axisDirection); + return switch (constraints.growthDirection) { + GrowthDirection.forward => !reversed, + GrowthDirection.reverse => reversed, + }; } /// Utility function for [hitTestChildren] for use when the children are diff --git a/packages/flutter/lib/src/rendering/sliver_group.dart b/packages/flutter/lib/src/rendering/sliver_group.dart index e336602dfb1..e75fe17fae2 100644 --- a/packages/flutter/lib/src/rendering/sliver_group.dart +++ b/packages/flutter/lib/src/rendering/sliver_group.dart @@ -37,14 +37,11 @@ class RenderSliverCrossAxisGroup extends RenderSliver with ContainerRenderObject @override double childCrossAxisPosition(RenderSliver child) { - switch (constraints.axisDirection) { - case AxisDirection.up: - case AxisDirection.down: - return (child.parentData! as SliverPhysicalParentData).paintOffset.dx; - case AxisDirection.left: - case AxisDirection.right: - return (child.parentData! as SliverPhysicalParentData).paintOffset.dy; - } + final Offset paintOffset = (child.parentData! as SliverPhysicalParentData).paintOffset; + return switch (constraints.axis) { + Axis.vertical => paintOffset.dx, + Axis.horizontal => paintOffset.dy, + }; } @override diff --git a/packages/flutter/lib/src/rendering/viewport.dart b/packages/flutter/lib/src/rendering/viewport.dart index 3bfd1dbcbed..612cba836d8 100644 --- a/packages/flutter/lib/src/rendering/viewport.dart +++ b/packages/flutter/lib/src/rendering/viewport.dart @@ -925,22 +925,13 @@ abstract class RenderViewportBase pivotExtent - rectLocal.bottom, + AxisDirection.left => pivotExtent - rectLocal.right, + AxisDirection.right => rectLocal.left, + AxisDirection.down => rectLocal.top, + }; // So far leadingScrollOffset is the scroll offset of `rect` in the `child` // sliver's sliver coordinate system. The sign of this value indicates @@ -973,35 +964,26 @@ abstract class RenderViewportBase targetRect.height, + Axis.horizontal => targetRect.width, + }; } - final double mainAxisExtent; - switch (axis) { - case Axis.horizontal: - mainAxisExtent = size.width - extentOfPinnedSlivers; - case Axis.vertical: - mainAxisExtent = size.height - extentOfPinnedSlivers; - } + final double mainAxisExtentDifference = switch (axis) { + Axis.horizontal => size.width - extentOfPinnedSlivers - rectLocal.width, + Axis.vertical => size.height - extentOfPinnedSlivers - rectLocal.height, + }; - final double targetOffset = leadingScrollOffset - (mainAxisExtent - targetMainAxisExtent) * alignment; + final double targetOffset = leadingScrollOffset - mainAxisExtentDifference * alignment; final double offsetDifference = offset.pixels - targetOffset; - switch (axisDirection) { - case AxisDirection.down: - targetRect = targetRect.translate(0.0, offsetDifference); - case AxisDirection.right: - targetRect = targetRect.translate(offsetDifference, 0.0); - case AxisDirection.up: - targetRect = targetRect.translate(0.0, -offsetDifference); - case AxisDirection.left: - targetRect = targetRect.translate(-offsetDifference, 0.0); - } + targetRect = switch (axisDirection) { + AxisDirection.up => targetRect.translate(0.0, -offsetDifference), + AxisDirection.down => targetRect.translate(0.0, offsetDifference), + AxisDirection.left => targetRect.translate(-offsetDifference, 0.0), + AxisDirection.right => targetRect.translate( offsetDifference, 0.0), + }; return RevealedOffset(offset: targetOffset, rect: targetRect); } diff --git a/packages/flutter/lib/src/services/raw_keyboard_ios.dart b/packages/flutter/lib/src/services/raw_keyboard_ios.dart index 300fa5f94d5..ca88e0325f5 100644 --- a/packages/flutter/lib/src/services/raw_keyboard_ios.dart +++ b/packages/flutter/lib/src/services/raw_keyboard_ios.dart @@ -145,21 +145,19 @@ class RawKeyEventDataIos extends RawKeyEventData { if (modifiers & anyMask == 0) { return false; } - // If only the "anyMask" bit is set, then we respond true for requests of - // whether either left or right is pressed. Handles the case where iOS - // supplies just the "either" modifier flag, but not the left/right flag. - // (e.g. modifierShift but not modifierLeftShift). - final bool anyOnly = modifiers & (leftMask | rightMask | anyMask) == anyMask; - switch (side) { - case KeyboardSide.any: - return true; - case KeyboardSide.all: - return modifiers & leftMask != 0 && modifiers & rightMask != 0 || anyOnly; - case KeyboardSide.left: - return modifiers & leftMask != 0 || anyOnly; - case KeyboardSide.right: - return modifiers & rightMask != 0 || anyOnly; + if (modifiers & (leftMask | rightMask | anyMask) == anyMask) { + // If only the "anyMask" bit is set, then we respond true for requests of + // whether either left or right is pressed. Handles the case where iOS + // supplies just the "either" modifier flag, but not the left/right flag. + // (e.g. modifierShift but not modifierLeftShift). + return true; } + return switch (side) { + KeyboardSide.any => true, + KeyboardSide.all => modifiers & leftMask != 0 && modifiers & rightMask != 0, + KeyboardSide.left => modifiers & leftMask != 0, + KeyboardSide.right => modifiers & rightMask != 0, + }; } @override diff --git a/packages/flutter/lib/src/services/raw_keyboard_macos.dart b/packages/flutter/lib/src/services/raw_keyboard_macos.dart index 0ab2fe5f098..921f01a9090 100644 --- a/packages/flutter/lib/src/services/raw_keyboard_macos.dart +++ b/packages/flutter/lib/src/services/raw_keyboard_macos.dart @@ -147,21 +147,19 @@ class RawKeyEventDataMacOs extends RawKeyEventData { if (modifiers & anyMask == 0) { return false; } - // If only the "anyMask" bit is set, then we respond true for requests of - // whether either left or right is pressed. Handles the case where macOS - // supplies just the "either" modifier flag, but not the left/right flag. - // (e.g. modifierShift but not modifierLeftShift). - final bool anyOnly = modifiers & (leftMask | rightMask | anyMask) == anyMask; - switch (side) { - case KeyboardSide.any: - return true; - case KeyboardSide.all: - return modifiers & leftMask != 0 && modifiers & rightMask != 0 || anyOnly; - case KeyboardSide.left: - return modifiers & leftMask != 0 || anyOnly; - case KeyboardSide.right: - return modifiers & rightMask != 0 || anyOnly; + if (modifiers & (leftMask | rightMask | anyMask) == anyMask) { + // If only the "anyMask" bit is set, then we respond true for requests of + // whether either left or right is pressed. Handles the case where macOS + // supplies just the "either" modifier flag, but not the left/right flag. + // (e.g. modifierShift but not modifierLeftShift). + return true; } + return switch (side) { + KeyboardSide.any => true, + KeyboardSide.all => modifiers & leftMask != 0 && modifiers & rightMask != 0, + KeyboardSide.left => modifiers & leftMask != 0, + KeyboardSide.right => modifiers & rightMask != 0, + }; } @override diff --git a/packages/flutter/lib/src/services/raw_keyboard_windows.dart b/packages/flutter/lib/src/services/raw_keyboard_windows.dart index 8d43ea1a886..e29f1e4fa42 100644 --- a/packages/flutter/lib/src/services/raw_keyboard_windows.dart +++ b/packages/flutter/lib/src/services/raw_keyboard_windows.dart @@ -101,26 +101,22 @@ class RawKeyEventDataWindows extends RawKeyEventData { } bool _isLeftRightModifierPressed(KeyboardSide side, int anyMask, int leftMask, int rightMask) { - if (modifiers & anyMask == 0 && - modifiers & leftMask == 0 && - modifiers & rightMask == 0) { + if (modifiers & (leftMask | rightMask | anyMask) == 0) { return false; } - // If only the "anyMask" bit is set, then we respond true for requests of - // whether either left or right is pressed. Handles the case where Windows - // supplies just the "either" modifier flag, but not the left/right flag. - // (e.g. modifierShift but not modifierLeftShift). - final bool anyOnly = modifiers & (leftMask | rightMask | anyMask) == anyMask; - switch (side) { - case KeyboardSide.any: - return true; - case KeyboardSide.all: - return modifiers & leftMask != 0 && modifiers & rightMask != 0 || anyOnly; - case KeyboardSide.left: - return modifiers & leftMask != 0 || anyOnly; - case KeyboardSide.right: - return modifiers & rightMask != 0 || anyOnly; + if (modifiers & (leftMask | rightMask | anyMask) == anyMask) { + // If only the "anyMask" bit is set, then we respond true for requests of + // whether either left or right is pressed. Handles the case where Windows + // supplies just the "either" modifier flag, but not the left/right flag. + // (e.g. modifierShift but not modifierLeftShift). + return true; } + return switch (side) { + KeyboardSide.any => true, + KeyboardSide.all => modifiers & leftMask != 0 && modifiers & rightMask != 0, + KeyboardSide.left => modifiers & leftMask != 0, + KeyboardSide.right => modifiers & rightMask != 0, + }; } @override diff --git a/packages/flutter/lib/src/widgets/overscroll_indicator.dart b/packages/flutter/lib/src/widgets/overscroll_indicator.dart index 3d8b9f94d66..42025034069 100644 --- a/packages/flutter/lib/src/widgets/overscroll_indicator.dart +++ b/packages/flutter/lib/src/widgets/overscroll_indicator.dart @@ -731,24 +731,16 @@ class _StretchingOverscrollIndicatorState extends State widget.axisDirection, + _StretchDirection.leading => flipAxisDirection(widget.axisDirection), + }; + return switch (direction) { + AxisDirection.up => AlignmentDirectional.topCenter, + AxisDirection.down => AlignmentDirectional.bottomCenter, + AxisDirection.left => Alignment.centerLeft, + AxisDirection.right => Alignment.centerRight, + }; } @override diff --git a/packages/flutter/lib/src/widgets/scrollable.dart b/packages/flutter/lib/src/widgets/scrollable.dart index 9b55e707166..76436807738 100644 --- a/packages/flutter/lib/src/widgets/scrollable.dart +++ b/packages/flutter/lib/src/widgets/scrollable.dart @@ -885,7 +885,6 @@ class ScrollableState extends State with TickerProviderStateMixin, R // direction, and any modifiers specified by the ScrollBehavior taken into // account. double _pointerSignalEventDelta(PointerScrollEvent event) { - late double delta; final Set pressed = HardwareKeyboard.instance.logicalKeysPressed; final bool flipAxes = pressed.any(_configuration.pointerAxisModifiers.contains) && // Axes are only flipped for physical mouse wheel input. @@ -896,21 +895,13 @@ class ScrollableState extends State with TickerProviderStateMixin, R // axis. event.kind == PointerDeviceKind.mouse; - switch (widget.axis) { - case Axis.horizontal: - delta = flipAxes - ? event.scrollDelta.dy - : event.scrollDelta.dx; - case Axis.vertical: - delta = flipAxes - ? event.scrollDelta.dx - : event.scrollDelta.dy; - } + final Axis axis = flipAxes ? flipAxis(widget.axis) : widget.axis; + final double delta = switch (axis) { + Axis.horizontal => event.scrollDelta.dx, + Axis.vertical => event.scrollDelta.dy, + }; - if (axisDirectionIsReversed(widget.axisDirection)) { - delta *= -1; - } - return delta; + return axisDirectionIsReversed(widget.axisDirection) ? -delta : delta; } void _receivedPointerSignal(PointerSignalEvent event) { diff --git a/packages/flutter/lib/src/widgets/scrollable_helpers.dart b/packages/flutter/lib/src/widgets/scrollable_helpers.dart index 1854cfc2175..e61c015a922 100644 --- a/packages/flutter/lib/src/widgets/scrollable_helpers.dart +++ b/packages/flutter/lib/src/widgets/scrollable_helpers.dart @@ -426,49 +426,11 @@ class ScrollAction extends ContextAction { /// Find out how much of an increment to move by, taking the different /// directions into account. static double getDirectionalIncrement(ScrollableState state, ScrollIntent intent) { - final double increment = _calculateScrollIncrement(state, type: intent.type); - switch (intent.direction) { - case AxisDirection.down: - switch (state.axisDirection) { - case AxisDirection.up: - return -increment; - case AxisDirection.down: - return increment; - case AxisDirection.right: - case AxisDirection.left: - return 0.0; - } - case AxisDirection.up: - switch (state.axisDirection) { - case AxisDirection.up: - return increment; - case AxisDirection.down: - return -increment; - case AxisDirection.right: - case AxisDirection.left: - return 0.0; - } - case AxisDirection.left: - switch (state.axisDirection) { - case AxisDirection.right: - return -increment; - case AxisDirection.left: - return increment; - case AxisDirection.up: - case AxisDirection.down: - return 0.0; - } - case AxisDirection.right: - switch (state.axisDirection) { - case AxisDirection.right: - return increment; - case AxisDirection.left: - return -increment; - case AxisDirection.up: - case AxisDirection.down: - return 0.0; - } + if (axisDirectionToAxis(intent.direction) == axisDirectionToAxis(state.axisDirection)) { + final double increment = _calculateScrollIncrement(state, type: intent.type); + return intent.direction == state.axisDirection ? increment : -increment; } + return 0.0; } @override