Toggle navigation
Toggle navigation
This project
Loading...
Sign in
flutter_package
/
mobile_scanner
Go to a project
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
Julian Steenbakker
2024-05-02 15:20:46 +0200
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
7f935e8a05d424e5681bfb360995f7f7cadc217a
7f935e8a
1 parent
15522ea1
imp: add didChangeAppLifecycleState when no controller is provided
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
33 additions
and
6 deletions
lib/src/mobile_scanner.dart
lib/src/mobile_scanner.dart
View file @
7f935e8
...
...
@@ -118,7 +118,8 @@ class MobileScanner extends StatefulWidget {
State
<
MobileScanner
>
createState
()
=>
_MobileScannerState
();
}
class
_MobileScannerState
extends
State
<
MobileScanner
>
{
class
_MobileScannerState
extends
State
<
MobileScanner
>
with
WidgetsBindingObserver
{
late
final
controller
=
widget
.
controller
??
MobileScannerController
();
/// The current scan window.
...
...
@@ -242,12 +243,13 @@ class _MobileScannerState extends State<MobileScanner> {
);
}
StreamSubscription
?
_
barcodeS
ubscription
;
StreamSubscription
?
_
s
ubscription
;
@override
void
initState
()
{
if
(
widget
.
onDetect
!=
null
)
{
_barcodeSubscription
=
controller
.
barcodes
.
listen
(
widget
.
onDetect
);
WidgetsBinding
.
instance
.
addObserver
(
this
);
_subscription
=
controller
.
barcodes
.
listen
(
widget
.
onDetect
);
}
if
(
controller
.
autoStart
)
{
controller
.
start
();
...
...
@@ -258,9 +260,10 @@ class _MobileScannerState extends State<MobileScanner> {
@override
void
dispose
()
{
super
.
dispose
();
if
(
_barcodeSubscription
!=
null
)
{
_barcodeSubscription
!.
cancel
();
_barcodeSubscription
=
null
;
if
(
_subscription
!=
null
)
{
_subscription
!.
cancel
();
_subscription
=
null
;
}
if
(
controller
.
autoStart
)
{
...
...
@@ -272,6 +275,30 @@ class _MobileScannerState extends State<MobileScanner> {
// Dispose default controller if not provided by user
if
(
widget
.
controller
==
null
)
{
controller
.
dispose
();
WidgetsBinding
.
instance
.
removeObserver
(
this
);
}
}
@override
void
didChangeAppLifecycleState
(
AppLifecycleState
state
)
{
if
(
widget
.
controller
!=
null
)
return
;
if
(!
controller
.
value
.
isInitialized
)
{
return
;
}
switch
(
state
)
{
case
AppLifecycleState
.
detached
:
case
AppLifecycleState
.
hidden
:
case
AppLifecycleState
.
paused
:
return
;
case
AppLifecycleState
.
resumed
:
_subscription
=
controller
.
barcodes
.
listen
(
widget
.
onDetect
);
unawaited
(
controller
.
start
());
case
AppLifecycleState
.
inactive
:
unawaited
(
_subscription
?.
cancel
());
_subscription
=
null
;
unawaited
(
controller
.
stop
());
}
}
}
...
...
Please
register
or
login
to post a comment