diff --git a/packages/flutter/lib/src/animation/animation_controller.dart b/packages/flutter/lib/src/animation/animation_controller.dart index 70b303ba98..eb3affccba 100644 --- a/packages/flutter/lib/src/animation/animation_controller.dart +++ b/packages/flutter/lib/src/animation/animation_controller.dart @@ -886,13 +886,11 @@ class _InterpolationSimulation extends Simulation { @override double x(double timeInSeconds) { final double t = clampDouble(timeInSeconds / _durationInSeconds, 0.0, 1.0); - if (t == 0.0) { - return _begin; - } else if (t == 1.0) { - return _end; - } else { - return _begin + (_end - _begin) * _curve.transform(t); - } + return switch (t) { + 0.0 => _begin, + 1.0 => _end, + _ => _begin + (_end - _begin) * _curve.transform(t), + }; } @override diff --git a/packages/flutter/lib/src/cupertino/context_menu.dart b/packages/flutter/lib/src/cupertino/context_menu.dart index a9ddd25bc9..c3b4a10bd3 100644 --- a/packages/flutter/lib/src/cupertino/context_menu.dart +++ b/packages/flutter/lib/src/cupertino/context_menu.dart @@ -1298,15 +1298,14 @@ class _ContextMenuRouteStaticState extends State<_ContextMenuRouteStatic> with T child: AnimatedBuilder( animation: _moveController, builder: _buildAnimation, - child: widget.orientation == Orientation.portrait - ? Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: children, - ) - : Row( - crossAxisAlignment: CrossAxisAlignment.start, - children: children, - ), + child: Flex( + direction: switch (widget.orientation) { + Orientation.portrait => Axis.vertical, + Orientation.landscape => Axis.horizontal, + }, + crossAxisAlignment: CrossAxisAlignment.start, + children: children, + ), ), ), ), diff --git a/packages/flutter/lib/src/cupertino/list_tile.dart b/packages/flutter/lib/src/cupertino/list_tile.dart index 31f8bd77a0..4ba883f718 100644 --- a/packages/flutter/lib/src/cupertino/list_tile.dart +++ b/packages/flutter/lib/src/cupertino/list_tile.dart @@ -251,37 +251,20 @@ class _CupertinoListTileState extends State { @override Widget build(BuildContext context) { - final TextStyle titleTextStyle = - widget._type == _CupertinoListTileType.base || widget.subtitle == null - ? CupertinoTheme.of(context).textTheme.textStyle - : CupertinoTheme.of(context).textTheme.textStyle.merge( - TextStyle( - fontWeight: FontWeight.w600, - fontSize: widget.leading == null ? _kNotchedTitleWithSubtitleFontSize : null, - ), - ); - - final TextStyle subtitleTextStyle = widget._type == _CupertinoListTileType.base - ? CupertinoTheme.of(context).textTheme.textStyle.merge( - TextStyle( - fontSize: _kSubtitleFontSize, - color: CupertinoColors.secondaryLabel.resolveFrom(context), - ), - ) - : CupertinoTheme.of(context).textTheme.textStyle.merge( - TextStyle( - fontSize: _kNotchedSubtitleFontSize, - color: CupertinoColors.secondaryLabel.resolveFrom(context), - ), - ); - - final TextStyle? additionalInfoTextStyle = widget.additionalInfo != null - ? CupertinoTheme.of(context).textTheme.textStyle.merge( - TextStyle(color: CupertinoColors.secondaryLabel.resolveFrom(context))) - : null; + final TextStyle textStyle = CupertinoTheme.of(context).textTheme.textStyle; + final TextStyle coloredStyle = textStyle.copyWith( + color: CupertinoColors.secondaryLabel.resolveFrom(context), + ); + final bool baseType = switch (widget._type) { + _CupertinoListTileType.base => true, + _CupertinoListTileType.notched => false, + }; final Widget title = DefaultTextStyle( - style: titleTextStyle, + style: baseType || widget.subtitle == null ? textStyle : textStyle.copyWith( + fontWeight: FontWeight.w600, + fontSize: widget.leading == null ? _kNotchedTitleWithSubtitleFontSize : null, + ), maxLines: 1, overflow: TextOverflow.ellipsis, child: widget.title, @@ -294,25 +277,6 @@ class _CupertinoListTileState extends State { _CupertinoListTileType.notched => _kNotchedPaddingWithoutLeading, }; - Widget? subtitle; - if (widget.subtitle != null) { - subtitle = DefaultTextStyle( - style: subtitleTextStyle, - maxLines: 1, - overflow: TextOverflow.ellipsis, - child: widget.subtitle!, - ); - } - - Widget? additionalInfo; - if (widget.additionalInfo != null) { - additionalInfo = DefaultTextStyle( - style: additionalInfoTextStyle!, - maxLines: 1, - child: widget.additionalInfo!, - ); - } - // The color for default state tile is set to either what user provided or // null and it will resolve to the correct color provided by context. But if // the tile was tapped, it is set to what user provided or if null to the @@ -323,7 +287,7 @@ class _CupertinoListTileState extends State { } final double minHeight = switch (widget._type) { - _CupertinoListTileType.base when subtitle != null => _kMinHeightWithSubtitle, + _CupertinoListTileType.base when widget.subtitle != null => _kMinHeightWithSubtitle, _CupertinoListTileType.notched when widget.leading != null => _kNotchedMinHeight, _CupertinoListTileType.base => _kMinHeight, _CupertinoListTileType.notched => _kNotchedMinHeightWithoutLeading, @@ -336,13 +300,10 @@ class _CupertinoListTileState extends State { padding: padding, child: Row( children: [ - if (widget.leading != null) ...[ - SizedBox( - width: widget.leadingSize, - height: widget.leadingSize, - child: Center( - child: widget.leading, - ), + if (widget.leading case final Widget leading) ...[ + SizedBox.square( + dimension: widget.leadingSize, + child: Center(child: leading), ), SizedBox(width: widget.leadingToTitle), ] else @@ -353,15 +314,28 @@ class _CupertinoListTileState extends State { crossAxisAlignment: CrossAxisAlignment.start, children: [ title, - if (subtitle != null) ...[ + if (widget.subtitle case final Widget subtitle) ...[ const SizedBox(height: _kNotchedTitleToSubtitle), - subtitle, + DefaultTextStyle( + style: coloredStyle.copyWith( + fontSize: baseType + ? _kSubtitleFontSize + : _kNotchedSubtitleFontSize, + ), + maxLines: 1, + overflow: TextOverflow.ellipsis, + child: subtitle, + ), ], ], ), ), - if (additionalInfo != null) ...[ - additionalInfo, + if (widget.additionalInfo case final Widget additionalInfo) ...[ + DefaultTextStyle( + style: coloredStyle, + maxLines: 1, + child: additionalInfo, + ), if (widget.trailing != null) const SizedBox(width: _kAdditionalInfoToTrailing), ], diff --git a/packages/flutter/lib/src/cupertino/route.dart b/packages/flutter/lib/src/cupertino/route.dart index df7e8626cb..776957218c 100644 --- a/packages/flutter/lib/src/cupertino/route.dart +++ b/packages/flutter/lib/src/cupertino/route.dart @@ -718,17 +718,17 @@ class _CupertinoBackGestureDetectorState extends State<_CupertinoBackGestureD assert(debugCheckHasDirectionality(context)); // For devices with notches, the drag area needs to be larger on the side // that has the notch. - double dragAreaWidth = Directionality.of(context) == TextDirection.ltr ? - MediaQuery.paddingOf(context).left : - MediaQuery.paddingOf(context).right; - dragAreaWidth = max(dragAreaWidth, _kBackGestureWidth); + final double dragAreaWidth = switch (Directionality.of(context)) { + TextDirection.rtl => MediaQuery.paddingOf(context).right, + TextDirection.ltr => MediaQuery.paddingOf(context).left, + }; return Stack( fit: StackFit.passthrough, children: [ widget.child, PositionedDirectional( start: 0.0, - width: dragAreaWidth, + width: max(dragAreaWidth, _kBackGestureWidth), top: 0.0, bottom: 0.0, child: Listener( diff --git a/packages/flutter/lib/src/gestures/monodrag.dart b/packages/flutter/lib/src/gestures/monodrag.dart index d3f44152ff..b525618505 100644 --- a/packages/flutter/lib/src/gestures/monodrag.dart +++ b/packages/flutter/lib/src/gestures/monodrag.dart @@ -607,14 +607,12 @@ abstract class DragGestureRecognizer extends OneSequenceGestureRecognizer { event is PointerMoveEvent || event is PointerPanZoomStartEvent || event is PointerPanZoomUpdateEvent)) { - final VelocityTracker tracker = _velocityTrackers[event.pointer]!; - if (event is PointerPanZoomStartEvent) { - tracker.addPosition(event.timeStamp, Offset.zero); - } else if (event is PointerPanZoomUpdateEvent) { - tracker.addPosition(event.timeStamp, event.pan); - } else { - tracker.addPosition(event.timeStamp, event.localPosition); - } + final Offset position = switch (event) { + PointerPanZoomStartEvent() => Offset.zero, + PointerPanZoomUpdateEvent() => event.pan, + _ => event.localPosition, + }; + _velocityTrackers[event.pointer]!.addPosition(event.timeStamp, position); } if (event is PointerMoveEvent && event.buttons != _initialButtons) { _giveUpPointer(event.pointer); diff --git a/packages/flutter/lib/src/material/calendar_date_picker.dart b/packages/flutter/lib/src/material/calendar_date_picker.dart index bb9ab58564..cfbb07749a 100644 --- a/packages/flutter/lib/src/material/calendar_date_picker.dart +++ b/packages/flutter/lib/src/material/calendar_date_picker.dart @@ -325,10 +325,10 @@ class _CalendarDatePickerState extends State { _DatePickerModeToggleButton( mode: _mode, title: _localizations.formatMonthYear(_currentDisplayedMonthDate), - onTitlePressed: () { - // Toggle the day/year mode. - _handleModeChanged(_mode == DatePickerMode.day ? DatePickerMode.year : DatePickerMode.day); - }, + onTitlePressed: () => _handleModeChanged(switch (_mode) { + DatePickerMode.day => DatePickerMode.year, + DatePickerMode.year => DatePickerMode.day, + }), ), ], ); diff --git a/packages/flutter/lib/src/material/checkbox.dart b/packages/flutter/lib/src/material/checkbox.dart index 5d289aca6d..aa69c34a2e 100644 --- a/packages/flutter/lib/src/material/checkbox.dart +++ b/packages/flutter/lib/src/material/checkbox.dart @@ -765,10 +765,10 @@ class _CheckboxPainter extends ToggleablePainter { final Paint strokePaint = _createStrokePaint(); final Offset origin = size / 2.0 - const Size.square(_kEdgeSize) / 2.0 as Offset; - final AnimationStatus status = position.status; - final double tNormalized = status == AnimationStatus.forward || status == AnimationStatus.completed - ? position.value - : 1.0 - position.value; + final double tNormalized = switch (position.status) { + AnimationStatus.forward || AnimationStatus.completed => position.value, + AnimationStatus.reverse || AnimationStatus.dismissed => 1.0 - position.value, + }; // Four cases: false to null, false to true, null to false, true to false if (previousValue == false || value == false) { diff --git a/packages/flutter/lib/src/material/date_picker.dart b/packages/flutter/lib/src/material/date_picker.dart index 77ea9d17f2..044262e645 100644 --- a/packages/flutter/lib/src/material/date_picker.dart +++ b/packages/flutter/lib/src/material/date_picker.dart @@ -2824,11 +2824,10 @@ class _InputDateRangePickerDialog extends StatelessWidget { if (start == null || end == null) { return localizations.unspecifiedDateRange; } - if (Directionality.of(context) == TextDirection.ltr) { - return '$startText – $endText'; - } else { - return '$endText – $startText'; - } + return switch (Directionality.of(context)) { + TextDirection.rtl => '$endText – $startText', + TextDirection.ltr => '$startText – $endText', + }; } @override diff --git a/packages/flutter/lib/src/material/icon_button.dart b/packages/flutter/lib/src/material/icon_button.dart index 30895a62fc..ad5b1650e8 100644 --- a/packages/flutter/lib/src/material/icon_button.dart +++ b/packages/flutter/lib/src/material/icon_button.dart @@ -970,16 +970,14 @@ class _IconButtonM3 extends ButtonStyleButton { @override ButtonStyle? themeStyleOf(BuildContext context) { final IconThemeData iconTheme = IconTheme.of(context); - final bool isDark = Theme.of(context).brightness == Brightness.dark; - - bool isIconThemeDefault(Color? color) { - if (isDark) { - return identical(color, kDefaultIconLightColor); - } - return identical(color, kDefaultIconDarkColor); - } - final bool isDefaultColor = isIconThemeDefault(iconTheme.color); final bool isDefaultSize = iconTheme.size == const IconThemeData.fallback().size; + final bool isDefaultColor = identical( + iconTheme.color, + switch (Theme.of(context).brightness) { + Brightness.light => kDefaultIconDarkColor, + Brightness.dark => kDefaultIconLightColor, + }, + ); final ButtonStyle iconThemeStyle = IconButton.styleFrom( foregroundColor: isDefaultColor ? null : iconTheme.color, diff --git a/packages/flutter/lib/src/material/ink_decoration.dart b/packages/flutter/lib/src/material/ink_decoration.dart index 05434e7f63..b392d7dc5f 100644 --- a/packages/flutter/lib/src/material/ink_decoration.dart +++ b/packages/flutter/lib/src/material/ink_decoration.dart @@ -236,14 +236,12 @@ class Ink extends StatefulWidget { final double? height; EdgeInsetsGeometry get _paddingIncludingDecoration { - if (decoration == null) { - return padding ?? EdgeInsets.zero; - } - final EdgeInsetsGeometry decorationPadding = decoration!.padding; - if (padding == null) { - return decorationPadding; - } - return padding!.add(decorationPadding); + return switch ((padding, decoration?.padding)) { + (null, null) => EdgeInsets.zero, + (null, final EdgeInsetsGeometry padding) => padding, + (final EdgeInsetsGeometry padding, null) => padding, + _ => padding!.add(decoration!.padding), + }; } @override diff --git a/packages/flutter/lib/src/material/input_decorator.dart b/packages/flutter/lib/src/material/input_decorator.dart index f50f579833..61473feaeb 100644 --- a/packages/flutter/lib/src/material/input_decorator.dart +++ b/packages/flutter/lib/src/material/input_decorator.dart @@ -287,13 +287,11 @@ class _Shaker extends AnimatedWidget { double get translateX { const double shakeDelta = 4.0; final double t = animation.value; - if (t <= 0.25) { - return -t * shakeDelta; - } else if (t < 0.75) { - return (t - 0.5) * shakeDelta; - } else { - return (1.0 - t) * 4.0 * shakeDelta; - } + return shakeDelta * switch (t) { + <= 0.25 => -t, + < 0.75 => t - 0.5, + _ => (1.0 - t) * 4.0, + }; } @override diff --git a/packages/flutter/lib/src/material/scrollbar.dart b/packages/flutter/lib/src/material/scrollbar.dart index 5f7393fb89..4a962d12f4 100644 --- a/packages/flutter/lib/src/material/scrollbar.dart +++ b/packages/flutter/lib/src/material/scrollbar.dart @@ -271,10 +271,10 @@ class _MaterialScrollbarState extends RawScrollbarState<_MaterialScrollbar> { final Brightness brightness = _colorScheme.brightness; return MaterialStateProperty.resolveWith((Set states) { if (showScrollbar && _trackVisibility.resolve(states)) { - return _scrollbarTheme.trackColor?.resolve(states) - ?? (brightness == Brightness.light - ? onSurface.withOpacity(0.03) - : onSurface.withOpacity(0.05)); + return _scrollbarTheme.trackColor?.resolve(states) ?? switch (brightness) { + Brightness.light => onSurface.withOpacity(0.03), + Brightness.dark => onSurface.withOpacity(0.05), + }; } return const Color(0x00000000); }); @@ -285,10 +285,10 @@ class _MaterialScrollbarState extends RawScrollbarState<_MaterialScrollbar> { final Brightness brightness = _colorScheme.brightness; return MaterialStateProperty.resolveWith((Set states) { if (showScrollbar && _trackVisibility.resolve(states)) { - return _scrollbarTheme.trackBorderColor?.resolve(states) - ?? (brightness == Brightness.light - ? onSurface.withOpacity(0.1) - : onSurface.withOpacity(0.25)); + return _scrollbarTheme.trackBorderColor?.resolve(states) ?? switch (brightness) { + Brightness.light => onSurface.withOpacity(0.1), + Brightness.dark => onSurface.withOpacity(0.25), + }; } return const Color(0x00000000); }); diff --git a/packages/flutter/lib/src/material/search_anchor.dart b/packages/flutter/lib/src/material/search_anchor.dart index cf7d6c07f1..84b4e1fc88 100644 --- a/packages/flutter/lib/src/material/search_anchor.dart +++ b/packages/flutter/lib/src/material/search_anchor.dart @@ -1323,7 +1323,6 @@ class _SearchBarState extends State { Widget build(BuildContext context) { final TextDirection textDirection = Directionality.of(context); final ColorScheme colorScheme = Theme.of(context).colorScheme; - final IconThemeData iconTheme = IconTheme.of(context); final SearchBarThemeData searchBarTheme = SearchBarTheme.of(context); final SearchBarThemeData defaults = _SearchBarDefaultsM3(context); @@ -1354,33 +1353,27 @@ class _SearchBarState extends State { ?? searchBarTheme.textStyle?.resolve(states) ?? defaults.hintStyle?.resolve(states); - final bool isDark = Theme.of(context).brightness == Brightness.dark; - bool isIconThemeColorDefault(Color? color) { - if (isDark) { - return color == kDefaultIconLightColor; - } - return color == kDefaultIconDarkColor; - } + final Color defaultColor = switch (colorScheme.brightness) { + Brightness.light => kDefaultIconDarkColor, + Brightness.dark => kDefaultIconLightColor, + }; + final IconThemeData? customTheme = switch (IconTheme.of(context)) { + final IconThemeData iconTheme when iconTheme.color != defaultColor => iconTheme, + _ => null, + }; Widget? leading; if (widget.leading != null) { leading = IconTheme.merge( - data: isIconThemeColorDefault(iconTheme.color) - ? IconThemeData(color: colorScheme.onSurface) - : iconTheme, + data: customTheme ?? IconThemeData(color: colorScheme.onSurface), child: widget.leading!, ); } - List? trailing; - if (widget.trailing != null) { - trailing = widget.trailing?.map((Widget trailing) => IconTheme.merge( - data: isIconThemeColorDefault(iconTheme.color) - ? IconThemeData(color: colorScheme.onSurfaceVariant) - : iconTheme, - child: trailing, - )).toList(); - } + final List? trailing = widget.trailing?.map((Widget trailing) => IconTheme.merge( + data: customTheme ?? IconThemeData(color: colorScheme.onSurfaceVariant), + child: trailing, + )).toList(); return ConstrainedBox( constraints: widget.constraints ?? searchBarTheme.constraints ?? defaults.constraints!, @@ -1444,7 +1437,7 @@ class _SearchBarState extends State { ), ), ), - if (trailing != null) ...trailing, + ...?trailing, ], ), ), diff --git a/packages/flutter/lib/src/painting/matrix_utils.dart b/packages/flutter/lib/src/painting/matrix_utils.dart index b2d0018de6..c783f4ef47 100644 --- a/packages/flutter/lib/src/painting/matrix_utils.dart +++ b/packages/flutter/lib/src/painting/matrix_utils.dart @@ -504,11 +504,10 @@ abstract final class MatrixUtils { // Model matrix by first translating the object from the origin of the world // by radius in the z axis and then rotating against the world. - result = result * (( - orientation == Axis.horizontal - ? Matrix4.rotationY(angle) - : Matrix4.rotationX(angle) - ) * Matrix4.translationValues(0.0, 0.0, radius)) as Matrix4; + result = result * (switch (orientation) { + Axis.horizontal => Matrix4.rotationY(angle), + Axis.vertical => Matrix4.rotationX(angle), + } * Matrix4.translationValues(0.0, 0.0, radius)) as Matrix4; // Essentially perspective * view * model. return result; diff --git a/packages/flutter/lib/src/painting/shape_decoration.dart b/packages/flutter/lib/src/painting/shape_decoration.dart index a19f502d15..2e2adf25da 100644 --- a/packages/flutter/lib/src/painting/shape_decoration.dart +++ b/packages/flutter/lib/src/painting/shape_decoration.dart @@ -184,22 +184,20 @@ class ShapeDecoration extends Decoration { @override ShapeDecoration? lerpFrom(Decoration? a, double t) { - if (a is BoxDecoration) { - return ShapeDecoration.lerp(ShapeDecoration.fromBoxDecoration(a), this, t); - } else if (a == null || a is ShapeDecoration) { - return ShapeDecoration.lerp(a as ShapeDecoration?, this, t); - } - return super.lerpFrom(a, t) as ShapeDecoration?; + return switch (a) { + BoxDecoration() => ShapeDecoration.lerp(ShapeDecoration.fromBoxDecoration(a), this, t), + ShapeDecoration? _ => ShapeDecoration.lerp(a, this, t), + _ => super.lerpFrom(a, t) as ShapeDecoration?, + }; } @override ShapeDecoration? lerpTo(Decoration? b, double t) { - if (b is BoxDecoration) { - return ShapeDecoration.lerp(this, ShapeDecoration.fromBoxDecoration(b), t); - } else if (b == null || b is ShapeDecoration) { - return ShapeDecoration.lerp(this, b as ShapeDecoration?, t); - } - return super.lerpTo(b, t) as ShapeDecoration?; + return switch (b) { + BoxDecoration() => ShapeDecoration.lerp(this, ShapeDecoration.fromBoxDecoration(b), t), + ShapeDecoration? _ => ShapeDecoration.lerp(this, b, t), + _ => super.lerpTo(b, t) as ShapeDecoration?, + }; } /// Linearly interpolate between two shapes. diff --git a/packages/flutter/lib/src/rendering/flex.dart b/packages/flutter/lib/src/rendering/flex.dart index 13625b673a..4726bd4f01 100644 --- a/packages/flutter/lib/src/rendering/flex.dart +++ b/packages/flutter/lib/src/rendering/flex.dart @@ -680,10 +680,10 @@ class RenderFlex extends RenderBox with ContainerRenderObjectMixin defaultComputeDistanceToHighestActualBaseline(baseline), + Axis.vertical => defaultComputeDistanceToFirstActualBaseline(baseline), + }; } static int _getFlex(RenderBox child) { diff --git a/packages/flutter/lib/src/rendering/proxy_box.dart b/packages/flutter/lib/src/rendering/proxy_box.dart index 9229a10733..df9b7b0d24 100644 --- a/packages/flutter/lib/src/rendering/proxy_box.dart +++ b/packages/flutter/lib/src/rendering/proxy_box.dart @@ -3497,26 +3497,21 @@ class RenderRepaintBoundary extends RenderProxyBox { bool inReleaseMode = true; assert(() { inReleaseMode = false; - if (debugSymmetricPaintCount + debugAsymmetricPaintCount == 0) { + final int totalPaints = debugSymmetricPaintCount + debugAsymmetricPaintCount; + if (totalPaints == 0) { properties.add(MessageProperty('usefulness ratio', 'no metrics collected yet (never painted)')); } else { - final double fraction = debugAsymmetricPaintCount / (debugSymmetricPaintCount + debugAsymmetricPaintCount); - final String diagnosis; - if (debugSymmetricPaintCount + debugAsymmetricPaintCount < 5) { - diagnosis = 'insufficient data to draw conclusion (less than five repaints)'; - } else if (fraction > 0.9) { - diagnosis = 'this is an outstandingly useful repaint boundary and should definitely be kept'; - } else if (fraction > 0.5) { - diagnosis = 'this is a useful repaint boundary and should be kept'; - } else if (fraction > 0.30) { - diagnosis = 'this repaint boundary is probably useful, but maybe it would be more useful in tandem with adding more repaint boundaries elsewhere'; - } else if (fraction > 0.1) { - diagnosis = 'this repaint boundary does sometimes show value, though currently not that often'; - } else if (debugAsymmetricPaintCount == 0) { - diagnosis = 'this repaint boundary is astoundingly ineffectual and should be removed'; - } else { - diagnosis = 'this repaint boundary is not very effective and should probably be removed'; - } + final double fraction = debugAsymmetricPaintCount / totalPaints; + final String diagnosis = switch (fraction) { + _ when totalPaints < 5 => 'insufficient data to draw conclusion (less than five repaints)', + > 0.9 => 'this is an outstandingly useful repaint boundary and should definitely be kept', + > 0.5 => 'this is a useful repaint boundary and should be kept', + > 0.3 => 'this repaint boundary is probably useful, but maybe it would be more useful in tandem with adding more repaint boundaries elsewhere', + > 0.1 => 'this repaint boundary does sometimes show value, though currently not that often', + _ when debugAsymmetricPaintCount > 0 => 'this repaint boundary is not very effective and should probably be removed', + _ => 'this repaint boundary is astoundingly ineffectual and should be removed', + }; + properties.add(PercentProperty('metrics', fraction, unit: 'useful', tooltip: '$debugSymmetricPaintCount bad vs $debugAsymmetricPaintCount good')); properties.add(MessageProperty('diagnosis', diagnosis)); } diff --git a/packages/flutter/lib/src/widgets/basic.dart b/packages/flutter/lib/src/widgets/basic.dart index 0e8dd68331..43c10386bb 100644 --- a/packages/flutter/lib/src/widgets/basic.dart +++ b/packages/flutter/lib/src/widgets/basic.dart @@ -2433,14 +2433,11 @@ class SizedBox extends SingleChildRenderObjectWidget { @override String toStringShort() { - final String type; - if (width == double.infinity && height == double.infinity) { - type = '${objectRuntimeType(this, 'SizedBox')}.expand'; - } else if (width == 0.0 && height == 0.0) { - type = '${objectRuntimeType(this, 'SizedBox')}.shrink'; - } else { - type = objectRuntimeType(this, 'SizedBox'); - } + final String type = switch ((width, height)) { + (double.infinity, double.infinity) => '${objectRuntimeType(this, 'SizedBox')}.expand', + (0.0, 0.0) => '${objectRuntimeType(this, 'SizedBox')}.shrink', + _ => objectRuntimeType(this, 'SizedBox'), + }; return key == null ? type : '$type-$key'; } diff --git a/packages/flutter/lib/src/widgets/container.dart b/packages/flutter/lib/src/widgets/container.dart index efaf249c57..9635ece97a 100644 --- a/packages/flutter/lib/src/widgets/container.dart +++ b/packages/flutter/lib/src/widgets/container.dart @@ -362,14 +362,11 @@ class Container extends StatelessWidget { final Clip clipBehavior; EdgeInsetsGeometry? get _paddingIncludingDecoration { - if (decoration == null) { - return padding; - } - final EdgeInsetsGeometry decorationPadding = decoration!.padding; - if (padding == null) { - return decorationPadding; - } - return padding!.add(decorationPadding); + return switch ((padding, decoration?.padding)) { + (null, final EdgeInsetsGeometry? padding) => padding, + (final EdgeInsetsGeometry? padding, null) => padding, + (_) => padding!.add(decoration!.padding), + }; } @override diff --git a/packages/flutter/lib/src/widgets/magnifier.dart b/packages/flutter/lib/src/widgets/magnifier.dart index 463f017b1a..485e506834 100644 --- a/packages/flutter/lib/src/widgets/magnifier.dart +++ b/packages/flutter/lib/src/widgets/magnifier.dart @@ -217,16 +217,10 @@ class MagnifierController { /// [AnimationStatus.completed] state, or in the [AnimationStatus.forward] /// state. bool get shown { - if (overlayEntry == null) { - return false; - } - - if (animationController != null) { - return animationController!.status == AnimationStatus.completed || - animationController!.status == AnimationStatus.forward; - } - - return true; + return overlayEntry != null && switch (animationController?.status) { + AnimationStatus.forward || AnimationStatus.completed || null => true, + AnimationStatus.reverse || AnimationStatus.dismissed => false, + }; } /// Displays the magnifier. diff --git a/packages/flutter/lib/src/widgets/overscroll_indicator.dart b/packages/flutter/lib/src/widgets/overscroll_indicator.dart index 00b0767af5..d3aac5fe95 100644 --- a/packages/flutter/lib/src/widgets/overscroll_indicator.dart +++ b/packages/flutter/lib/src/widgets/overscroll_indicator.dart @@ -152,16 +152,12 @@ class GlowingOverscrollIndicator extends StatefulWidget { void debugFillProperties(DiagnosticPropertiesBuilder properties) { super.debugFillProperties(properties); properties.add(EnumProperty('axisDirection', axisDirection)); - final String showDescription; - if (showLeading && showTrailing) { - showDescription = 'both sides'; - } else if (showLeading) { - showDescription = 'leading side only'; - } else if (showTrailing) { - showDescription = 'trailing side only'; - } else { - showDescription = 'neither side (!)'; - } + final String showDescription = switch ((showLeading, showTrailing)) { + (true, true) => 'both sides', + (true, false) => 'leading side only', + (false, true) => 'trailing side only', + (false, false) => 'neither side (!)', + }; properties.add(MessageProperty('show', showDescription)); properties.add(ColorProperty('color', color, showName: false)); } diff --git a/packages/flutter/lib/src/widgets/router.dart b/packages/flutter/lib/src/widgets/router.dart index ebb64d2ff6..49212d688f 100644 --- a/packages/flutter/lib/src/widgets/router.dart +++ b/packages/flutter/lib/src/widgets/router.dart @@ -1476,15 +1476,15 @@ class PlatformRouteInformationProvider extends RouteInformationProvider with Wid @override void routerReportsNewRouteInformation(RouteInformation routeInformation, {RouteInformationReportingType type = RouteInformationReportingType.none}) { - final bool replace = - type == RouteInformationReportingType.neglect || - (type == RouteInformationReportingType.none && - _equals(_valueInEngine.uri, routeInformation.uri)); SystemNavigator.selectMultiEntryHistory(); SystemNavigator.routeInformationUpdated( uri: routeInformation.uri, state: routeInformation.state, - replace: replace, + replace: switch (type) { + RouteInformationReportingType.neglect => true, + RouteInformationReportingType.navigate => false, + RouteInformationReportingType.none => _equals(_valueInEngine.uri, routeInformation.uri), + }, ); _value = routeInformation; _valueInEngine = routeInformation; diff --git a/packages/flutter/lib/src/widgets/text_selection.dart b/packages/flutter/lib/src/widgets/text_selection.dart index c0cf9d7d38..345f676db5 100644 --- a/packages/flutter/lib/src/widgets/text_selection.dart +++ b/packages/flutter/lib/src/widgets/text_selection.dart @@ -2906,12 +2906,10 @@ class TextSelectionGestureDetectorBuilder { final Offset editableOffset = renderEditable.maxLines == 1 ? Offset(renderEditable.offset.pixels - _dragStartViewportOffset, 0.0) : Offset(0.0, renderEditable.offset.pixels - _dragStartViewportOffset); - final double effectiveScrollPosition = _scrollPosition - _dragStartScrollOffset; - final bool scrollingOnVerticalAxis = _scrollDirection == AxisDirection.up || _scrollDirection == AxisDirection.down; - final Offset scrollableOffset = Offset( - !scrollingOnVerticalAxis ? effectiveScrollPosition : 0.0, - scrollingOnVerticalAxis ? effectiveScrollPosition : 0.0, - ); + final Offset scrollableOffset = switch (axisDirectionToAxis(_scrollDirection ?? AxisDirection.left)) { + Axis.horizontal => Offset(_scrollPosition - _dragStartScrollOffset, 0.0), + Axis.vertical => Offset(0.0, _scrollPosition - _dragStartScrollOffset), + }; final Offset dragStartGlobalPosition = details.globalPosition - details.offsetFromOrigin; // Select word by word. diff --git a/packages/flutter/lib/src/widgets/ticker_provider.dart b/packages/flutter/lib/src/widgets/ticker_provider.dart index 270e7b66a7..5985561fb6 100644 --- a/packages/flutter/lib/src/widgets/ticker_provider.dart +++ b/packages/flutter/lib/src/widgets/ticker_provider.dart @@ -255,18 +255,13 @@ mixin SingleTickerProviderStateMixin on State imple @override void debugFillProperties(DiagnosticPropertiesBuilder properties) { super.debugFillProperties(properties); - String? tickerDescription; - if (_ticker != null) { - if (_ticker!.isActive && _ticker!.muted) { - tickerDescription = 'active but muted'; - } else if (_ticker!.isActive) { - tickerDescription = 'active'; - } else if (_ticker!.muted) { - tickerDescription = 'inactive and muted'; - } else { - tickerDescription = 'inactive'; - } - } + final String? tickerDescription = switch ((_ticker?.isActive, _ticker?.muted)) { + (true, true) => 'active but muted', + (true, _) => 'active', + (false, true) => 'inactive and muted', + (false, _) => 'inactive', + (null, _) => null, + }; properties.add(DiagnosticsProperty('ticker', _ticker, description: tickerDescription, showSeparator: false, defaultValue: null)); } } diff --git a/packages/flutter/lib/src/widgets/widget_inspector.dart b/packages/flutter/lib/src/widgets/widget_inspector.dart index e04c121e7c..356b71c369 100644 --- a/packages/flutter/lib/src/widgets/widget_inspector.dart +++ b/packages/flutter/lib/src/widgets/widget_inspector.dart @@ -1669,30 +1669,26 @@ mixin WidgetInspectorService { List _getParentChain(String? id, String groupName) { final Object? value = toObject(id); - List<_DiagnosticsPathNode> path; - if (value is RenderObject) { - path = _getRenderObjectParentChain(value, groupName)!; - } else if (value is Element) { - path = _getElementParentChain(value, groupName); - } else { - throw FlutterError.fromParts([ErrorSummary('Cannot get parent chain for node of type ${value.runtimeType}')]); - } - - return path.map((_DiagnosticsPathNode node) => _pathNodeToJson( - node, - InspectorSerializationDelegate(groupName: groupName, service: this), - )).toList(); - } - - Map? _pathNodeToJson(_DiagnosticsPathNode? pathNode, InspectorSerializationDelegate delegate) { - if (pathNode == null) { - return null; - } - return { - 'node': _nodeToJson(pathNode.node, delegate), - 'children': _nodesToJson(pathNode.children, delegate, parent: pathNode.node), - 'childIndex': pathNode.childIndex, + final List<_DiagnosticsPathNode> path = switch (value) { + RenderObject() => _getRenderObjectParentChain(value, groupName)!, + Element() => _getElementParentChain(value, groupName), + _ => throw FlutterError.fromParts([ + ErrorSummary('Cannot get parent chain for node of type ${value.runtimeType}'), + ]), }; + + InspectorSerializationDelegate createDelegate() => + InspectorSerializationDelegate(groupName: groupName, service: this); + + return [ + for (final _DiagnosticsPathNode pathNode in path) + if (createDelegate() case final InspectorSerializationDelegate delegate) + { + 'node': _nodeToJson(pathNode.node, delegate), + 'children': _nodesToJson(pathNode.children, delegate, parent: pathNode.node), + 'childIndex': pathNode.childIndex, + }, + ]; } List _getRawElementParentChain(Element element, { required int? numLocalParents }) {