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
Navaron Bracke
2023-11-09 10:16:28 +0100
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
7975e22771b8038d033c145d7320d953fe5cc8fa
7975e227
1 parent
c941a4cb
clean up the zoom slider example
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
75 additions
and
127 deletions
example/lib/barcode_scanner_zoom.dart
example/lib/barcode_scanner_zoom.dart
View file @
7975e22
import
'dart:async'
;
import
'package:flutter/material.dart'
;
import
'package:image_picker/image_picker.dart'
;
import
'package:mobile_scanner/mobile_scanner.dart'
;
import
'package:mobile_scanner_example/scanner_button_widgets.dart'
;
import
'package:mobile_scanner_example/scanner_error_widget.dart'
;
class
BarcodeScannerWithZoom
extends
StatefulWidget
{
...
...
@@ -15,13 +17,71 @@ class _BarcodeScannerWithZoomState extends State<BarcodeScannerWithZoom>
with
SingleTickerProviderStateMixin
{
BarcodeCapture
?
barcode
;
MobileScannerController
controller
=
MobileScannerController
(
final
MobileScannerController
controller
=
MobileScannerController
(
torchEnabled:
true
,
);
bool
isStarted
=
true
;
double
_zoomFactor
=
0.0
;
StreamSubscription
<
Object
?>?
_barcodesSubscription
;
@override
void
initState
()
{
super
.
initState
();
_barcodesSubscription
=
controller
.
barcodes
.
listen
((
event
)
{
setState
(()
{
barcode
=
event
;
});
});
controller
.
start
();
}
Widget
_buildZoomScaleSlider
()
{
return
ValueListenableBuilder
(
valueListenable:
controller
,
builder:
(
context
,
state
,
child
)
{
if
(!
state
.
isInitialized
||
!
state
.
isRunning
)
{
return
const
SizedBox
.
shrink
();
}
final
TextStyle
labelStyle
=
Theme
.
of
(
context
)
.
textTheme
.
headlineMedium
!
.
copyWith
(
color:
Colors
.
white
);
return
Padding
(
padding:
const
EdgeInsets
.
symmetric
(
horizontal:
8.0
),
child:
Row
(
children:
[
Text
(
'0%'
,
overflow:
TextOverflow
.
fade
,
style:
labelStyle
,
),
Expanded
(
child:
Slider
(
value:
_zoomFactor
,
onChanged:
(
value
)
{
setState
(()
{
_zoomFactor
=
value
;
controller
.
setZoomScale
(
value
);
});
},
),
),
Text
(
'100%'
,
overflow:
TextOverflow
.
fade
,
style:
labelStyle
,
),
],
),
);
},
);
}
@override
Widget
build
(
BuildContext
context
)
{
return
Scaffold
(
...
...
@@ -37,11 +97,6 @@ class _BarcodeScannerWithZoomState extends State<BarcodeScannerWithZoom>
errorBuilder:
(
context
,
error
,
child
)
{
return
ScannerErrorWidget
(
error:
error
);
},
onDetect:
(
barcode
)
{
setState
(()
{
this
.
barcode
=
barcode
;
});
},
),
Align
(
alignment:
Alignment
.
bottomCenter
,
...
...
@@ -51,81 +106,12 @@ class _BarcodeScannerWithZoomState extends State<BarcodeScannerWithZoom>
color:
Colors
.
black
.
withOpacity
(
0.4
),
child:
Column
(
children:
[
Padding
(
padding:
const
EdgeInsets
.
symmetric
(
horizontal:
8.0
),
child:
Row
(
children:
[
Text
(
"0%"
,
overflow:
TextOverflow
.
fade
,
style:
Theme
.
of
(
context
)
.
textTheme
.
headlineMedium
!
.
copyWith
(
color:
Colors
.
white
),
),
Expanded
(
child:
Slider
(
max:
100
,
divisions:
100
,
value:
_zoomFactor
,
label:
"
${_zoomFactor.round()}
%"
,
onChanged:
(
value
)
{
setState
(()
{
_zoomFactor
=
value
;
controller
.
setZoomScale
(
value
);
});
},
),
),
Text
(
"100%"
,
overflow:
TextOverflow
.
fade
,
style:
Theme
.
of
(
context
)
.
textTheme
.
headlineMedium
!
.
copyWith
(
color:
Colors
.
white
),
),
],
),
),
_buildZoomScaleSlider
(),
Row
(
mainAxisAlignment:
MainAxisAlignment
.
spaceEvenly
,
children:
[
IconButton
(
color:
Colors
.
white
,
icon:
ValueListenableBuilder
<
TorchState
>(
valueListenable:
controller
.
torchState
,
builder:
(
context
,
state
,
child
)
{
switch
(
state
)
{
case
TorchState
.
off
:
return
const
Icon
(
Icons
.
flash_off
,
color:
Colors
.
grey
,
);
case
TorchState
.
on
:
return
const
Icon
(
Icons
.
flash_on
,
color:
Colors
.
yellow
,
);
}
},
),
iconSize:
32.0
,
onPressed:
()
=>
controller
.
toggleTorch
(),
),
IconButton
(
color:
Colors
.
white
,
icon:
isStarted
?
const
Icon
(
Icons
.
stop
)
:
const
Icon
(
Icons
.
play_arrow
),
iconSize:
32.0
,
onPressed:
()
=>
setState
(()
{
isStarted
?
controller
.
stop
()
:
controller
.
start
();
isStarted
=
!
isStarted
;
}),
),
ToggleFlashlightButton
(
controller:
controller
),
StartStopMobileScannerButton
(
controller:
controller
),
Center
(
child:
SizedBox
(
width:
MediaQuery
.
of
(
context
).
size
.
width
-
200
,
...
...
@@ -143,53 +129,8 @@ class _BarcodeScannerWithZoomState extends State<BarcodeScannerWithZoom>
),
),
),
IconButton
(
color:
Colors
.
white
,
icon:
ValueListenableBuilder
<
CameraFacing
>(
valueListenable:
controller
.
cameraFacingState
,
builder:
(
context
,
state
,
child
)
{
switch
(
state
)
{
case
CameraFacing
.
front
:
return
const
Icon
(
Icons
.
camera_front
);
case
CameraFacing
.
back
:
return
const
Icon
(
Icons
.
camera_rear
);
}
},
),
iconSize:
32.0
,
onPressed:
()
=>
controller
.
switchCamera
(),
),
IconButton
(
color:
Colors
.
white
,
icon:
const
Icon
(
Icons
.
image
),
iconSize:
32.0
,
onPressed:
()
async
{
final
ImagePicker
picker
=
ImagePicker
();
// Pick an image
final
XFile
?
image
=
await
picker
.
pickImage
(
source
:
ImageSource
.
gallery
,
);
if
(
image
!=
null
)
{
if
(
await
controller
.
analyzeImage
(
image
.
path
))
{
if
(!
context
.
mounted
)
return
;
ScaffoldMessenger
.
of
(
context
).
showSnackBar
(
const
SnackBar
(
content:
Text
(
'Barcode found!'
),
backgroundColor:
Colors
.
green
,
),
);
}
else
{
if
(!
context
.
mounted
)
return
;
ScaffoldMessenger
.
of
(
context
).
showSnackBar
(
const
SnackBar
(
content:
Text
(
'No barcode found!'
),
backgroundColor:
Colors
.
red
,
),
);
}
}
},
),
SwitchCameraButton
(
controller:
controller
),
AnalyzeImageFromGalleryButton
(
controller:
controller
),
],
),
],
...
...
@@ -202,4 +143,11 @@ class _BarcodeScannerWithZoomState extends State<BarcodeScannerWithZoom>
),
);
}
@override
Future
<
void
>
dispose
()
async
{
_barcodesSubscription
?.
cancel
();
await
controller
.
dispose
();
super
.
dispose
();
}
}
...
...
Please
register
or
login
to post a comment