Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoogstraat committed May 10, 2021
1 parent b78b9a8 commit 455a72a
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 59 deletions.
10 changes: 5 additions & 5 deletions fast_barcode_scanner/example/lib/detections_counter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ class _DetectionsCounterState extends State<DetectionsCounter> {
@override
void initState() {
super.initState();
_streamToken = CameraController.instance.state.codeStream.listen((event) {
final count = detectionCount.update(event.value, (value) => value + 1,
ifAbsent: () => 1);
detectionInfo.value = "${count}x\n${event.value}";
});
// _streamToken = CameraController.instance.state.codeStream.listen((event) {
// final count = detectionCount.update(event.value, (value) => value + 1,
// ifAbsent: () => 1);
// detectionInfo.value = "${count}x\n${event.value}";
// });
}

late StreamSubscription _streamToken;
Expand Down
39 changes: 17 additions & 22 deletions fast_barcode_scanner/example/lib/scanner_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,23 @@ class _ScannerScreenState extends State<ScannerScreen> {
framerate: Framerate.fps30,
mode: DetectionMode.pauseVideo,
position: CameraPosition.front,
child: Stack(
alignment: Alignment.center,
fit: StackFit.expand,
children: [
// (key) => BeepPreviewOverlay(key: key),
MaterialPreviewOverlay(animateDetection: false),
BlurPreviewOverlay(),
Positioned(
bottom: 50,
child: Column(
children: [
ElevatedButton(
child: Text("Resume"),
onPressed: () => CameraController.instance.resumeDetector(),
),
SizedBox(height: 20),
DetectionsCounter()
],
),
)
],
),
children: [
MaterialPreviewOverlay(animateDetection: false),
BlurPreviewOverlay(),
Positioned(
bottom: 50,
child: Column(
children: [
ElevatedButton(
child: Text("Resume"),
onPressed: () => CameraController.instance.resumeDetector(),
),
SizedBox(height: 20),
DetectionsCounter()
],
),
)
],
),
);
}
Expand Down
27 changes: 10 additions & 17 deletions fast_barcode_scanner/lib/src/camera.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class BarcodeCamera extends StatefulWidget {
this.resolution = Resolution.hd720,
this.framerate = Framerate.fps30,
this.position = CameraPosition.back,
this.child,
this.onScan,
this.children = const [],
ErrorCallback? onError})
: onError = onError ?? _defaultOnError,
super(key: key);
Expand All @@ -35,33 +36,25 @@ class BarcodeCamera extends StatefulWidget {
final Framerate framerate;
final DetectionMode mode;
final CameraPosition position;
final Widget? child;
final void Function(Barcode)? onScan;
final List<Widget> children;
final ErrorCallback onError;

@override
BarcodeCameraState createState() => BarcodeCameraState();
}

class BarcodeCameraState extends State<BarcodeCamera> {
var _opacity = 1.0;
var _opacity = 0.0;

@override
void didChangeDependencies() {
super.didChangeDependencies();
print("UPDATE DEPS");
if (!CameraController.instance.state.isInitialized) {
_opacity = 0.0;
CameraController.instance
.initialize(widget.types, widget.resolution, widget.framerate,
widget.mode, widget.position)
.whenComplete(() => setState(() => _opacity = 1.0));
}
}

@override
void dispose() {
CameraController.instance.dispose();
super.dispose();
CameraController.instance
.initialize(widget.types, widget.resolution, widget.framerate,
widget.mode, widget.position, widget.onScan)
.whenComplete(() => setState(() => _opacity = 1.0));
}

@override
Expand All @@ -79,7 +72,7 @@ class BarcodeCameraState extends State<BarcodeCamera> {
children: [
if (camera.isInitialized)
_buildPreview(camera.previewConfig!),
if (widget.child != null) widget.child!
...widget.children
],
),
),
Expand Down
27 changes: 13 additions & 14 deletions fast_barcode_scanner/lib/src/camera_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,32 @@ class CameraController {
/// The camera is initialized only once per session.
/// All susequent calls to this method will be dropped.
Future<void> initialize(
List<BarcodeType> types,
Resolution resolution,
Framerate framerate,
DetectionMode detectionMode,
CameraPosition position,
) async {
List<BarcodeType> types,
Resolution resolution,
Framerate framerate,
DetectionMode detectionMode,
CameraPosition position,
void Function(Barcode)? onScan) async {
state.eventNotifier.value = CameraEvent.init;

try {
if (state.isInitialized) await _platform.dispose();
state._previewConfig = await _platform.init(
types, resolution, framerate, detectionMode, position);

/// Notify the overlays when a barcode is detected and then call [onDetect].
_platform.setOnDetectHandler((code) {
state.eventNotifier.value = CameraEvent.codeFound;
onScan?.call(code);
});

state.eventNotifier.value = CameraEvent.resumed;
} catch (error, stack) {
print(error);
debugPrintStack(stackTrace: stack);
state.eventNotifier.value = CameraEvent.error;
return;
}

/// Notify the overlays when a barcode is detected and then call [onDetect].
_platform.setOnDetectHandler((code) {
state.eventNotifier.value = CameraEvent.codeFound;
state._codeStream.add(code);
});
}

Future<void> dispose() async {
Expand Down Expand Up @@ -111,7 +112,6 @@ class CameraController {
}

class CameraState {
final _codeStream = StreamController<Barcode>.broadcast();
PreviewConfiguration? _previewConfig;
bool _togglingTorch = false;
Object? error;
Expand All @@ -120,5 +120,4 @@ class CameraState {
bool get isInitialized => _previewConfig != null;
bool get hasError => error != null;
PreviewConfiguration? get previewConfig => _previewConfig;
Stream<Barcode> get codeStream => _codeStream.stream;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class MethodChannelFastBarcodeScanner extends FastBarcodeScannerPlatform {
// This might fail if the code type is not present in the list of available code types.
// Barcode init will throw in this case.
final barcode = Barcode(call.arguments);
_onDetectHandler?.call(barcode);
this._onDetectHandler?.call(barcode);
break;
default:
assert(true,
Expand Down

0 comments on commit 455a72a

Please sign in to comment.