Skip to content

Commit

Permalink
fix: Camera activation after native view + double camera init on firs…
Browse files Browse the repository at this point in the history
…t start (#1027)
  • Loading branch information
M123-dev authored Jan 27, 2022
1 parent b0dddd2 commit 95b3275
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
32 changes: 20 additions & 12 deletions packages/smooth_app/lib/pages/scan/lifecycle_manager.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:visibility_detector/visibility_detector.dart';

/// This Widgets tracks if the scanner is currently visible and if the app
/// is currently open/idle/closed and controls the camera depending
/// This Widgets tracks if the child is currently visible on screen and if the
/// app gets minimized/resumed by the system
class LifeCycleManager extends StatefulWidget {
const LifeCycleManager({
required this.onResume,
Expand All @@ -21,6 +22,19 @@ class LifeCycleManager extends StatefulWidget {

class LifeCycleManagerState extends State<LifeCycleManager>
with WidgetsBindingObserver {
double visibleFraction = 100.0;
AppLifecycleState appLifecycleState = AppLifecycleState.resumed;

void checkLifeCycle() {
if (appLifecycleState == AppLifecycleState.inactive ||
visibleFraction == 0.0) {
widget.onPause.call();
} else if (appLifecycleState == AppLifecycleState.resumed &&
visibleFraction > 0.0) {
widget.onResume.call();
}
}

@override
void initState() {
super.initState();
Expand All @@ -40,23 +54,17 @@ class LifeCycleManagerState extends State<LifeCycleManager>
// background or returns the app to the foreground.
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.inactive) {
widget.onPause.call();
} else if (state == AppLifecycleState.resumed) {
widget.onResume.call();
}
appLifecycleState = state;
checkLifeCycle();
}

@override
Widget build(BuildContext context) {
return VisibilityDetector(
key: const ValueKey<String>('VisibilityDetector'),
onVisibilityChanged: (VisibilityInfo info) {
if (info.visibleFraction == 0.0) {
widget.onPause.call();
} else {
widget.onResume.call();
}
visibleFraction = info.visibleFraction;
checkLifeCycle();
},
child: widget.child,
);
Expand Down
25 changes: 13 additions & 12 deletions packages/smooth_app/lib/pages/scan/ml_kit_scan_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,31 +110,31 @@ class MLKitScannerPageState extends State<MLKitScannerPage> {
}

Future<void> _startLiveFeed() async {
if (_controller != null) {
return;
}

stoppingCamera = false;
final CameraDescription camera = cameras[_cameraIndex];

final CameraController cameraController = CameraController(
_controller = CameraController(
camera,
ResolutionPreset.high,
enableAudio: false,
);
cameraController.setFocusMode(FocusMode.auto);
cameraController.lockCaptureOrientation(DeviceOrientation.portraitUp);
_controller?.setFocusMode(FocusMode.auto);
_controller?.lockCaptureOrientation(DeviceOrientation.portraitUp);

_controller = cameraController;
_controller = _controller;

// If the controller is updated then update the UI.
cameraController.addListener(() {
try {
await _controller!.initialize();
if (mounted) {
setState(() {});
}
if (cameraController.value.hasError) {
debugPrint(cameraController.value.errorDescription);
if (_controller!.value.hasError) {
debugPrint(_controller!.value.errorDescription);
}
});

try {
await cameraController.initialize();
_controller?.startImageStream(_processCameraImage);
} on CameraException catch (e) {
if (kDebugMode) {
Expand All @@ -154,6 +154,7 @@ class MLKitScannerPageState extends State<MLKitScannerPage> {
setState(() {});
}
await _controller?.dispose();
_controller = null;
}

//Convert the [CameraImage] to a [InputImage]
Expand Down

0 comments on commit 95b3275

Please sign in to comment.