imp: add didChangeAppLifecycleState when no controller is provided
Showing
1 changed file
with
33 additions
and
6 deletions
| @@ -118,7 +118,8 @@ class MobileScanner extends StatefulWidget { | @@ -118,7 +118,8 @@ class MobileScanner extends StatefulWidget { | ||
| 118 | State<MobileScanner> createState() => _MobileScannerState(); | 118 | State<MobileScanner> createState() => _MobileScannerState(); |
| 119 | } | 119 | } |
| 120 | 120 | ||
| 121 | -class _MobileScannerState extends State<MobileScanner> { | 121 | +class _MobileScannerState extends State<MobileScanner> |
| 122 | + with WidgetsBindingObserver { | ||
| 122 | late final controller = widget.controller ?? MobileScannerController(); | 123 | late final controller = widget.controller ?? MobileScannerController(); |
| 123 | 124 | ||
| 124 | /// The current scan window. | 125 | /// The current scan window. |
| @@ -242,12 +243,13 @@ class _MobileScannerState extends State<MobileScanner> { | @@ -242,12 +243,13 @@ class _MobileScannerState extends State<MobileScanner> { | ||
| 242 | ); | 243 | ); |
| 243 | } | 244 | } |
| 244 | 245 | ||
| 245 | - StreamSubscription? _barcodeSubscription; | 246 | + StreamSubscription? _subscription; |
| 246 | 247 | ||
| 247 | @override | 248 | @override |
| 248 | void initState() { | 249 | void initState() { |
| 249 | if (widget.onDetect != null) { | 250 | if (widget.onDetect != null) { |
| 250 | - _barcodeSubscription = controller.barcodes.listen(widget.onDetect); | 251 | + WidgetsBinding.instance.addObserver(this); |
| 252 | + _subscription = controller.barcodes.listen(widget.onDetect); | ||
| 251 | } | 253 | } |
| 252 | if (controller.autoStart) { | 254 | if (controller.autoStart) { |
| 253 | controller.start(); | 255 | controller.start(); |
| @@ -258,9 +260,10 @@ class _MobileScannerState extends State<MobileScanner> { | @@ -258,9 +260,10 @@ class _MobileScannerState extends State<MobileScanner> { | ||
| 258 | @override | 260 | @override |
| 259 | void dispose() { | 261 | void dispose() { |
| 260 | super.dispose(); | 262 | super.dispose(); |
| 261 | - if (_barcodeSubscription != null) { | ||
| 262 | - _barcodeSubscription!.cancel(); | ||
| 263 | - _barcodeSubscription = null; | 263 | + |
| 264 | + if (_subscription != null) { | ||
| 265 | + _subscription!.cancel(); | ||
| 266 | + _subscription = null; | ||
| 264 | } | 267 | } |
| 265 | 268 | ||
| 266 | if (controller.autoStart) { | 269 | if (controller.autoStart) { |
| @@ -272,6 +275,30 @@ class _MobileScannerState extends State<MobileScanner> { | @@ -272,6 +275,30 @@ class _MobileScannerState extends State<MobileScanner> { | ||
| 272 | // Dispose default controller if not provided by user | 275 | // Dispose default controller if not provided by user |
| 273 | if (widget.controller == null) { | 276 | if (widget.controller == null) { |
| 274 | controller.dispose(); | 277 | controller.dispose(); |
| 278 | + WidgetsBinding.instance.removeObserver(this); | ||
| 279 | + } | ||
| 280 | + } | ||
| 281 | + | ||
| 282 | + @override | ||
| 283 | + void didChangeAppLifecycleState(AppLifecycleState state) { | ||
| 284 | + if (widget.controller != null) return; | ||
| 285 | + if (!controller.value.isInitialized) { | ||
| 286 | + return; | ||
| 287 | + } | ||
| 288 | + | ||
| 289 | + switch (state) { | ||
| 290 | + case AppLifecycleState.detached: | ||
| 291 | + case AppLifecycleState.hidden: | ||
| 292 | + case AppLifecycleState.paused: | ||
| 293 | + return; | ||
| 294 | + case AppLifecycleState.resumed: | ||
| 295 | + _subscription = controller.barcodes.listen(widget.onDetect); | ||
| 296 | + | ||
| 297 | + unawaited(controller.start()); | ||
| 298 | + case AppLifecycleState.inactive: | ||
| 299 | + unawaited(_subscription?.cancel()); | ||
| 300 | + _subscription = null; | ||
| 301 | + unawaited(controller.stop()); | ||
| 275 | } | 302 | } |
| 276 | } | 303 | } |
| 277 | } | 304 | } |
-
Please register or login to post a comment