diff --git a/example/pubspec.lock b/example/pubspec.lock index 30e07fb3..936340bb 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -180,6 +180,14 @@ packages: description: flutter source: sdk version: "0.0.0" + focus_detector: + dependency: transitive + description: + name: focus_detector + sha256: "05e32d9dd378cd54f1a3f9ce813c05156f28eb83f8e68f5bf1a37e9cdb21af1c" + url: "https://pub.dev" + source: hosted + version: "2.0.1" fuchsia_remote_debug_protocol: dependency: transitive description: flutter @@ -571,6 +579,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.0.13" + visibility_detector: + dependency: transitive + description: + name: visibility_detector + sha256: ec932527913f32f65aa01d3a393504240b9e9021ecc77123f017755605e48832 + url: "https://pub.dev" + source: hosted + version: "0.2.2" vm_service: dependency: transitive description: diff --git a/lib/src/widgets/camera_awesome_builder.dart b/lib/src/widgets/camera_awesome_builder.dart index 1d8b7e46..e8c50c6c 100644 --- a/lib/src/widgets/camera_awesome_builder.dart +++ b/lib/src/widgets/camera_awesome_builder.dart @@ -7,6 +7,7 @@ import 'package:camerawesome/src/orchestrator/camera_context.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; +import 'package:focus_detector/focus_detector.dart'; /// This is the builder for your camera interface /// Using the [state] you can do anything you need without having to think about the camera flow @@ -405,7 +406,10 @@ class _CameraWidgetBuilder extends State void initState() { super.initState(); WidgetsBinding.instance.addObserver(this); + init(); + } + void init() { _cameraContext = CameraContext.create( SensorConfig( sensor: widget.sensor, @@ -433,85 +437,99 @@ class _CameraWidgetBuilder extends State @override Widget build(BuildContext context) { - return AwesomeThemeProvider( - theme: widget.theme, - child: StreamBuilder( - stream: _cameraContext.state$, - builder: (context, snapshot) { - if (!snapshot.hasData || - snapshot.data!.captureMode == null || - snapshot.requireData is PreparingCameraState) { - return widget.progressIndicator ?? - Center( - child: Platform.isIOS - ? const CupertinoActivityIndicator() - : const CircularProgressIndicator(), - ); - } - return Stack( - fit: StackFit.expand, - children: [ - Positioned.fill( - child: !widget.showPreview - ? widget.builder( - snapshot.requireData, - PreviewSize(width: 0, height: 0), - Rect.zero, - ) - : AwesomeCameraPreview( - key: _cameraPreviewKey, - previewFit: widget.previewFit, - state: snapshot.requireData, - padding: widget.previewPadding, - alignment: widget.previewAlignment, - onPreviewTap: widget.onPreviewTapBuilder - ?.call(snapshot.requireData) ?? - OnPreviewTap( - onTap: (position, flutterPreviewSize, - pixelPreviewSize) { - snapshot.requireData.when( - onPhotoMode: (photoState) => - photoState.focusOnPoint( - flutterPosition: position, - pixelPreviewSize: pixelPreviewSize, - flutterPreviewSize: flutterPreviewSize, - ), - onVideoMode: (videoState) => - videoState.focusOnPoint( - flutterPosition: position, - pixelPreviewSize: pixelPreviewSize, - flutterPreviewSize: flutterPreviewSize, - ), - onVideoRecordingMode: (videoRecState) => - videoRecState.focusOnPoint( - flutterPosition: position, - pixelPreviewSize: pixelPreviewSize, - flutterPreviewSize: flutterPreviewSize, - ), - onPreviewMode: (previewState) => - previewState.focusOnPoint( - flutterPosition: position, - pixelPreviewSize: pixelPreviewSize, - flutterPreviewSize: flutterPreviewSize, - ), - ); - }, - ), - onPreviewScale: widget.onPreviewScaleBuilder - ?.call(snapshot.requireData) ?? - OnPreviewScale( - onScale: (scale) { - snapshot.requireData.sensorConfig - .setZoom(scale); - }, - ), - interfaceBuilder: widget.builder, - previewDecoratorBuilder: widget.previewDecoratorBuilder, - ), - ), - ], - ); - }, + return FocusDetector( + onFocusGained: () async { + if (((await _cameraContext.state$.first).runtimeType) != + PreparingCameraState) { + setState(() { + init(); + }); + } + }, + onFocusLost: () { + _cameraContext.dispose(); + }, + child: AwesomeThemeProvider( + theme: widget.theme, + child: StreamBuilder( + stream: _cameraContext.state$, + builder: (context, snapshot) { + if (!snapshot.hasData || + snapshot.data!.captureMode == null || + snapshot.requireData is PreparingCameraState) { + return widget.progressIndicator ?? + Center( + child: Platform.isIOS + ? const CupertinoActivityIndicator() + : const CircularProgressIndicator(), + ); + } + return Stack( + fit: StackFit.expand, + children: [ + Positioned.fill( + child: !widget.showPreview + ? widget.builder( + snapshot.requireData, + PreviewSize(width: 0, height: 0), + Rect.zero, + ) + : AwesomeCameraPreview( + key: _cameraPreviewKey, + previewFit: widget.previewFit, + state: snapshot.requireData, + padding: widget.previewPadding, + alignment: widget.previewAlignment, + onPreviewTap: widget.onPreviewTapBuilder + ?.call(snapshot.requireData) ?? + OnPreviewTap( + onTap: (position, flutterPreviewSize, + pixelPreviewSize) { + snapshot.requireData.when( + onPhotoMode: (photoState) => + photoState.focusOnPoint( + flutterPosition: position, + pixelPreviewSize: pixelPreviewSize, + flutterPreviewSize: flutterPreviewSize, + ), + onVideoMode: (videoState) => + videoState.focusOnPoint( + flutterPosition: position, + pixelPreviewSize: pixelPreviewSize, + flutterPreviewSize: flutterPreviewSize, + ), + onVideoRecordingMode: (videoRecState) => + videoRecState.focusOnPoint( + flutterPosition: position, + pixelPreviewSize: pixelPreviewSize, + flutterPreviewSize: flutterPreviewSize, + ), + onPreviewMode: (previewState) => + previewState.focusOnPoint( + flutterPosition: position, + pixelPreviewSize: pixelPreviewSize, + flutterPreviewSize: flutterPreviewSize, + ), + ); + }, + ), + onPreviewScale: widget.onPreviewScaleBuilder + ?.call(snapshot.requireData) ?? + OnPreviewScale( + onScale: (scale) { + snapshot.requireData.sensorConfig + .setZoom(scale); + }, + ), + interfaceBuilder: widget.builder, + previewDecoratorBuilder: + widget.previewDecoratorBuilder, + ), + ), + ], + ); + }, + ), ), ); } diff --git a/pubspec.lock b/pubspec.lock index e2695608..f6889061 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -139,6 +139,14 @@ packages: description: flutter source: sdk version: "0.0.0" + focus_detector: + dependency: "direct main" + description: + name: focus_detector + sha256: "05e32d9dd378cd54f1a3f9ce813c05156f28eb83f8e68f5bf1a37e9cdb21af1c" + url: "https://pub.dev" + source: hosted + version: "2.0.1" glob: dependency: transitive description: @@ -328,6 +336,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.4" + visibility_detector: + dependency: transitive + description: + name: visibility_detector + sha256: ec932527913f32f65aa01d3a393504240b9e9021ecc77123f017755605e48832 + url: "https://pub.dev" + source: hosted + version: "0.2.2" watcher: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 2f9da81a..c5f5a7e6 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -16,6 +16,7 @@ dependencies: colorfilter_generator: ^0.0.8 flutter: sdk: flutter + focus_detector: ^2.0.1 image: ^4.0.15 # photofilters: # git: https://github.com/svenopdehipt/photofilters.git