mirror of
https://github.com/flutter/flutter
synced 2024-08-24 18:36:03 +00:00
Turning if
chains into shorter switch
statements (#144977)
This pull request is part of the effort to solve issue #144903. In the past, my efforts to reduce line length involved refactoring away from switch statements, but unlike [yesterday's PR](https://github.com/flutter/flutter/pull/144905), this one is full of switch statements that make things more concise!
This commit is contained in:
parent
017f33df6c
commit
b9e537336f
|
@ -259,18 +259,13 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||
|
||||
@override
|
||||
void initState() {
|
||||
if (widget.setup == Setup.canvasSaveLayer) {
|
||||
_loadImage().then((ui.Image? value) {
|
||||
setState(() {
|
||||
_image = value;
|
||||
});
|
||||
});
|
||||
} else if (widget.setup == Setup.drawnImage) {
|
||||
_drawImage().then((ui.Image? value) {
|
||||
setState(() {
|
||||
_image = value;
|
||||
});
|
||||
});
|
||||
switch (widget.setup) {
|
||||
case Setup.canvasSaveLayer:
|
||||
_loadImage().then((ui.Image? value) => setState(() { _image = value; }));
|
||||
case Setup.drawnImage:
|
||||
_drawImage().then((ui.Image? value) => setState(() { _image = value; }));
|
||||
case Setup.image || Setup.blur || Setup.none:
|
||||
break;
|
||||
}
|
||||
super.initState();
|
||||
}
|
||||
|
|
|
@ -55,23 +55,18 @@ class _ColorfulButtonState extends State<ColorfulButton> {
|
|||
KeyEventResult _handleKeyPress(FocusNode node, KeyEvent event) {
|
||||
if (event is KeyDownEvent) {
|
||||
debugPrint('Focus node ${node.debugLabel} got key event: ${event.logicalKey}');
|
||||
if (event.logicalKey == LogicalKeyboardKey.keyR) {
|
||||
switch (event.logicalKey) {
|
||||
case LogicalKeyboardKey.keyR:
|
||||
debugPrint('Changing color to red.');
|
||||
setState(() {
|
||||
_color = Colors.red;
|
||||
});
|
||||
setState(() { _color = Colors.red; });
|
||||
return KeyEventResult.handled;
|
||||
} else if (event.logicalKey == LogicalKeyboardKey.keyG) {
|
||||
case LogicalKeyboardKey.keyG:
|
||||
debugPrint('Changing color to green.');
|
||||
setState(() {
|
||||
_color = Colors.green;
|
||||
});
|
||||
setState(() { _color = Colors.green; });
|
||||
return KeyEventResult.handled;
|
||||
} else if (event.logicalKey == LogicalKeyboardKey.keyB) {
|
||||
case LogicalKeyboardKey.keyB:
|
||||
debugPrint('Changing color to blue.');
|
||||
setState(() {
|
||||
_color = Colors.blue;
|
||||
});
|
||||
setState(() { _color = Colors.blue; });
|
||||
return KeyEventResult.handled;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,23 +36,18 @@ class _FocusExampleState extends State<FocusExample> {
|
|||
KeyEventResult _handleKeyPress(FocusNode node, KeyEvent event) {
|
||||
if (event is KeyDownEvent) {
|
||||
debugPrint('Focus node ${node.debugLabel} got key event: ${event.logicalKey}');
|
||||
if (event.logicalKey == LogicalKeyboardKey.keyR) {
|
||||
switch (event.logicalKey) {
|
||||
case LogicalKeyboardKey.keyR:
|
||||
debugPrint('Changing color to red.');
|
||||
setState(() {
|
||||
_color = Colors.red;
|
||||
});
|
||||
setState(() { _color = Colors.red; });
|
||||
return KeyEventResult.handled;
|
||||
} else if (event.logicalKey == LogicalKeyboardKey.keyG) {
|
||||
case LogicalKeyboardKey.keyG:
|
||||
debugPrint('Changing color to green.');
|
||||
setState(() {
|
||||
_color = Colors.green;
|
||||
});
|
||||
setState(() { _color = Colors.green; });
|
||||
return KeyEventResult.handled;
|
||||
} else if (event.logicalKey == LogicalKeyboardKey.keyB) {
|
||||
case LogicalKeyboardKey.keyB:
|
||||
debugPrint('Changing color to blue.');
|
||||
setState(() {
|
||||
_color = Colors.blue;
|
||||
});
|
||||
setState(() { _color = Colors.blue; });
|
||||
return KeyEventResult.handled;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -372,34 +372,31 @@ class CupertinoListSection extends StatelessWidget {
|
|||
height: dividerHeight,
|
||||
);
|
||||
|
||||
Widget? headerWidget;
|
||||
TextStyle style = CupertinoTheme.of(context).textTheme.textStyle;
|
||||
|
||||
Widget? headerWidget, footerWidget;
|
||||
switch (type) {
|
||||
case CupertinoListSectionType.base:
|
||||
style = style.merge(TextStyle(
|
||||
fontSize: 13.0,
|
||||
color: CupertinoDynamicColor.resolve(_kHeaderFooterColor, context),
|
||||
));
|
||||
if (header != null) {
|
||||
headerWidget = DefaultTextStyle(style: style, child: header!);
|
||||
}
|
||||
if (footer != null) {
|
||||
footerWidget = DefaultTextStyle(style: style, child: footer!);
|
||||
}
|
||||
case CupertinoListSectionType.insetGrouped:
|
||||
if (header != null) {
|
||||
headerWidget = DefaultTextStyle(
|
||||
style: CupertinoTheme.of(context).textTheme.textStyle.merge(
|
||||
type == CupertinoListSectionType.base
|
||||
? TextStyle(
|
||||
fontSize: 13.0,
|
||||
color: CupertinoDynamicColor.resolve(
|
||||
_kHeaderFooterColor, context))
|
||||
: const TextStyle(
|
||||
fontSize: 20.0, fontWeight: FontWeight.bold),
|
||||
),
|
||||
style: style.merge(const TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold)),
|
||||
child: header!,
|
||||
);
|
||||
}
|
||||
|
||||
Widget? footerWidget;
|
||||
if (footer != null) {
|
||||
footerWidget = DefaultTextStyle(
|
||||
style: type == CupertinoListSectionType.base
|
||||
? CupertinoTheme.of(context).textTheme.textStyle.merge(TextStyle(
|
||||
fontSize: 13.0,
|
||||
color: CupertinoDynamicColor.resolve(
|
||||
_kHeaderFooterColor, context),
|
||||
))
|
||||
: CupertinoTheme.of(context).textTheme.textStyle,
|
||||
child: footer!,
|
||||
);
|
||||
footerWidget = DefaultTextStyle(style: style, child: footer!);
|
||||
}
|
||||
}
|
||||
|
||||
Widget? decoratedChildrenGroup;
|
||||
|
|
|
@ -73,16 +73,16 @@ class _CombiningGestureArenaMember extends GestureArenaMember {
|
|||
if (_resolved) {
|
||||
return;
|
||||
}
|
||||
if (disposition == GestureDisposition.rejected) {
|
||||
switch (disposition) {
|
||||
case GestureDisposition.accepted:
|
||||
_winner ??= _owner.captain ?? member;
|
||||
_entry!.resolve(disposition);
|
||||
case GestureDisposition.rejected:
|
||||
_members.remove(member);
|
||||
member.rejectGesture(_pointer);
|
||||
if (_members.isEmpty) {
|
||||
_entry!.resolve(disposition);
|
||||
}
|
||||
} else {
|
||||
assert(disposition == GestureDisposition.accepted);
|
||||
_winner ??= _owner.captain ?? member;
|
||||
_entry!.resolve(disposition);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2754,25 +2754,20 @@ class _HighlightPainter extends CustomPainter {
|
|||
..color = color
|
||||
..style = PaintingStyle.fill;
|
||||
|
||||
final Rect rectLeft = Rect.fromLTWH(0, 0, size.width / 2, size.height);
|
||||
final Rect rectRight = Rect.fromLTWH(size.width / 2, 0, size.width / 2, size.height);
|
||||
final bool rtl = switch (textDirection) {
|
||||
TextDirection.rtl || null => true,
|
||||
TextDirection.ltr => false,
|
||||
};
|
||||
|
||||
switch (style) {
|
||||
case _HighlightPainterStyle.highlightTrailing:
|
||||
canvas.drawRect(
|
||||
textDirection == TextDirection.ltr ? rectRight : rectLeft,
|
||||
paint,
|
||||
);
|
||||
case _HighlightPainterStyle.highlightLeading when rtl:
|
||||
case _HighlightPainterStyle.highlightTrailing when !rtl:
|
||||
canvas.drawRect(Rect.fromLTWH(size.width / 2, 0, size.width / 2, size.height), paint);
|
||||
case _HighlightPainterStyle.highlightLeading:
|
||||
canvas.drawRect(
|
||||
textDirection == TextDirection.ltr ? rectLeft : rectRight,
|
||||
paint,
|
||||
);
|
||||
case _HighlightPainterStyle.highlightTrailing:
|
||||
canvas.drawRect(Rect.fromLTWH(0, 0, size.width / 2, size.height), paint);
|
||||
case _HighlightPainterStyle.highlightAll:
|
||||
canvas.drawRect(
|
||||
Rect.fromLTWH(0, 0, size.width, size.height),
|
||||
paint,
|
||||
);
|
||||
canvas.drawRect(Rect.fromLTWH(0, 0, size.width, size.height), paint);
|
||||
case _HighlightPainterStyle.none:
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -1281,17 +1281,13 @@ class _CurvedAnimationBuilderState extends State<_CurvedAnimationBuilder> {
|
|||
_animationDirection = status;
|
||||
});
|
||||
}
|
||||
|
||||
if (status == AnimationStatus.completed || status == AnimationStatus.dismissed) {
|
||||
setState(() {
|
||||
_preservedDirection = null;
|
||||
});
|
||||
}
|
||||
|
||||
if (_preservedDirection == null && (status == AnimationStatus.forward || status == AnimationStatus.reverse)) {
|
||||
setState(() {
|
||||
_preservedDirection = status;
|
||||
});
|
||||
switch (status) {
|
||||
case AnimationStatus.forward || AnimationStatus.reverse when _preservedDirection != null:
|
||||
break;
|
||||
case AnimationStatus.forward || AnimationStatus.reverse:
|
||||
setState(() { _preservedDirection = status; });
|
||||
case AnimationStatus.completed || AnimationStatus.dismissed:
|
||||
setState(() { _preservedDirection = null; });
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -59,27 +59,25 @@ Future<void> startTransitionBetween(
|
|||
}
|
||||
|
||||
CupertinoPageScaffold? scaffoldForNavBar(Widget? navBar) {
|
||||
if (navBar is CupertinoNavigationBar || navBar == null) {
|
||||
switch (navBar) {
|
||||
case CupertinoNavigationBar? _:
|
||||
return CupertinoPageScaffold(
|
||||
navigationBar: navBar as CupertinoNavigationBar? ?? const CupertinoNavigationBar(),
|
||||
navigationBar: navBar ?? const CupertinoNavigationBar(),
|
||||
child: const Placeholder(),
|
||||
);
|
||||
} else if (navBar is CupertinoSliverNavigationBar) {
|
||||
case CupertinoSliverNavigationBar():
|
||||
return CupertinoPageScaffold(
|
||||
child: CustomScrollView(
|
||||
slivers: <Widget>[
|
||||
child: CustomScrollView(slivers: <Widget>[
|
||||
navBar,
|
||||
// Add filler so it's scrollable.
|
||||
const SliverToBoxAdapter(
|
||||
child: Placeholder(fallbackHeight: 1000.0),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SliverToBoxAdapter(child: Placeholder(fallbackHeight: 1000.0)),
|
||||
]),
|
||||
);
|
||||
}
|
||||
default:
|
||||
assert(false, 'Unexpected nav bar type ${navBar.runtimeType}');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Finder flying(WidgetTester tester, Finder finder) {
|
||||
final ContainerRenderObjectMixin<RenderBox, StackParentData> theater = tester.renderObject(find.byType(Overlay));
|
||||
|
|
|
@ -240,17 +240,13 @@ void main() {
|
|||
painter.paint(testCanvas, size);
|
||||
final Rect rect = captureRect();
|
||||
|
||||
switch (direction) {
|
||||
case AxisDirection.up:
|
||||
case AxisDirection.down:
|
||||
expect(
|
||||
margin,
|
||||
textDirection == TextDirection.ltr
|
||||
? size.width - rect.right
|
||||
: rect.left,
|
||||
);
|
||||
case AxisDirection.left:
|
||||
case AxisDirection.right:
|
||||
switch (axisDirectionToAxis(direction)) {
|
||||
case Axis.vertical:
|
||||
expect(margin, switch (textDirection) {
|
||||
TextDirection.ltr => size.width - rect.right,
|
||||
TextDirection.rtl => rect.left,
|
||||
});
|
||||
case Axis.horizontal:
|
||||
expect(margin, size.height - rect.bottom);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -520,25 +520,20 @@ class _MismatchedCall extends Error {
|
|||
}
|
||||
|
||||
bool _evaluatePainter(Object? object, Canvas canvas, PaintingContext context) {
|
||||
if (object is _ContextPainterFunction) {
|
||||
final _ContextPainterFunction function = object;
|
||||
switch (object) {
|
||||
case final _ContextPainterFunction function:
|
||||
function(context, Offset.zero);
|
||||
} else if (object is _CanvasPainterFunction) {
|
||||
final _CanvasPainterFunction function = object;
|
||||
case final _CanvasPainterFunction function:
|
||||
function(canvas);
|
||||
} else {
|
||||
if (object is Finder) {
|
||||
case final Finder finder:
|
||||
TestAsyncUtils.guardSync();
|
||||
final Finder finder = object;
|
||||
object = finder.evaluate().single.renderObject;
|
||||
}
|
||||
if (object is RenderObject) {
|
||||
final RenderObject renderObject = object;
|
||||
final RenderObject? result = finder.evaluate().single.renderObject;
|
||||
return (result?..paint(context, Offset.zero)) != null;
|
||||
case final RenderObject renderObject:
|
||||
renderObject.paint(context, Offset.zero);
|
||||
} else {
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -133,14 +133,12 @@ class StdoutHandler {
|
|||
compilerOutput?.complete(output);
|
||||
return;
|
||||
}
|
||||
if (state == StdoutState.CollectDiagnostic) {
|
||||
if (!_suppressCompilerMessages) {
|
||||
_logger.printError(message);
|
||||
} else {
|
||||
switch (state) {
|
||||
case StdoutState.CollectDiagnostic when _suppressCompilerMessages:
|
||||
_logger.printTrace(message);
|
||||
}
|
||||
} else {
|
||||
assert(state == StdoutState.CollectDependencies);
|
||||
case StdoutState.CollectDiagnostic:
|
||||
_logger.printError(message);
|
||||
case StdoutState.CollectDependencies:
|
||||
switch (message[0]) {
|
||||
case '+':
|
||||
sources.add(Uri.parse(message.substring(1)));
|
||||
|
|
|
@ -101,16 +101,15 @@ class DaemonInputStreamConverter {
|
|||
|
||||
int start = 0;
|
||||
while (start < chunk.length) {
|
||||
if (state == _InputStreamParseState.json) {
|
||||
switch (state) {
|
||||
case _InputStreamParseState.json:
|
||||
start += _processChunkInJsonMode(chunk, start);
|
||||
} else if (state == _InputStreamParseState.binary) {
|
||||
case _InputStreamParseState.binary:
|
||||
final int bytesSent = _addBinaryChunk(chunk, start, remainingBinaryLength);
|
||||
start += bytesSent;
|
||||
remainingBinaryLength -= bytesSent;
|
||||
|
||||
if (remainingBinaryLength <= 0) {
|
||||
assert(remainingBinaryLength == 0);
|
||||
|
||||
unawaited(currentBinaryStream.close());
|
||||
state = _InputStreamParseState.json;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue