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-02-23 22:29:00 +0100
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
e96a0d6f5e8eb2a72107a402e76b1c6e1de97dbb
e96a0d6f
1 parent
cd0f2573
bug: fix disposing controller
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
220 additions
and
231 deletions
example/lib/barcode_scanner_controller.dart
example/lib/barcode_scanner_without_controller.dart
example/lib/main.dart
lib/src/mobile_scanner.dart
example/lib/barcode_scanner_controller.dart
0 → 100644
View file @
e96a0d6
import
'package:flutter/material.dart'
;
import
'package:mobile_scanner/mobile_scanner.dart'
;
class
BarcodeScannerWithController
extends
StatefulWidget
{
const
BarcodeScannerWithController
({
Key
?
key
})
:
super
(
key:
key
);
@override
_BarcodeScannerWithControllerState
createState
()
=>
_BarcodeScannerWithControllerState
();
}
class
_BarcodeScannerWithControllerState
extends
State
<
BarcodeScannerWithController
>
with
SingleTickerProviderStateMixin
{
String
?
barcode
;
MobileScannerController
controller
=
MobileScannerController
(
torchEnabled:
true
,
// facing: CameraFacing.front,
);
@override
Widget
build
(
BuildContext
context
)
{
return
MaterialApp
(
home:
Scaffold
(
backgroundColor:
Colors
.
black
,
body:
Builder
(
builder:
(
context
)
{
return
Stack
(
children:
[
MobileScanner
(
controller:
controller
,
fit:
BoxFit
.
contain
,
// controller: MobileScannerController(
// torchEnabled: true,
// facing: CameraFacing.front,
// ),
onDetect:
(
barcode
,
args
)
{
if
(
this
.
barcode
!=
barcode
.
rawValue
)
{
setState
(()
{
this
.
barcode
=
barcode
.
rawValue
;
});
}
}),
Align
(
alignment:
Alignment
.
bottomCenter
,
child:
Container
(
alignment:
Alignment
.
bottomCenter
,
height:
100
,
color:
Colors
.
black
.
withOpacity
(
0.4
),
child:
Row
(
crossAxisAlignment:
CrossAxisAlignment
.
center
,
mainAxisAlignment:
MainAxisAlignment
.
spaceEvenly
,
children:
[
IconButton
(
color:
Colors
.
white
,
icon:
ValueListenableBuilder
(
valueListenable:
controller
.
torchState
,
builder:
(
context
,
state
,
child
)
{
switch
(
state
as
TorchState
)
{
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
(),
),
Center
(
child:
SizedBox
(
width:
MediaQuery
.
of
(
context
).
size
.
width
-
120
,
height:
50
,
child:
FittedBox
(
child:
Text
(
barcode
??
'Scan something!'
,
overflow:
TextOverflow
.
fade
,
style:
Theme
.
of
(
context
)
.
textTheme
.
headline4
!
.
copyWith
(
color:
Colors
.
white
),
),
),
),
),
IconButton
(
color:
Colors
.
white
,
icon:
ValueListenableBuilder
(
valueListenable:
controller
.
cameraFacingState
,
builder:
(
context
,
state
,
child
)
{
switch
(
state
as
CameraFacing
)
{
case
CameraFacing
.
front
:
return
const
Icon
(
Icons
.
camera_front
);
case
CameraFacing
.
back
:
return
const
Icon
(
Icons
.
camera_rear
);
}
},
),
iconSize:
32.0
,
onPressed:
()
=>
controller
.
switchCamera
(),
),
],
),
),
),
],
);
}),
),
);
}
}
...
...
example/lib/barcode_scanner_without_controller.dart
0 → 100644
View file @
e96a0d6
import
'package:flutter/material.dart'
;
import
'package:mobile_scanner/mobile_scanner.dart'
;
class
BarcodeScannerWithoutController
extends
StatefulWidget
{
const
BarcodeScannerWithoutController
({
Key
?
key
})
:
super
(
key:
key
);
@override
_BarcodeScannerWithoutControllerState
createState
()
=>
_BarcodeScannerWithoutControllerState
();
}
class
_BarcodeScannerWithoutControllerState
extends
State
<
BarcodeScannerWithoutController
>
with
SingleTickerProviderStateMixin
{
String
?
barcode
;
@override
Widget
build
(
BuildContext
context
)
{
return
MaterialApp
(
home:
Scaffold
(
backgroundColor:
Colors
.
black
,
body:
Builder
(
builder:
(
context
)
{
return
Stack
(
children:
[
MobileScanner
(
fit:
BoxFit
.
contain
,
onDetect:
(
barcode
,
args
)
{
if
(
this
.
barcode
!=
barcode
.
rawValue
)
{
setState
(()
{
this
.
barcode
=
barcode
.
rawValue
;
});
}
}),
Align
(
alignment:
Alignment
.
bottomCenter
,
child:
Container
(
alignment:
Alignment
.
bottomCenter
,
height:
100
,
color:
Colors
.
black
.
withOpacity
(
0.4
),
child:
Row
(
crossAxisAlignment:
CrossAxisAlignment
.
center
,
mainAxisAlignment:
MainAxisAlignment
.
spaceEvenly
,
children:
[
Center
(
child:
SizedBox
(
width:
MediaQuery
.
of
(
context
).
size
.
width
-
120
,
height:
50
,
child:
FittedBox
(
child:
Text
(
barcode
??
'Scan something!'
,
overflow:
TextOverflow
.
fade
,
style:
Theme
.
of
(
context
)
.
textTheme
.
headline4
!
.
copyWith
(
color:
Colors
.
white
),
),
),
),
),
],
),
),
),
],
);
}),
),
);
}
}
...
...
example/lib/main.dart
View file @
e96a0d6
import
'package:flutter/material.dart'
;
import
'package:mobile_scanner/mobile_scanner.dart'
;
import
'package:mobile_scanner_example/barcode_scanner_controller.dart'
;
import
'package:mobile_scanner_example/barcode_scanner_without_controller.dart'
;
void
main
(
)
{
runApp
(
const
AnalyzeView
());
}
class
AnalyzeView
extends
StatefulWidget
{
const
AnalyzeView
({
Key
?
key
})
:
super
(
key:
key
);
void
main
(
)
=>
runApp
(
const
MaterialApp
(
home:
MyHome
()));
@override
_AnalyzeViewState
createState
()
=>
_AnalyzeViewState
();
}
class
_AnalyzeViewState
extends
State
<
AnalyzeView
>
with
SingleTickerProviderStateMixin
{
String
?
barcode
;
MobileScannerController
controller
=
MobileScannerController
(
torchEnabled:
true
,
facing:
CameraFacing
.
front
,
);
class
MyHome
extends
StatelessWidget
{
const
MyHome
({
Key
?
key
})
:
super
(
key:
key
);
@override
Widget
build
(
BuildContext
context
)
{
return
MaterialApp
(
home:
Scaffold
(
backgroundColor:
Colors
.
black
,
body:
Builder
(
builder:
(
context
)
{
return
Stack
(
children:
[
MobileScanner
(
controller:
controller
,
fit:
BoxFit
.
contain
,
// controller: MobileScannerController(
// torchEnabled: true,
// facing: CameraFacing.front,
// ),
onDetect:
(
barcode
,
args
)
{
if
(
this
.
barcode
!=
barcode
.
rawValue
)
{
setState
(()
{
this
.
barcode
=
barcode
.
rawValue
;
});
}
}),
Align
(
alignment:
Alignment
.
bottomCenter
,
child:
Container
(
alignment:
Alignment
.
bottomCenter
,
height:
100
,
color:
Colors
.
black
.
withOpacity
(
0.4
),
child:
Row
(
return
Scaffold
(
appBar:
AppBar
(
title:
const
Text
(
'Flutter Demo Home Page'
)),
body:
SizedBox
(
width:
MediaQuery
.
of
(
context
).
size
.
width
,
height:
MediaQuery
.
of
(
context
).
size
.
height
,
child:
Column
(
mainAxisAlignment:
MainAxisAlignment
.
center
,
crossAxisAlignment:
CrossAxisAlignment
.
center
,
mainAxisAlignment:
MainAxisAlignment
.
spaceEvenly
,
children:
[
IconButton
(
color:
Colors
.
white
,
icon:
ValueListenableBuilder
(
valueListenable:
controller
.
torchState
,
builder:
(
context
,
state
,
child
)
{
switch
(
state
as
TorchState
)
{
case
TorchState
.
off
:
return
const
Icon
(
Icons
.
flash_off
,
color:
Colors
.
grey
);
case
TorchState
.
on
:
return
const
Icon
(
Icons
.
flash_on
,
color:
Colors
.
yellow
);
}
ElevatedButton
(
onPressed:
()
{
Navigator
.
of
(
context
).
push
(
MaterialPageRoute
(
builder:
(
context
)
=>
const
BarcodeScannerWithController
(),
));
},
child:
const
Text
(
'MobileScanner with Controller'
),
),
iconSize:
32.0
,
onPressed:
()
=>
controller
.
toggleTorch
(),
),
Center
(
child:
SizedBox
(
width:
MediaQuery
.
of
(
context
).
size
.
width
-
120
,
height:
50
,
child:
FittedBox
(
child:
Text
(
barcode
??
'Scan something!'
,
overflow:
TextOverflow
.
fade
,
style:
Theme
.
of
(
context
)
.
textTheme
.
headline4
!
.
copyWith
(
color:
Colors
.
white
),
),
),
),
),
IconButton
(
color:
Colors
.
white
,
icon:
ValueListenableBuilder
(
valueListenable:
controller
.
cameraFacingState
,
builder:
(
context
,
state
,
child
)
{
switch
(
state
as
CameraFacing
)
{
case
CameraFacing
.
front
:
return
const
Icon
(
Icons
.
camera_front
);
case
CameraFacing
.
back
:
return
const
Icon
(
Icons
.
camera_rear
);
}
ElevatedButton
(
onPressed:
()
{
Navigator
.
of
(
context
).
push
(
MaterialPageRoute
(
builder:
(
context
)
=>
const
BarcodeScannerWithoutController
(),
));
},
),
iconSize:
32.0
,
onPressed:
()
=>
controller
.
switchCamera
(),
child:
const
Text
(
'MobileScanner without Controller'
),
),
],
),
),
),
// Container(
// alignment: Alignment.bottomCenter,
// margin: EdgeInsets.only(bottom: 80.0),
// child: IconButton(
// icon: ValueListenableBuilder(
// valueListenable: cameraController.torchState,
// builder: (context, state, child) {
// final color =
// state == TorchState.off ? Colors.grey : Colors.white;
// return Icon(Icons.bolt, color: color);
// },
// ),
// iconSize: 32.0,
// onPressed: () => cameraController.torch(),
// ),
// ),
],
);
}),
),
);
}
@override
void
dispose
()
{
// cameraController.dispose();
super
.
dispose
();
}
void
display
(
Barcode
barcode
)
{
Navigator
.
of
(
context
).
popAndPushNamed
(
'display'
,
arguments:
barcode
);
}
}
// import 'package:flutter/material.dart';
// import 'package:flutter/rendering.dart';
// import 'package:mobile_scanner/mobile_scanner.dart';
//
// void main() {
// debugPaintSizeEnabled = false;
// runApp(HomePage());
// }
//
// class HomePage extends StatefulWidget {
// @override
// HomeState createState() => HomeState();
// }
//
// class HomeState extends State<HomePage> {
// @override
// Widget build(BuildContext context) {
// return MaterialApp(home: MyApp());
// }
// }
//
// class MyApp extends StatefulWidget {
// @override
// _MyAppState createState() => _MyAppState();
// }
//
// class _MyAppState extends State<MyApp> {
// String? qr;
// bool camState = false;
//
// @override
// initState() {
// super.initState();
// }
//
// @override
// Widget build(BuildContext context) {
// return Scaffold(
// appBar: AppBar(
// title: Text('Plugin example app'),
// ),
// body: Center(
// child: Column(
// crossAxisAlignment: CrossAxisAlignment.center,
// mainAxisAlignment: MainAxisAlignment.center,
// children: <Widget>[
// Expanded(
// child: camState
// ? Center(
// child: SizedBox(
// width: 300.0,
// height: 600.0,
// child: MobileScanner(
// onError: (context, error) => Text(
// error.toString(),
// style: TextStyle(color: Colors.red),
// ),
// qrCodeCallback: (code) {
// setState(() {
// qr = code;
// });
// },
// child: Container(
// decoration: BoxDecoration(
// color: Colors.transparent,
// border: Border.all(
// color: Colors.orange,
// width: 10.0,
// style: BorderStyle.solid),
// ),
// ),
// ),
// ),
// )
// : Center(child: Text("Camera inactive"))),
// Text("QRCODE: $qr"),
// ],
// ),
// ),
// floatingActionButton: FloatingActionButton(
// child: Text(
// "press me",
// textAlign: TextAlign.center,
// ),
// onPressed: () {
// setState(() {
// camState = !camState;
// });
// }),
// );
// }
// }
...
...
lib/src/mobile_scanner.dart
View file @
e96a0d6
...
...
@@ -29,8 +29,7 @@ class MobileScanner extends StatefulWidget {
this
.
onDetect
,
this
.
controller
,
this
.
fit
=
BoxFit
.
cover
,
})
:
assert
((
controller
!=
null
)),
super
(
key:
key
);
})
:
super
(
key:
key
);
@override
State
<
MobileScanner
>
createState
()
=>
_MobileScannerState
();
...
...
@@ -38,33 +37,32 @@ class MobileScanner extends StatefulWidget {
class
_MobileScannerState
extends
State
<
MobileScanner
>
with
WidgetsBindingObserver
{
bool
onScreen
=
true
;
late
MobileScannerController
controller
;
@override
void
initState
()
{
super
.
initState
();
WidgetsBinding
.
instance
?.
addObserver
(
this
);
controller
=
widget
.
controller
??
MobileScannerController
();
}
@override
void
didChangeAppLifecycleState
(
AppLifecycleState
state
)
{
if
(
state
==
AppLifecycleState
.
resumed
)
{
setState
(()
=>
onScreen
=
true
);
}
else
{
if
(
onScreen
)
{
switch
(
state
)
{
case
AppLifecycleState
.
resumed
:
controller
.
start
();
break
;
case
AppLifecycleState
.
inactive
:
case
AppLifecycleState
.
paused
:
case
AppLifecycleState
.
detached
:
controller
.
stop
();
}
setState
(()
{
onScreen
=
false
;
});
break
;
}
}
@override
Widget
build
(
BuildContext
context
)
{
return
LayoutBuilder
(
builder:
(
context
,
BoxConstraints
constraints
)
{
if
(!
onScreen
)
return
const
Text
(
"Camera Paused."
);
return
ValueListenableBuilder
(
valueListenable:
controller
.
args
,
builder:
(
context
,
value
,
child
)
{
...
...
@@ -112,7 +110,8 @@ class _MobileScannerState extends State<MobileScanner>
@override
void
dispose
()
{
if
(
widget
.
controller
==
null
)
controller
.
dispose
();
controller
.
dispose
();
WidgetsBinding
.
instance
?.
removeObserver
(
this
);
super
.
dispose
();
}
}
...
...
Please
register
or
login
to post a comment