From 7e836151a7df6054d557e32e54a6439d73ac45eb Mon Sep 17 00:00:00 2001 From: AmirrezaChegini Date: Sun, 2 Nov 2025 11:39:12 +0330 Subject: [PATCH] fix: pop scope --- lib/core/routers/my_routes.dart | 3 +- lib/core/widgets/pop_scope/my_pop_scope.dart | 26 ++++++ .../home/presentation/bloc/home_bloc.dart | 6 -- .../home/presentation/ui/home_page.dart | 7 +- .../level/presentation/ui/level_page.dart | 39 ++++---- .../presentation/ui/question_page.dart | 89 ++++++++++--------- 6 files changed, 97 insertions(+), 73 deletions(-) create mode 100644 lib/core/widgets/pop_scope/my_pop_scope.dart diff --git a/lib/core/routers/my_routes.dart b/lib/core/routers/my_routes.dart index bec75cf..0a7c063 100644 --- a/lib/core/routers/my_routes.dart +++ b/lib/core/routers/my_routes.dart @@ -96,7 +96,8 @@ GoRouter get appPages => GoRouter( path: Routes.homePage, builder: (context, state) => BlocProvider( create: (context) => - HomeBloc(locator(instanceName: MyConstants.mainAudioService), + HomeBloc( + locator(instanceName: MyConstants.mainAudioService), locator(instanceName: MyConstants.effectAudioService), ), child: const HomePage(), diff --git a/lib/core/widgets/pop_scope/my_pop_scope.dart b/lib/core/widgets/pop_scope/my_pop_scope.dart new file mode 100644 index 0000000..96b98da --- /dev/null +++ b/lib/core/widgets/pop_scope/my_pop_scope.dart @@ -0,0 +1,26 @@ +import 'package:flutter/material.dart'; +import 'package:hadi_hoda_flutter/core/widgets/dialog/exit_dialog.dart'; + +class MyPopScope extends StatelessWidget { + const MyPopScope({super.key, required this.child}); + + final Widget child; + + void onPopInvokedWithResult( + bool didPop, + dynamic result, + BuildContext context, + ) { + showExitDialog(context: context); + } + + @override + Widget build(BuildContext context) { + return PopScope( + canPop: false, + onPopInvokedWithResult: (didPop, result) => + onPopInvokedWithResult(didPop, result, context), + child: child, + ); + } +} diff --git a/lib/features/home/presentation/bloc/home_bloc.dart b/lib/features/home/presentation/bloc/home_bloc.dart index 2ae4457..596352e 100644 --- a/lib/features/home/presentation/bloc/home_bloc.dart +++ b/lib/features/home/presentation/bloc/home_bloc.dart @@ -7,10 +7,8 @@ import 'package:hadi_hoda_flutter/common_ui/resources/my_audios.dart'; import 'package:hadi_hoda_flutter/core/constants/my_constants.dart'; import 'package:hadi_hoda_flutter/core/routers/my_routes.dart'; import 'package:hadi_hoda_flutter/core/services/audio_service.dart'; -import 'package:hadi_hoda_flutter/core/utils/context_provider.dart'; import 'package:hadi_hoda_flutter/core/utils/local_storage.dart'; import 'package:hadi_hoda_flutter/core/widgets/dialog/about_us_dialog.dart'; -import 'package:hadi_hoda_flutter/core/widgets/dialog/exit_dialog.dart'; import 'package:hadi_hoda_flutter/features/home/presentation/bloc/home_event.dart'; import 'package:hadi_hoda_flutter/features/home/presentation/bloc/home_state.dart'; import 'package:hadi_hoda_flutter/features/level/domain/entity/total_data_entity.dart'; @@ -74,10 +72,6 @@ class HomeBloc extends Bloc { await _mainAudioService.play(); } - void onPopInvokedWithResult(bool didPop, dynamic result) { - showExitDialog(context: ContextProvider.context); - } - /// ------------Api Calls------------ FutureOr _getHomeEvent(event, emit) async {} } diff --git a/lib/features/home/presentation/ui/home_page.dart b/lib/features/home/presentation/ui/home_page.dart index 1006553..e1aaa72 100644 --- a/lib/features/home/presentation/ui/home_page.dart +++ b/lib/features/home/presentation/ui/home_page.dart @@ -10,6 +10,7 @@ import 'package:hadi_hoda_flutter/core/widgets/animations/slide_up_fade.dart'; import 'package:hadi_hoda_flutter/core/widgets/button/my_yellow_button.dart'; import 'package:hadi_hoda_flutter/core/widgets/images/my_image.dart'; import 'package:hadi_hoda_flutter/core/widgets/inkwell/my_inkwell.dart'; +import 'package:hadi_hoda_flutter/core/widgets/pop_scope/my_pop_scope.dart'; import 'package:hadi_hoda_flutter/features/home/presentation/bloc/home_bloc.dart'; class HomePage extends StatelessWidget { @@ -18,11 +19,7 @@ class HomePage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( - body: PopScope( - canPop: false, - onPopInvokedWithResult: context - .read() - .onPopInvokedWithResult, + body: MyPopScope( child: DecoratedBox( decoration: BoxDecoration( image: DecorationImage( diff --git a/lib/features/level/presentation/ui/level_page.dart b/lib/features/level/presentation/ui/level_page.dart index 436090e..f52b34c 100644 --- a/lib/features/level/presentation/ui/level_page.dart +++ b/lib/features/level/presentation/ui/level_page.dart @@ -7,6 +7,7 @@ import 'package:hadi_hoda_flutter/core/utils/screen_size.dart'; import 'package:hadi_hoda_flutter/core/utils/set_platform_size.dart'; import 'package:hadi_hoda_flutter/core/widgets/images/my_image.dart'; import 'package:hadi_hoda_flutter/core/widgets/inkwell/my_inkwell.dart'; +import 'package:hadi_hoda_flutter/core/widgets/pop_scope/my_pop_scope.dart'; import 'package:hadi_hoda_flutter/features/level/domain/entity/level_entity.dart'; import 'package:hadi_hoda_flutter/features/level/presentation/bloc/level_bloc.dart'; import 'package:hadi_hoda_flutter/features/level/presentation/bloc/level_event.dart'; @@ -24,25 +25,27 @@ class LevelPage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( - body: Stack( - alignment: Alignment.center, - children: [ - SingleChildScrollView( - controller: context.read().scrollController, - reverse: true, - child: Stack( - alignment: Alignment.center, - children: [ - _background(), - _path(context), - _levelLocation(context), - _planets(context), - ], + body: MyPopScope( + child: Stack( + alignment: Alignment.center, + children: [ + SingleChildScrollView( + controller: context.read().scrollController, + reverse: true, + child: Stack( + alignment: Alignment.center, + children: [ + _background(), + _path(context), + _levelLocation(context), + _planets(context), + ], + ), ), - ), - _topButtons(context), - _playButton(context) - ], + _topButtons(context), + _playButton(context) + ], + ), ), ); } diff --git a/lib/features/question/presentation/ui/question_page.dart b/lib/features/question/presentation/ui/question_page.dart index c7a8ebb..75faf47 100644 --- a/lib/features/question/presentation/ui/question_page.dart +++ b/lib/features/question/presentation/ui/question_page.dart @@ -7,6 +7,7 @@ import 'package:hadi_hoda_flutter/core/utils/gap.dart'; import 'package:hadi_hoda_flutter/core/utils/screen_size.dart'; import 'package:hadi_hoda_flutter/core/utils/set_platform_size.dart'; import 'package:hadi_hoda_flutter/core/widgets/animations/slide_down_fade.dart'; +import 'package:hadi_hoda_flutter/core/widgets/pop_scope/my_pop_scope.dart'; import 'package:hadi_hoda_flutter/features/question/presentation/bloc/question_bloc.dart'; import 'package:hadi_hoda_flutter/features/question/presentation/bloc/question_state.dart'; import 'package:hadi_hoda_flutter/features/question/presentation/ui/screens/diamond_screen.dart'; @@ -23,53 +24,55 @@ class QuestionPage extends StatelessWidget { return ShowCaseWidget( builder: (context) { return Scaffold( - body: Container( - height: context.heightScreen, - width: context.widthScreen, - padding: EdgeInsets.symmetric( - horizontal: setSize(context: context, - mobile: MySpaces.s16, - tablet: MySpaces.s30) ?? 0, - vertical: setPlatform(android: MySpaces.s20) ?? 0, - ), - decoration: BoxDecoration( - gradient: LinearGradient( - begin: Alignment.topCenter, - end: Alignment.bottomCenter, - colors: [Color(0XFF6930DA), Color(0XFF263AA1)], + body: MyPopScope( + child: Container( + height: context.heightScreen, + width: context.widthScreen, + padding: EdgeInsets.symmetric( + horizontal: setSize(context: context, + mobile: MySpaces.s16, + tablet: MySpaces.s30) ?? 0, + vertical: setPlatform(android: MySpaces.s20) ?? 0, ), - image: DecorationImage( - image: AssetImage(MyAssets.pattern), - scale: 3, - repeat: ImageRepeat.repeat, - colorFilter: ColorFilter.mode( - Colors.black.withValues(alpha: 0.3), - BlendMode.srcIn, + decoration: BoxDecoration( + gradient: LinearGradient( + begin: Alignment.topCenter, + end: Alignment.bottomCenter, + colors: [Color(0XFF6930DA), Color(0XFF263AA1)], + ), + image: DecorationImage( + image: AssetImage(MyAssets.pattern), + scale: 3, + repeat: ImageRepeat.repeat, + colorFilter: ColorFilter.mode( + Colors.black.withValues(alpha: 0.3), + BlendMode.srcIn, + ), ), ), - ), - child: SafeArea( - bottom: false, - child: Column( - children: [ - _topButtons(context), - MySpaces.s10.gapHeight, - Expanded( - child: BlocBuilder( - buildWhen: (previous, current) => - (previous.currentQuestion?.order != - current.currentQuestion?.order), - builder: (context, state) { - if (state.currentQuestion?.order == - state.levelEntity?.questions?.length) { - return DiamondScreen(); - } else { - return QuestionScreen(); - } - }, + child: SafeArea( + bottom: false, + child: Column( + children: [ + _topButtons(context), + MySpaces.s10.gapHeight, + Expanded( + child: BlocBuilder( + buildWhen: (previous, current) => + (previous.currentQuestion?.order != + current.currentQuestion?.order), + builder: (context, state) { + if (state.currentQuestion?.order == + state.levelEntity?.questions?.length) { + return DiamondScreen(); + } else { + return QuestionScreen(); + } + }, + ), ), - ), - ], + ], + ), ), ), ),