add: app life cycle observer
#47
Merged
amirreza.chegini
merged 1 commits from fix/changes into develop 5 days ago
4 changed files with 66 additions and 1 deletions
-
32lib/core/utils/app_life_cycle.dart
-
12lib/features/question/presentation/bloc/question_bloc.dart
-
21lib/features/question/presentation/ui/screens/question_screen.dart
-
2lib/main.dart
@ -0,0 +1,32 @@ |
|||||
|
import 'package:flutter/material.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 AppLifeCycleController extends WidgetsBindingObserver { |
||||
|
AppLifeCycleController() { |
||||
|
WidgetsBinding.instance.addObserver(this); |
||||
|
} |
||||
|
|
||||
|
void dispose() { |
||||
|
WidgetsBinding.instance.removeObserver(this); |
||||
|
} |
||||
|
|
||||
|
@override |
||||
|
void didChangeAppLifecycleState(AppLifecycleState state) { |
||||
|
final AudioService mainAudio = locator( |
||||
|
instanceName: MyConstants.mainAudioService, |
||||
|
); |
||||
|
final AudioService effect = locator( |
||||
|
instanceName: MyConstants.effectAudioService, |
||||
|
); |
||||
|
|
||||
|
if (state == AppLifecycleState.paused || state == AppLifecycleState.inactive) { |
||||
|
mainAudio.pause(); |
||||
|
effect.pause(); |
||||
|
} else if (state == AppLifecycleState.resumed) { |
||||
|
mainAudio.play(); |
||||
|
effect.play(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue