From 9e5d6dccb8a6760831277e54e9c0701341525674 Mon Sep 17 00:00:00 2001 From: Jonathan Daniels Date: Fri, 29 Apr 2022 09:35:27 -0700 Subject: [PATCH] chore: moved timer logic to how to play dialog --- .../view/character_selection_page.dart | 31 +++++-------------- .../widgets/how_to_play_dialog.dart | 26 +++++++++++++++- .../view/character_selection_page_test.dart | 16 +++++++++- 3 files changed, 47 insertions(+), 26 deletions(-) diff --git a/lib/select_character/view/character_selection_page.dart b/lib/select_character/view/character_selection_page.dart index 8e652f72..9392c515 100644 --- a/lib/select_character/view/character_selection_page.dart +++ b/lib/select_character/view/character_selection_page.dart @@ -1,7 +1,5 @@ // ignore_for_file: public_member_api_docs -import 'dart:async'; - import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:pinball/l10n/l10n.dart'; @@ -48,34 +46,19 @@ class CharacterSelectionView extends StatelessWidget { const SizedBox(height: 20), TextButton( onPressed: () { - late Timer timer; // TODO(arturplaczek): remove after merge StarBlocListener final height = MediaQuery.of(context).size.height * 0.5; Navigator.of(context).pop(); showDialog( context: context, - builder: (context) { - timer = Timer( - const Duration(seconds: 3), - () { - Navigator.of(context).pop(); - }, - ); - return Center( - child: SizedBox( - height: height, - width: height * 1.4, - child: const HowToPlayDialog(), - ), - ); - }, - ).then( - (_) { - if (timer.isActive) { - timer.cancel(); - } - }, + builder: (context) => Center( + child: SizedBox( + height: height, + width: height * 1.4, + child: const HowToPlayDialog(), + ), + ), ); }, child: Text(l10n.start), diff --git a/lib/start_game/widgets/how_to_play_dialog.dart b/lib/start_game/widgets/how_to_play_dialog.dart index 9cd2a4e2..bbcd48a1 100644 --- a/lib/start_game/widgets/how_to_play_dialog.dart +++ b/lib/start_game/widgets/how_to_play_dialog.dart @@ -1,5 +1,7 @@ // ignore_for_file: public_member_api_docs +import 'dart:async'; + import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:pinball/gen/gen.dart'; @@ -49,9 +51,31 @@ extension on Control { } } -class HowToPlayDialog extends StatelessWidget { +class HowToPlayDialog extends StatefulWidget { const HowToPlayDialog({Key? key}) : super(key: key); + @override + State createState() => _HowToPlayDialogState(); +} + +class _HowToPlayDialogState extends State { + late Timer closeTimer; + @override + void initState() { + super.initState(); + closeTimer = Timer(const Duration(seconds: 3), () { + if (mounted) { + Navigator.of(context).maybePop(); + } + }); + } + + @override + void dispose() { + closeTimer.cancel(); + super.dispose(); + } + @override Widget build(BuildContext context) { final isMobile = defaultTargetPlatform == TargetPlatform.iOS || diff --git a/test/select_character/view/character_selection_page_test.dart b/test/select_character/view/character_selection_page_test.dart index 90ac509d..dc5d70ea 100644 --- a/test/select_character/view/character_selection_page_test.dart +++ b/test/select_character/view/character_selection_page_test.dart @@ -91,9 +91,23 @@ void main() { 'is displayed for 3 seconds when start is tapped', (tester) async { await tester.pumpApp( - CharacterSelectionView(), + Scaffold( + body: Builder( + builder: (context) { + return ElevatedButton( + onPressed: () { + Navigator.of(context) + .push(CharacterSelectionDialog.route()); + }, + child: Text('Tap me'), + ); + }, + ), + ), characterThemeCubit: characterThemeCubit, ); + await tester.tap(find.text('Tap me')); + await tester.pumpAndSettle(); await tester.ensureVisible(find.byType(TextButton)); await tester.tap(find.byType(TextButton)); await tester.pumpAndSettle();