From 4b30d1fa070049856247d313c301999ea39ba7b0 Mon Sep 17 00:00:00 2001 From: AmirrezaChegini Date: Wed, 29 Oct 2025 10:23:25 +0330 Subject: [PATCH] add: inkwell with play audio effect --- lib/core/widgets/button/my_blue_button.dart | 5 +- lib/core/widgets/button/my_yellow_button.dart | 3 +- lib/core/widgets/dialog/about_us_dialog.dart | 4 +- .../widgets/dialog/styles/dialog_button.dart | 3 +- lib/core/widgets/inkwell/my_inkwell.dart | 55 +++++++++++++++++++ .../home/presentation/bloc/home_bloc.dart | 9 --- .../home/presentation/ui/home_page.dart | 7 ++- 7 files changed, 68 insertions(+), 18 deletions(-) create mode 100644 lib/core/widgets/inkwell/my_inkwell.dart diff --git a/lib/core/widgets/button/my_blue_button.dart b/lib/core/widgets/button/my_blue_button.dart index 8006b49..b90f8b1 100644 --- a/lib/core/widgets/button/my_blue_button.dart +++ b/lib/core/widgets/button/my_blue_button.dart @@ -5,6 +5,7 @@ import 'package:hadi_hoda_flutter/common_ui/resources/my_spaces.dart'; import 'package:hadi_hoda_flutter/common_ui/resources/my_text_style.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'; class MyBlueButton extends StatelessWidget { const MyBlueButton({ @@ -20,7 +21,7 @@ class MyBlueButton extends StatelessWidget { @override Widget build(BuildContext context) { - return InkWell( + return MyInkwell( onTap: onTap, highlightColor: MyColors.transparent, splashColor: MyColors.transparent, @@ -36,7 +37,7 @@ class MyBlueButton extends StatelessWidget { '', ), PositionedDirectional( - top: top ?? setSize(context: context, mobile: MySpaces.s10, tablet: MySpaces.s20), + top: top ?? setSize(context: context, mobile: MySpaces.s6, tablet: MySpaces.s20), child: Text( title ?? '', style: MYTextStyle.button1.copyWith( diff --git a/lib/core/widgets/button/my_yellow_button.dart b/lib/core/widgets/button/my_yellow_button.dart index b6364b4..33fc12e 100644 --- a/lib/core/widgets/button/my_yellow_button.dart +++ b/lib/core/widgets/button/my_yellow_button.dart @@ -5,6 +5,7 @@ import 'package:hadi_hoda_flutter/common_ui/resources/my_spaces.dart'; import 'package:hadi_hoda_flutter/common_ui/resources/my_text_style.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'; class MyYellowButton extends StatelessWidget { const MyYellowButton({ @@ -20,7 +21,7 @@ class MyYellowButton extends StatelessWidget { @override Widget build(BuildContext context) { - return InkWell( + return MyInkwell( onTap: onTap, highlightColor: MyColors.transparent, splashColor: MyColors.transparent, diff --git a/lib/core/widgets/dialog/about_us_dialog.dart b/lib/core/widgets/dialog/about_us_dialog.dart index 4ecdc78..4992978 100644 --- a/lib/core/widgets/dialog/about_us_dialog.dart +++ b/lib/core/widgets/dialog/about_us_dialog.dart @@ -9,6 +9,7 @@ import 'package:hadi_hoda_flutter/core/utils/my_localization.dart'; import 'package:hadi_hoda_flutter/core/utils/set_platform_size.dart'; import 'package:hadi_hoda_flutter/core/widgets/dialog/styles/dialog_background.dart'; import 'package:hadi_hoda_flutter/core/widgets/images/my_image.dart'; +import 'package:hadi_hoda_flutter/core/widgets/inkwell/my_inkwell.dart'; Future showAboutUsDialog({required BuildContext context}) async { await showDialog( @@ -91,9 +92,8 @@ class AboutUsDialog extends StatelessWidget { Positioned( right: setSize(context: context, mobile: 30, tablet: 40), top: setSize(context: context, mobile: -12, tablet: -20), - child: GestureDetector( + child: MyInkwell( onTap: context.pop, - behavior: HitTestBehavior.opaque, child: MyImage( image: MyAssets.closeBtn, size: setSize(context: context, mobile: 40, tablet: 60), diff --git a/lib/core/widgets/dialog/styles/dialog_button.dart b/lib/core/widgets/dialog/styles/dialog_button.dart index fc4f160..ef3a819 100644 --- a/lib/core/widgets/dialog/styles/dialog_button.dart +++ b/lib/core/widgets/dialog/styles/dialog_button.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:hadi_hoda_flutter/core/widgets/inkwell/my_inkwell.dart'; class DialogButton extends StatelessWidget { const DialogButton({ @@ -20,7 +21,7 @@ class DialogButton extends StatelessWidget { clipper: ButtonDialogClipper(), child: Material( type: MaterialType.transparency, - child: InkWell( + child: MyInkwell( onTap: onTap, child: Ink( height: height, diff --git a/lib/core/widgets/inkwell/my_inkwell.dart b/lib/core/widgets/inkwell/my_inkwell.dart new file mode 100644 index 0000000..2967c7d --- /dev/null +++ b/lib/core/widgets/inkwell/my_inkwell.dart @@ -0,0 +1,55 @@ +import 'package:flutter/material.dart'; +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/services/audio_service.dart'; +import 'package:hadi_hoda_flutter/init_bindings.dart'; + +class MyInkwell extends StatefulWidget { + const MyInkwell({ + super.key, + this.onTap, + this.child, + this.borderRadius, + this.highlightColor, + this.splashColor, + }); + + final VoidCallback? onTap; + final Widget? child; + final BorderRadius? borderRadius; + final Color? highlightColor; + final Color? splashColor; + + @override + State createState() => _MyInkwellState(); +} + +class _MyInkwellState extends State { + final AudioService audioService = locator( + instanceName: MyConstants.effectAudioService, + ); + + @override + void initState() { + super.initState(); + } + + void playAudio() { + audioService.setAudio(assetPath: MyAudios.clickButton); + audioService.play(); + } + + @override + Widget build(BuildContext context) { + return InkWell( + onTap: () { + playAudio(); + widget.onTap?.call(); + }, + borderRadius: widget.borderRadius, + highlightColor: widget.highlightColor, + splashColor: widget.splashColor, + child: widget.child, + ); + } +} diff --git a/lib/features/home/presentation/bloc/home_bloc.dart b/lib/features/home/presentation/bloc/home_bloc.dart index 6dd61de..339e1e5 100644 --- a/lib/features/home/presentation/bloc/home_bloc.dart +++ b/lib/features/home/presentation/bloc/home_bloc.dart @@ -38,7 +38,6 @@ class HomeBloc extends Bloc { /// ------------Functions------------ void goToLevelPage(BuildContext context){ - playEffect(); final String? selectedLanguage = LocalStorage.readData(key: MyConstants.selectLanguage); final Box dataBox = Hive.box(MyConstants.levelBox); final TotalDataEntity findData = dataBox.values.singleWhere( @@ -53,17 +52,14 @@ class HomeBloc extends Bloc { } void goToLanguagePage(BuildContext context){ - playEffect(); context.pushNamed(Routes.languagePage); } void showAboutUs(BuildContext context){ - playEffect(); showAboutUsDialog(context: context); } Future changeMute() async { - playEffect(); await Future.wait([ _mainAudioService.changeMute(), _effectAudioService.changeMute(), @@ -78,11 +74,6 @@ class HomeBloc extends Bloc { await _mainAudioService.play(); } - Future playEffect() async{ - await _effectAudioService.setAudio(assetPath: MyAudios.clickButton); - await _effectAudioService.play(); - } - void onPopInvokedWithResult(bool didPop, dynamic result) { showExitDialog(context: ContextProvider.context); } diff --git a/lib/features/home/presentation/ui/home_page.dart b/lib/features/home/presentation/ui/home_page.dart index 32dfd4f..9a2a1b5 100644 --- a/lib/features/home/presentation/ui/home_page.dart +++ b/lib/features/home/presentation/ui/home_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/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/features/home/presentation/bloc/home_bloc.dart'; class HomePage extends StatelessWidget { @@ -49,7 +50,7 @@ class HomePage extends StatelessWidget { child: StreamBuilder( initialData: 1, stream: context.read().volumeStream, - builder: (context, snapshot) => InkWell( + builder: (context, snapshot) => MyInkwell( onTap: () => context.read().changeMute(), child: MyImage( image: snapshot.data == 0 ? MyAssets.musicOff : MyAssets.musicOn, @@ -90,7 +91,7 @@ class HomePage extends StatelessWidget { crossAxisAlignment: CrossAxisAlignment.end, mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ - InkWell( + MyInkwell( onTap: () => context.read().goToLanguagePage(context), child: MyImage( image: MyAssets.language, @@ -101,7 +102,7 @@ class HomePage extends StatelessWidget { onTap: () => context.read().goToLevelPage(context), title: context.translate.start, ), - InkWell( + MyInkwell( onTap: () => context.read().showAboutUs(context), child: MyImage( image: MyAssets.theme,