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

Dispose camera when navigated to another page #330

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
176 changes: 97 additions & 79 deletions lib/src/widgets/camera_awesome_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -405,7 +406,10 @@ class _CameraWidgetBuilder extends State<CameraAwesomeBuilder>
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
init();
}

void init() {
_cameraContext = CameraContext.create(
SensorConfig(
sensor: widget.sensor,
Expand Down Expand Up @@ -433,85 +437,99 @@ class _CameraWidgetBuilder extends State<CameraAwesomeBuilder>

@override
Widget build(BuildContext context) {
return AwesomeThemeProvider(
theme: widget.theme,
child: StreamBuilder<CameraState>(
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: <Widget>[
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<CameraState>(
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: <Widget>[
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,
),
),
],
);
},
),
),
);
}
Expand Down
16 changes: 16 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down