-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from erickzanardo/feat/key_button
feat: adding nes key icon
- Loading branch information
Showing
10 changed files
with
182 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:nes_ui/nes_ui.dart'; | ||
import 'package:phased/phased.dart'; | ||
|
||
class KeyIconsSection extends StatelessWidget { | ||
const KeyIconsSection({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final theme = Theme.of(context); | ||
return Column( | ||
crossAxisAlignment: CrossAxisAlignment.start, | ||
children: [ | ||
Text( | ||
'Key Icons', | ||
style: theme.textTheme.displayMedium, | ||
), | ||
const SizedBox(height: 16), | ||
Wrap( | ||
children: [ | ||
const NesKeyIcon( | ||
buttonKey: 'a', | ||
), | ||
const SizedBox(width: 16), | ||
const NesKeyIcon( | ||
buttonKey: 'a', | ||
pressed: true, | ||
), | ||
const SizedBox(width: 16), | ||
_Toogler(), | ||
], | ||
), | ||
], | ||
); | ||
} | ||
} | ||
|
||
class _Toogler extends Phased<bool> { | ||
_Toogler() | ||
: super( | ||
state: PhasedState<bool>( | ||
values: [true, false], | ||
ticker: const Duration(milliseconds: 500), | ||
), | ||
); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return NesKeyIcon( | ||
buttonKey: 'a', | ||
pressed: state.value, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:nes_ui/nes_ui.dart'; | ||
|
||
/// {@template nes_key_icon} | ||
/// A pressable icon, representing a single key. | ||
/// | ||
/// This widget by itself doesn't handle touch/key events | ||
/// use it in combination with other widgets to make it | ||
/// interactive. | ||
/// {@endtemplate} | ||
class NesKeyIcon extends StatelessWidget { | ||
/// {@macro nes_key_icon} | ||
const NesKeyIcon({ | ||
super.key, | ||
required this.buttonKey, | ||
this.pressed = false, | ||
this.size, | ||
}) : assert(buttonKey.length == 1, 'buttonKey must be a single character'); | ||
|
||
/// The key that this icon represents. | ||
final String buttonKey; | ||
|
||
/// If this button is in a pressed state or not. | ||
final bool pressed; | ||
|
||
/// Size. | ||
final Size? size; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final nesTheme = context.nesThemeExtension<NesTheme>(); | ||
final nesIconTheme = context.nesThemeExtension<NesIconTheme>(); | ||
|
||
final pixelSize = nesTheme.pixelSize.toDouble(); | ||
|
||
final iconData = pressed | ||
? NesIcons.instance.pressedButton | ||
: NesIcons.instance.unpressedButton; | ||
|
||
final buttonSize = size ?? | ||
Size( | ||
iconData.sprite.pixels[0].length * pixelSize, | ||
iconData.sprite.pixels.length * pixelSize, | ||
); | ||
|
||
return Stack( | ||
children: [ | ||
Positioned( | ||
child: NesIcon( | ||
size: buttonSize, | ||
iconData: iconData, | ||
), | ||
), | ||
Positioned( | ||
left: buttonSize.width / 2 - 5, | ||
top: buttonSize.height / 2 - (pressed ? 4 : 8), | ||
child: Text( | ||
buttonKey.toUpperCase(), | ||
style: Theme.of(context).textTheme.bodySmall?.copyWith( | ||
color: nesIconTheme.secondary, | ||
), | ||
), | ||
), | ||
], | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:nes_ui/nes_ui.dart'; | ||
|
||
void main() { | ||
group('NesIcon', () { | ||
testWidgets('renders correctly', (tester) async { | ||
await tester.pumpWidget( | ||
MaterialApp( | ||
theme: flutterNesTheme(), | ||
home: const Scaffold( | ||
body: NesKeyIcon( | ||
buttonKey: 'A', | ||
), | ||
), | ||
), | ||
); | ||
|
||
expect(find.byType(NesKeyIcon), findsOneWidget); | ||
}); | ||
|
||
testWidgets('renders pressed correctly', (tester) async { | ||
await tester.pumpWidget( | ||
MaterialApp( | ||
theme: flutterNesTheme(), | ||
home: const Scaffold( | ||
body: NesKeyIcon( | ||
buttonKey: 'A', | ||
pressed: true, | ||
), | ||
), | ||
), | ||
); | ||
|
||
expect(find.byType(NesKeyIcon), findsOneWidget); | ||
}); | ||
}); | ||
} |