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:
Nate
2024-03-13 11:16:17 -06:00
committed by GitHub
parent 017f33df6c
commit b9e537336f
12 changed files with 140 additions and 181 deletions

View File

@@ -55,24 +55,19 @@ 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) {
debugPrint('Changing color to red.');
setState(() {
_color = Colors.red;
});
return KeyEventResult.handled;
} else if (event.logicalKey == LogicalKeyboardKey.keyG) {
debugPrint('Changing color to green.');
setState(() {
_color = Colors.green;
});
return KeyEventResult.handled;
} else if (event.logicalKey == LogicalKeyboardKey.keyB) {
debugPrint('Changing color to blue.');
setState(() {
_color = Colors.blue;
});
return KeyEventResult.handled;
switch (event.logicalKey) {
case LogicalKeyboardKey.keyR:
debugPrint('Changing color to red.');
setState(() { _color = Colors.red; });
return KeyEventResult.handled;
case LogicalKeyboardKey.keyG:
debugPrint('Changing color to green.');
setState(() { _color = Colors.green; });
return KeyEventResult.handled;
case LogicalKeyboardKey.keyB:
debugPrint('Changing color to blue.');
setState(() { _color = Colors.blue; });
return KeyEventResult.handled;
}
}
return KeyEventResult.ignored;

View File

@@ -36,24 +36,19 @@ 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) {
debugPrint('Changing color to red.');
setState(() {
_color = Colors.red;
});
return KeyEventResult.handled;
} else if (event.logicalKey == LogicalKeyboardKey.keyG) {
debugPrint('Changing color to green.');
setState(() {
_color = Colors.green;
});
return KeyEventResult.handled;
} else if (event.logicalKey == LogicalKeyboardKey.keyB) {
debugPrint('Changing color to blue.');
setState(() {
_color = Colors.blue;
});
return KeyEventResult.handled;
switch (event.logicalKey) {
case LogicalKeyboardKey.keyR:
debugPrint('Changing color to red.');
setState(() { _color = Colors.red; });
return KeyEventResult.handled;
case LogicalKeyboardKey.keyG:
debugPrint('Changing color to green.');
setState(() { _color = Colors.green; });
return KeyEventResult.handled;
case LogicalKeyboardKey.keyB:
debugPrint('Changing color to blue.');
setState(() { _color = Colors.blue; });
return KeyEventResult.handled;
}
}
return KeyEventResult.ignored;