Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Camera activation after native view + double camera init on first start #1027

Merged
merged 1 commit into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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