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
2022-09-28 15:18:14 +0200
Browse Files
Options
Browse Files
Download
Plain Diff
Committed by
GitHub
2022-09-28 15:18:14 +0200
Commit
c943a136d4515f29254ee742f6d718bebc222a9f
c943a136
2 parents
0616500f
1700896b
Merge pull request #231 from MichaelOrpheo/master
feat: Add permission callback
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
21 additions
and
2 deletions
lib/src/mobile_scanner.dart
lib/src/mobile_scanner_controller.dart
lib/src/mobile_scanner.dart
View file @
c943a13
...
...
@@ -9,6 +9,9 @@ class MobileScanner extends StatefulWidget {
/// The controller of the camera.
final
MobileScannerController
?
controller
;
/// Calls the provided [onPermissionSet] callback when the permission is set.
final
Function
(
bool
permissionGranted
)?
onPermissionSet
;
/// Function that gets called when a Barcode is detected.
///
/// [barcode] The barcode object with all information about the scanned code.
...
...
@@ -34,6 +37,7 @@ class MobileScanner extends StatefulWidget {
this
.
controller
,
this
.
fit
=
BoxFit
.
cover
,
this
.
allowDuplicates
=
false
,
this
.
onPermissionSet
,
});
@override
...
...
@@ -48,7 +52,8 @@ class _MobileScannerState extends State<MobileScanner>
void
initState
()
{
super
.
initState
();
WidgetsBinding
.
instance
.
addObserver
(
this
);
controller
=
widget
.
controller
??
MobileScannerController
();
controller
=
widget
.
controller
??
MobileScannerController
(
onPermissionSet:
widget
.
onPermissionSet
);
if
(!
controller
.
isStarting
)
controller
.
start
();
}
...
...
@@ -118,7 +123,8 @@ class _MobileScannerState extends State<MobileScanner>
}
}
else
{
if
(
widget
.
controller
==
null
)
{
controller
=
MobileScannerController
();
controller
=
MobileScannerController
(
onPermissionSet:
widget
.
onPermissionSet
);
}
else
if
(
oldWidget
.
controller
!=
widget
.
controller
)
{
controller
=
widget
.
controller
!;
}
...
...
lib/src/mobile_scanner_controller.dart
View file @
c943a13
...
...
@@ -39,6 +39,7 @@ class MobileScannerController {
static
int
?
_controllerHashcode
;
StreamSubscription
?
events
;
Function
(
bool
permissionGranted
)?
onPermissionSet
;
final
ValueNotifier
<
MobileScannerArguments
?>
args
=
ValueNotifier
(
null
);
final
ValueNotifier
<
TorchState
>
torchState
=
ValueNotifier
(
TorchState
.
off
);
late
final
ValueNotifier
<
CameraFacing
>
cameraFacingState
;
...
...
@@ -66,6 +67,7 @@ class MobileScannerController {
this
.
ratio
,
this
.
torchEnabled
,
this
.
formats
,
this
.
onPermissionSet
,
this
.
autoResume
=
true
,
this
.
returnImage
=
false
,
})
{
...
...
@@ -156,11 +158,14 @@ class MobileScannerController {
state
=
result
?
MobileScannerState
.
authorized
:
MobileScannerState
.
denied
;
onPermissionSet
?.
call
(
result
);
break
;
case
MobileScannerState
.
denied
:
isStarting
=
false
;
onPermissionSet
?.
call
(
false
);
throw
PlatformException
(
code:
'NO ACCESS'
);
case
MobileScannerState
.
authorized
:
onPermissionSet
?.
call
(
true
);
break
;
}
}
...
...
@@ -192,6 +197,9 @@ class MobileScannerController {
}
on
PlatformException
catch
(
error
)
{
debugPrint
(
'
${error.code}
:
${error.message}
'
);
isStarting
=
false
;
if
(
error
.
code
==
"MobileScannerWeb"
)
{
onPermissionSet
?.
call
(
false
);
}
// setAnalyzeMode(AnalyzeMode.none.index);
return
;
}
...
...
@@ -204,6 +212,10 @@ class MobileScannerController {
hasTorch
=
startResult
[
'torchable'
]
as
bool
?
??
false
;
if
(
kIsWeb
)
{
onPermissionSet
?.
call
(
true
,
);
// If we reach this line, it means camera permission has been granted
args
.
value
=
MobileScannerArguments
(
webId:
startResult
[
'ViewID'
]
as
String
?,
size:
Size
(
...
...
@@ -287,6 +299,7 @@ class MobileScannerController {
events
?.
cancel
();
events
=
null
;
_controllerHashcode
=
null
;
onPermissionSet
=
null
;
}
barcodesController
.
close
();
}
...
...
Please
register
or
login
to post a comment