From 23e86edca72a04a22af918f4ea75dbeb5048271c Mon Sep 17 00:00:00 2001 From: arturplaczek Date: Thu, 28 Apr 2022 16:56:15 +0200 Subject: [PATCH] chore: create PinballButton --- lib/game/view/widgets/pinball_button.dart | 45 +++++++++++++++++++ .../view/widgets/pinball_button_test.dart | 39 ++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 lib/game/view/widgets/pinball_button.dart create mode 100644 test/game/view/widgets/pinball_button_test.dart diff --git a/lib/game/view/widgets/pinball_button.dart b/lib/game/view/widgets/pinball_button.dart new file mode 100644 index 00000000..fc739696 --- /dev/null +++ b/lib/game/view/widgets/pinball_button.dart @@ -0,0 +1,45 @@ +import 'package:flutter/material.dart'; +import 'package:pinball/gen/gen.dart'; + +/// {@template pinball_button} +/// Pinball button with onPressed [VoidCallback] and child [Widget]. +/// {@endtemplate} +class PinballButton extends StatelessWidget { + /// {@macro pinball_button} + const PinballButton({ + Key? key, + required this.child, + this.onPressed, + }) : super(key: key); + + /// Child displayed on button. + final Widget child; + + /// On pressed callback used for user integration. + final VoidCallback? onPressed; + + @override + Widget build(BuildContext context) { + return InkWell( + onTap: onPressed, + child: DecoratedBox( + decoration: BoxDecoration( + image: DecorationImage( + image: AssetImage( + Assets.images.selectCharacter.pinballButton.keyName, + ), + ), + ), + child: Center( + child: Padding( + padding: const EdgeInsets.symmetric( + horizontal: 32, + vertical: 16, + ), + child: child, + ), + ), + ), + ); + } +} diff --git a/test/game/view/widgets/pinball_button_test.dart b/test/game/view/widgets/pinball_button_test.dart new file mode 100644 index 00000000..50566dde --- /dev/null +++ b/test/game/view/widgets/pinball_button_test.dart @@ -0,0 +1,39 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pinball/game/game.dart'; + +import '../../../helpers/helpers.dart'; + +void main() { + const buttonText = 'this is the button text'; + + testWidgets('displays button', (tester) async { + await tester.pumpApp( + const Material( + child: PinballButton( + child: Text(buttonText), + ), + ), + ); + + expect(find.text(buttonText), findsOneWidget); + }); + + testWidgets('on tap calls onPressed callback', (tester) async { + var isTapped = false; + + await tester.pumpApp( + Material( + child: PinballButton( + child: const Text(buttonText), + onPressed: () { + isTapped = true; + }, + ), + ), + ); + await tester.tap(find.text(buttonText)); + + expect(isTapped, isTrue); + }); +}