Unverified Commit 944cd11d authored by Nate's avatar Nate Committed by GitHub

Implementing `switch` expressions [refactoring `flutter/lib/src/`] (#143496)

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).
parent 383067fc
...@@ -1013,36 +1013,27 @@ class CupertinoDynamicColor extends Color with Diagnosticable { ...@@ -1013,36 +1013,27 @@ class CupertinoDynamicColor extends Color with Diagnosticable {
/// brightness, normal contrast, [CupertinoUserInterfaceLevelData.base] /// brightness, normal contrast, [CupertinoUserInterfaceLevelData.base]
/// elevation level). /// elevation level).
CupertinoDynamicColor resolveFrom(BuildContext context) { CupertinoDynamicColor resolveFrom(BuildContext context) {
Brightness brightness = Brightness.light; final Brightness brightness = _isPlatformBrightnessDependent
if (_isPlatformBrightnessDependent) { ? CupertinoTheme.maybeBrightnessOf(context) ?? Brightness.light
brightness = CupertinoTheme.maybeBrightnessOf(context) ?? Brightness.light; : Brightness.light;
}
bool isHighContrastEnabled = false;
if (_isHighContrastDependent) {
isHighContrastEnabled = MediaQuery.maybeHighContrastOf(context) ?? false;
}
final CupertinoUserInterfaceLevelData level = _isInterfaceElevationDependent final CupertinoUserInterfaceLevelData level = _isInterfaceElevationDependent
? CupertinoUserInterfaceLevel.maybeOf(context) ?? CupertinoUserInterfaceLevelData.base ? CupertinoUserInterfaceLevel.maybeOf(context) ?? CupertinoUserInterfaceLevelData.base
: CupertinoUserInterfaceLevelData.base; : CupertinoUserInterfaceLevelData.base;
final Color resolved; final bool highContrast = _isHighContrastDependent
switch (brightness) { && (MediaQuery.maybeHighContrastOf(context) ?? false);
case Brightness.light:
switch (level) { final Color resolved = switch ((brightness, level, highContrast)) {
case CupertinoUserInterfaceLevelData.base: (Brightness.light, CupertinoUserInterfaceLevelData.base, false) => color,
resolved = isHighContrastEnabled ? highContrastColor : color; (Brightness.light, CupertinoUserInterfaceLevelData.base, true) => highContrastColor,
case CupertinoUserInterfaceLevelData.elevated: (Brightness.light, CupertinoUserInterfaceLevelData.elevated, false) => elevatedColor,
resolved = isHighContrastEnabled ? highContrastElevatedColor : elevatedColor; (Brightness.light, CupertinoUserInterfaceLevelData.elevated, true) => highContrastElevatedColor,
} (Brightness.dark, CupertinoUserInterfaceLevelData.base, false) => darkColor,
case Brightness.dark: (Brightness.dark, CupertinoUserInterfaceLevelData.base, true) => darkHighContrastColor,
switch (level) { (Brightness.dark, CupertinoUserInterfaceLevelData.elevated, false) => darkElevatedColor,
case CupertinoUserInterfaceLevelData.base: (Brightness.dark, CupertinoUserInterfaceLevelData.elevated, true) => darkHighContrastElevatedColor,
resolved = isHighContrastEnabled ? darkHighContrastColor : darkColor; };
case CupertinoUserInterfaceLevelData.elevated:
resolved = isHighContrastEnabled ? darkHighContrastElevatedColor : darkElevatedColor;
}
}
Element? debugContext; Element? debugContext;
assert(() { assert(() {
......
...@@ -1925,10 +1925,11 @@ class _RenderChip extends RenderBox with SlottedContainerRenderObjectMixin<_Chip ...@@ -1925,10 +1925,11 @@ class _RenderChip extends RenderBox with SlottedContainerRenderObjectMixin<_Chip
assert(sizes.content >= boxSize.height); assert(sizes.content >= boxSize.height);
switch (textDirection!) { switch (textDirection!) {
case TextDirection.rtl: case TextDirection.rtl:
return Offset(x - boxSize.width, (sizes.content - boxSize.height + sizes.densityAdjustment.dy) / 2.0); x -= boxSize.width;
case TextDirection.ltr: 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 // These are the offsets to the upper left corners of the boxes (including
...@@ -2036,20 +2037,11 @@ class _RenderChip extends RenderBox with SlottedContainerRenderObjectMixin<_Chip ...@@ -2036,20 +2037,11 @@ class _RenderChip extends RenderBox with SlottedContainerRenderObjectMixin<_Chip
if (enableAnimation.isCompleted) { if (enableAnimation.isCompleted) {
return Colors.white; return Colors.white;
} }
final ColorTween enableTween; final Color color = switch (theme.brightness) {
switch (theme.brightness) { Brightness.light => Colors.white,
case Brightness.light: Brightness.dark => Colors.black,
enableTween = ColorTween( };
begin: Colors.white.withAlpha(_kDisabledAlpha), return ColorTween(begin: color.withAlpha(_kDisabledAlpha), end: color).evaluate(enableAnimation)!;
end: Colors.white,
);
case Brightness.dark:
enableTween = ColorTween(
begin: Colors.black.withAlpha(_kDisabledAlpha),
end: Colors.black,
);
}
return enableTween.evaluate(enableAnimation)!;
} }
void _paintCheck(Canvas canvas, Offset origin, double size) { void _paintCheck(Canvas canvas, Offset origin, double size) {
......
...@@ -482,26 +482,20 @@ class _DatePickerDialogState extends State<DatePickerDialog> with RestorationMix ...@@ -482,26 +482,20 @@ class _DatePickerDialogState extends State<DatePickerDialog> with RestorationMix
Size _dialogSize(BuildContext context) { Size _dialogSize(BuildContext context) {
final bool useMaterial3 = Theme.of(context).useMaterial3; 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); final Orientation orientation = MediaQuery.orientationOf(context);
switch (_entryMode.value) { return switch ((isCalendar, orientation)) {
case DatePickerEntryMode.calendar: (true, Orientation.portrait) when useMaterial3 => _calendarPortraitDialogSizeM3,
case DatePickerEntryMode.calendarOnly: (false, Orientation.portrait) when useMaterial3 => _inputPortraitDialogSizeM3,
switch (orientation) { (true, Orientation.portrait) => _calendarPortraitDialogSizeM2,
case Orientation.portrait: (false, Orientation.portrait) => _inputPortraitDialogSizeM2,
return useMaterial3 ? _calendarPortraitDialogSizeM3 : _calendarPortraitDialogSizeM2; (true, Orientation.landscape) => _calendarLandscapeDialogSize,
case Orientation.landscape: (false, Orientation.landscape) => _inputLandscapeDialogSize,
return _calendarLandscapeDialogSize; };
}
case DatePickerEntryMode.input:
case DatePickerEntryMode.inputOnly:
switch (orientation) {
case Orientation.portrait:
return useMaterial3 ? _inputPortraitDialogSizeM3 : _inputPortraitDialogSizeM2;
case Orientation.landscape:
return _inputLandscapeDialogSize;
}
}
} }
static const Map<ShortcutActivator, Intent> _formShortcutMap = <ShortcutActivator, Intent>{ static const Map<ShortcutActivator, Intent> _formShortcutMap = <ShortcutActivator, Intent>{
......
...@@ -662,8 +662,6 @@ class DrawerControllerState extends State<DrawerController> with SingleTickerPro ...@@ -662,8 +662,6 @@ class DrawerControllerState extends State<DrawerController> with SingleTickerPro
} }
Widget _buildDrawer(BuildContext context) { Widget _buildDrawer(BuildContext context) {
final bool drawerIsStart = widget.alignment == DrawerAlignment.start;
final TextDirection textDirection = Directionality.of(context);
final bool isDesktop; final bool isDesktop;
switch (Theme.of(context).platform) { switch (Theme.of(context).platform) {
case TargetPlatform.android: case TargetPlatform.android:
...@@ -676,18 +674,13 @@ class DrawerControllerState extends State<DrawerController> with SingleTickerPro ...@@ -676,18 +674,13 @@ class DrawerControllerState extends State<DrawerController> with SingleTickerPro
isDesktop = true; isDesktop = true;
} }
double? dragAreaWidth = widget.edgeDragWidth; final double dragAreaWidth = widget.edgeDragWidth
if (widget.edgeDragWidth == null) { ?? _kEdgeDragWidth + switch ((widget.alignment, Directionality.of(context))) {
final EdgeInsets padding = MediaQuery.paddingOf(context); (DrawerAlignment.start, TextDirection.ltr) => MediaQuery.paddingOf(context).left,
switch (textDirection) { (DrawerAlignment.start, TextDirection.rtl) => MediaQuery.paddingOf(context).right,
case TextDirection.ltr: (DrawerAlignment.end, TextDirection.rtl) => MediaQuery.paddingOf(context).left,
dragAreaWidth = _kEdgeDragWidth + (DrawerAlignment.end, TextDirection.ltr) => MediaQuery.paddingOf(context).right,
(drawerIsStart ? padding.left : padding.right); };
case TextDirection.rtl:
dragAreaWidth = _kEdgeDragWidth +
(drawerIsStart ? padding.right : padding.left);
}
}
if (_controller.status == AnimationStatus.dismissed) { if (_controller.status == AnimationStatus.dismissed) {
if (widget.enableOpenDragGesture && !isDesktop) { if (widget.enableOpenDragGesture && !isDesktop) {
......
...@@ -1377,55 +1377,27 @@ class _RenderListTile extends RenderBox with SlottedContainerRenderObjectMixin<_ ...@@ -1377,55 +1377,27 @@ class _RenderListTile extends RenderBox with SlottedContainerRenderObjectMixin<_
} }
} }
final double leadingY; final double leadingDiff = tileHeight - leadingSize.height;
final double trailingY; final double trailingDiff = tileHeight - trailingSize.height;
switch (titleAlignment) { final (double leadingY, double trailingY) = switch (titleAlignment) {
case ListTileTitleAlignment.threeLine: { ListTileTitleAlignment.threeLine when isThreeLine => (_minVerticalPadding, _minVerticalPadding),
if (isThreeLine) { ListTileTitleAlignment.threeLine => (leadingDiff / 2.0, trailingDiff / 2.0),
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 // This attempts to implement the redlines for the vertical position of the
// leading and trailing icons on the spec page: // leading and trailing icons on the spec page:
// https://m2.material.io/components/lists#specs // 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 // For large tiles (> 72dp), both leading and trailing controls should be
// a fixed distance from top. As per guidelines this is set to 16dp. // a fixed distance from top. As per guidelines this is set to 16dp.
// - For smaller tiles, trailing should always be centered. Leading can be 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 // centered or closer to the top. It should never be further than 16dp
// to the top. // to the top.
if (tileHeight > 72.0) { ListTileTitleAlignment.titleHeight => (math.min(leadingDiff / 2.0, 16.0), trailingDiff / 2.0),
leadingY = 16.0; ListTileTitleAlignment.top => (_minVerticalPadding, _minVerticalPadding),
trailingY = 16.0; ListTileTitleAlignment.center => (leadingDiff / 2.0, trailingDiff / 2.0),
} else { ListTileTitleAlignment.bottom => (leadingDiff - _minVerticalPadding, trailingDiff - _minVerticalPadding),
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;
}
}
switch (textDirection) { switch (textDirection) {
case TextDirection.rtl: { case TextDirection.rtl: {
......
...@@ -1729,22 +1729,11 @@ abstract class RenderSliver extends RenderObject { ...@@ -1729,22 +1729,11 @@ abstract class RenderSliver extends RenderObject {
/// Mixin for [RenderSliver] subclasses that provides some utility functions. /// Mixin for [RenderSliver] subclasses that provides some utility functions.
mixin RenderSliverHelpers implements RenderSliver { mixin RenderSliverHelpers implements RenderSliver {
bool _getRightWayUp(SliverConstraints constraints) { bool _getRightWayUp(SliverConstraints constraints) {
bool rightWayUp; final bool reversed = axisDirectionIsReversed(constraints.axisDirection);
switch (constraints.axisDirection) { return switch (constraints.growthDirection) {
case AxisDirection.up: GrowthDirection.forward => !reversed,
case AxisDirection.left: GrowthDirection.reverse => reversed,
rightWayUp = false; };
case AxisDirection.down:
case AxisDirection.right:
rightWayUp = true;
}
switch (constraints.growthDirection) {
case GrowthDirection.forward:
break;
case GrowthDirection.reverse:
rightWayUp = !rightWayUp;
}
return rightWayUp;
} }
/// Utility function for [hitTestChildren] for use when the children are /// Utility function for [hitTestChildren] for use when the children are
......
...@@ -37,14 +37,11 @@ class RenderSliverCrossAxisGroup extends RenderSliver with ContainerRenderObject ...@@ -37,14 +37,11 @@ class RenderSliverCrossAxisGroup extends RenderSliver with ContainerRenderObject
@override @override
double childCrossAxisPosition(RenderSliver child) { double childCrossAxisPosition(RenderSliver child) {
switch (constraints.axisDirection) { final Offset paintOffset = (child.parentData! as SliverPhysicalParentData).paintOffset;
case AxisDirection.up: return switch (constraints.axis) {
case AxisDirection.down: Axis.vertical => paintOffset.dx,
return (child.parentData! as SliverPhysicalParentData).paintOffset.dx; Axis.horizontal => paintOffset.dy,
case AxisDirection.left: };
case AxisDirection.right:
return (child.parentData! as SliverPhysicalParentData).paintOffset.dy;
}
} }
@override @override
......
...@@ -925,22 +925,13 @@ abstract class RenderViewportBase<ParentDataClass extends ContainerParentDataMix ...@@ -925,22 +925,13 @@ abstract class RenderViewportBase<ParentDataClass extends ContainerParentDataMix
assert(child is RenderSliver); assert(child is RenderSliver);
final RenderSliver sliver = child as RenderSliver; final RenderSliver sliver = child as RenderSliver;
final double targetMainAxisExtent;
// The scroll offset of `rect` within `child`. // The scroll offset of `rect` within `child`.
switch (applyGrowthDirectionToAxisDirection(axisDirection, growthDirection)) { leadingScrollOffset += switch (applyGrowthDirectionToAxisDirection(axisDirection, growthDirection)) {
case AxisDirection.up: AxisDirection.up => pivotExtent - rectLocal.bottom,
leadingScrollOffset += pivotExtent - rectLocal.bottom; AxisDirection.left => pivotExtent - rectLocal.right,
targetMainAxisExtent = rectLocal.height; AxisDirection.right => rectLocal.left,
case AxisDirection.right: AxisDirection.down => rectLocal.top,
leadingScrollOffset += rectLocal.left; };
targetMainAxisExtent = rectLocal.width;
case AxisDirection.down:
leadingScrollOffset += rectLocal.top;
targetMainAxisExtent = rectLocal.height;
case AxisDirection.left:
leadingScrollOffset += pivotExtent - rectLocal.right;
targetMainAxisExtent = rectLocal.width;
}
// So far leadingScrollOffset is the scroll offset of `rect` in the `child` // So far leadingScrollOffset is the scroll offset of `rect` in the `child`
// sliver's sliver coordinate system. The sign of this value indicates // sliver's sliver coordinate system. The sign of this value indicates
...@@ -973,35 +964,26 @@ abstract class RenderViewportBase<ParentDataClass extends ContainerParentDataMix ...@@ -973,35 +964,26 @@ abstract class RenderViewportBase<ParentDataClass extends ContainerParentDataMix
// If child's growth direction is reverse, when viewport.offset is // If child's growth direction is reverse, when viewport.offset is
// `leadingScrollOffset`, it is positioned just outside of the leading // `leadingScrollOffset`, it is positioned just outside of the leading
// edge of the viewport. // edge of the viewport.
switch (axis) { leadingScrollOffset -= switch (axis) {
case Axis.vertical: Axis.vertical => targetRect.height,
leadingScrollOffset -= targetRect.height; Axis.horizontal => targetRect.width,
case Axis.horizontal: };
leadingScrollOffset -= targetRect.width;
}
} }
final double mainAxisExtent; final double mainAxisExtentDifference = switch (axis) {
switch (axis) { Axis.horizontal => size.width - extentOfPinnedSlivers - rectLocal.width,
case Axis.horizontal: Axis.vertical => size.height - extentOfPinnedSlivers - rectLocal.height,
mainAxisExtent = size.width - extentOfPinnedSlivers; };
case Axis.vertical:
mainAxisExtent = size.height - extentOfPinnedSlivers;
}
final double targetOffset = leadingScrollOffset - (mainAxisExtent - targetMainAxisExtent) * alignment; final double targetOffset = leadingScrollOffset - mainAxisExtentDifference * alignment;
final double offsetDifference = offset.pixels - targetOffset; final double offsetDifference = offset.pixels - targetOffset;
switch (axisDirection) { targetRect = switch (axisDirection) {
case AxisDirection.down: AxisDirection.up => targetRect.translate(0.0, -offsetDifference),
targetRect = targetRect.translate(0.0, offsetDifference); AxisDirection.down => targetRect.translate(0.0, offsetDifference),
case AxisDirection.right: AxisDirection.left => targetRect.translate(-offsetDifference, 0.0),
targetRect = targetRect.translate(offsetDifference, 0.0); AxisDirection.right => targetRect.translate( offsetDifference, 0.0),
case AxisDirection.up: };
targetRect = targetRect.translate(0.0, -offsetDifference);
case AxisDirection.left:
targetRect = targetRect.translate(-offsetDifference, 0.0);
}
return RevealedOffset(offset: targetOffset, rect: targetRect); return RevealedOffset(offset: targetOffset, rect: targetRect);
} }
......
...@@ -145,21 +145,19 @@ class RawKeyEventDataIos extends RawKeyEventData { ...@@ -145,21 +145,19 @@ class RawKeyEventDataIos extends RawKeyEventData {
if (modifiers & anyMask == 0) { if (modifiers & anyMask == 0) {
return false; return false;
} }
if (modifiers & (leftMask | rightMask | anyMask) == anyMask) {
// If only the "anyMask" bit is set, then we respond true for requests of // 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 // whether either left or right is pressed. Handles the case where iOS
// supplies just the "either" modifier flag, but not the left/right flag. // supplies just the "either" modifier flag, but not the left/right flag.
// (e.g. modifierShift but not modifierLeftShift). // (e.g. modifierShift but not modifierLeftShift).
final bool anyOnly = modifiers & (leftMask | rightMask | anyMask) == anyMask;
switch (side) {
case KeyboardSide.any:
return true; 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;
} }
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 @override
......
...@@ -147,21 +147,19 @@ class RawKeyEventDataMacOs extends RawKeyEventData { ...@@ -147,21 +147,19 @@ class RawKeyEventDataMacOs extends RawKeyEventData {
if (modifiers & anyMask == 0) { if (modifiers & anyMask == 0) {
return false; return false;
} }
if (modifiers & (leftMask | rightMask | anyMask) == anyMask) {
// If only the "anyMask" bit is set, then we respond true for requests of // 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 // whether either left or right is pressed. Handles the case where macOS
// supplies just the "either" modifier flag, but not the left/right flag. // supplies just the "either" modifier flag, but not the left/right flag.
// (e.g. modifierShift but not modifierLeftShift). // (e.g. modifierShift but not modifierLeftShift).
final bool anyOnly = modifiers & (leftMask | rightMask | anyMask) == anyMask;
switch (side) {
case KeyboardSide.any:
return true; 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;
} }
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 @override
......
...@@ -101,26 +101,22 @@ class RawKeyEventDataWindows extends RawKeyEventData { ...@@ -101,26 +101,22 @@ class RawKeyEventDataWindows extends RawKeyEventData {
} }
bool _isLeftRightModifierPressed(KeyboardSide side, int anyMask, int leftMask, int rightMask) { bool _isLeftRightModifierPressed(KeyboardSide side, int anyMask, int leftMask, int rightMask) {
if (modifiers & anyMask == 0 && if (modifiers & (leftMask | rightMask | anyMask) == 0) {
modifiers & leftMask == 0 &&
modifiers & rightMask == 0) {
return false; return false;
} }
if (modifiers & (leftMask | rightMask | anyMask) == anyMask) {
// If only the "anyMask" bit is set, then we respond true for requests of // 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 // whether either left or right is pressed. Handles the case where Windows
// supplies just the "either" modifier flag, but not the left/right flag. // supplies just the "either" modifier flag, but not the left/right flag.
// (e.g. modifierShift but not modifierLeftShift). // (e.g. modifierShift but not modifierLeftShift).
final bool anyOnly = modifiers & (leftMask | rightMask | anyMask) == anyMask;
switch (side) {
case KeyboardSide.any:
return true; 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;
} }
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 @override
......
...@@ -731,24 +731,16 @@ class _StretchingOverscrollIndicatorState extends State<StretchingOverscrollIndi ...@@ -731,24 +731,16 @@ class _StretchingOverscrollIndicatorState extends State<StretchingOverscrollIndi
AlignmentGeometry _getAlignmentForAxisDirection(_StretchDirection stretchDirection) { AlignmentGeometry _getAlignmentForAxisDirection(_StretchDirection stretchDirection) {
// Accounts for reversed scrollables by checking the AxisDirection // Accounts for reversed scrollables by checking the AxisDirection
switch (widget.axisDirection) { final AxisDirection direction = switch (stretchDirection) {
case AxisDirection.up: _StretchDirection.trailing => widget.axisDirection,
return stretchDirection == _StretchDirection.trailing _StretchDirection.leading => flipAxisDirection(widget.axisDirection),
? AlignmentDirectional.topCenter };
: AlignmentDirectional.bottomCenter; return switch (direction) {
case AxisDirection.right: AxisDirection.up => AlignmentDirectional.topCenter,
return stretchDirection == _StretchDirection.trailing AxisDirection.down => AlignmentDirectional.bottomCenter,
? Alignment.centerRight AxisDirection.left => Alignment.centerLeft,
: Alignment.centerLeft; AxisDirection.right => Alignment.centerRight,
case AxisDirection.down: };
return stretchDirection == _StretchDirection.trailing
? AlignmentDirectional.bottomCenter
: AlignmentDirectional.topCenter;
case AxisDirection.left:
return stretchDirection == _StretchDirection.trailing
? Alignment.centerLeft
: Alignment.centerRight;
}
} }
@override @override
......
...@@ -885,7 +885,6 @@ class ScrollableState extends State<Scrollable> with TickerProviderStateMixin, R ...@@ -885,7 +885,6 @@ class ScrollableState extends State<Scrollable> with TickerProviderStateMixin, R
// direction, and any modifiers specified by the ScrollBehavior taken into // direction, and any modifiers specified by the ScrollBehavior taken into
// account. // account.
double _pointerSignalEventDelta(PointerScrollEvent event) { double _pointerSignalEventDelta(PointerScrollEvent event) {
late double delta;
final Set<LogicalKeyboardKey> pressed = HardwareKeyboard.instance.logicalKeysPressed; final Set<LogicalKeyboardKey> pressed = HardwareKeyboard.instance.logicalKeysPressed;
final bool flipAxes = pressed.any(_configuration.pointerAxisModifiers.contains) && final bool flipAxes = pressed.any(_configuration.pointerAxisModifiers.contains) &&
// Axes are only flipped for physical mouse wheel input. // Axes are only flipped for physical mouse wheel input.
...@@ -896,21 +895,13 @@ class ScrollableState extends State<Scrollable> with TickerProviderStateMixin, R ...@@ -896,21 +895,13 @@ class ScrollableState extends State<Scrollable> with TickerProviderStateMixin, R
// axis. // axis.
event.kind == PointerDeviceKind.mouse; event.kind == PointerDeviceKind.mouse;
switch (widget.axis) { final Axis axis = flipAxes ? flipAxis(widget.axis) : widget.axis;
case Axis.horizontal: final double delta = switch (axis) {
delta = flipAxes Axis.horizontal => event.scrollDelta.dx,
? event.scrollDelta.dy Axis.vertical => event.scrollDelta.dy,
: event.scrollDelta.dx; };
case Axis.vertical:
delta = flipAxes
? event.scrollDelta.dx
: event.scrollDelta.dy;
}
if (axisDirectionIsReversed(widget.axisDirection)) { return axisDirectionIsReversed(widget.axisDirection) ? -delta : delta;
delta *= -1;
}
return delta;
} }
void _receivedPointerSignal(PointerSignalEvent event) { void _receivedPointerSignal(PointerSignalEvent event) {
......
...@@ -426,50 +426,12 @@ class ScrollAction extends ContextAction<ScrollIntent> { ...@@ -426,50 +426,12 @@ class ScrollAction extends ContextAction<ScrollIntent> {
/// Find out how much of an increment to move by, taking the different /// Find out how much of an increment to move by, taking the different
/// directions into account. /// directions into account.
static double getDirectionalIncrement(ScrollableState state, ScrollIntent intent) { static double getDirectionalIncrement(ScrollableState state, ScrollIntent intent) {
if (axisDirectionToAxis(intent.direction) == axisDirectionToAxis(state.axisDirection)) {
final double increment = _calculateScrollIncrement(state, type: intent.type); final double increment = _calculateScrollIncrement(state, type: intent.type);
switch (intent.direction) { return intent.direction == state.axisDirection ? increment : -increment;
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; 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;
}
}
}
@override @override
void invoke(ScrollIntent intent, [BuildContext? context]) { void invoke(ScrollIntent intent, [BuildContext? context]) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment