Skip to content

Commit

Permalink
add camera state to help with camera preview orientation + fix
Browse files Browse the repository at this point in the history
  • Loading branch information
moovida committed Nov 12, 2024
1 parent cedb611 commit 7d9f9d5
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 15 deletions.
76 changes: 61 additions & 15 deletions lib/com/hydrologis/flutterlibs/camera/camera_advanced.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ class _AdvancedCameraWidgetState extends State<AdvancedCameraWidget>
}

Widget buildMainWidget() {
var isTablet = ScreenUtilities.isTablet(context);
return Stack(
children: <Widget>[
Container(
Expand Down Expand Up @@ -1249,7 +1248,7 @@ class _AdvancedCameraWidgetState extends State<AdvancedCameraWidget>
// found in the LICENSE file.

/// A widget showing a live camera preview.
class CameraPreview2 extends StatelessWidget {
class CameraPreview2 extends StatefulWidget {
/// A widget to overlay on top of the camera preview
final Widget? child;
final bool isTablet;
Expand All @@ -1261,27 +1260,64 @@ class CameraPreview2 extends StatelessWidget {
/// The controller for the camera that the preview is shown for.
final CameraController controller;

@override
State<CameraPreview2> createState() => _CameraPreview2State();
}

class _CameraPreview2State extends State<CameraPreview2>
with WidgetsBindingObserver {
DeviceOrientation? currentOrientation;
CameraState? cameraState;
@override
void initState() {
cameraState = Provider.of<CameraState>(context, listen: false);

super.initState();
WidgetsBinding.instance.addObserver(this);
}

@override
void didChangeMetrics() {
super.didChangeMetrics();
currentOrientation =
cameraState!.getOrientation(context, setPreferred: true);
}

@override
void didChangeDependencies() {
super.didChangeDependencies();
currentOrientation =
cameraState!.getOrientation(context, setPreferred: true);
}

@override
void dispose() {
SystemChrome.setPreferredOrientations([]);
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}

@override
Widget build(BuildContext context) {
return controller.value.isInitialized
return widget.controller.value.isInitialized
? ValueListenableBuilder<CameraValue>(
valueListenable: controller,
valueListenable: widget.controller,
builder: (BuildContext context, Object? value, Widget? child) {
return AspectRatio(
aspectRatio: _isLandscape()
? controller.value.aspectRatio
: (1 / controller.value.aspectRatio),
? widget.controller.value.aspectRatio
: (1 / widget.controller.value.aspectRatio),
child: Stack(
fit: StackFit.expand,
children: <Widget>[
// controller.buildPreview(),
_wrapInRotatedBox(child: controller.buildPreview()),
_wrapInRotatedBox(child: widget.controller.buildPreview()),
child ?? Container(),
],
),
);
},
child: child,
child: widget.child,
)
: Container();
}
Expand Down Expand Up @@ -1311,16 +1347,26 @@ class CameraPreview2 extends StatelessWidget {
DeviceOrientation.portraitDown: 2,
DeviceOrientation.landscapeLeft: 3,
};
int extraRotation = isTablet ? 1 : 0;
return turns[_getApplicableOrientation()]! + extraRotation;
int extraRotation = 0; //widget.isTablet ? 1 : 0;
if (cameraState!.startupOrientation == DeviceOrientation.landscapeLeft ||
cameraState!.startupOrientation == DeviceOrientation.landscapeRight) {
extraRotation += 1;
}
var currentOrientation = _getApplicableOrientation();
print('currentOrientation: $currentOrientation' +
' vs startuporient ${cameraState!.startupOrientation}');

return turns[currentOrientation]! + extraRotation;
}

DeviceOrientation _getApplicableOrientation() {
return controller.value.isRecordingVideo
? controller.value.recordingOrientation!
: (controller.value.previewPauseOrientation ??
controller.value.lockedCaptureOrientation ??
controller.value.deviceOrientation);
return currentOrientation != null
? currentOrientation!
: widget.controller.value.isRecordingVideo
? widget.controller.value.recordingOrientation!
: (widget.controller.value.previewPauseOrientation ??
widget.controller.value.lockedCaptureOrientation ??
widget.controller.value.deviceOrientation);
}
}

Expand Down
77 changes: 77 additions & 0 deletions lib/com/hydrologis/flutterlibs/camera/camera_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
part of smashlibs;

/// Helper class in cases of advanced camera use.
/// Make sue yourt app initialises the init of thsi as follows at app startup:
///
// class HomeWidgetState extends State<WelcomeWidget>
// with WidgetsBindingObserver {
// CameraState? cameraState;
//
// @override
// void initState() {
// cameraState = Provider.of<CameraState>(context, listen: false);
//
// super.initState();
// WidgetsBinding.instance.addObserver(this);
//
// ...
// }
//
// @override
// void didChangeDependencies() {
// super.didChangeDependencies();
// if (cameraState != null) {
// cameraState!.init(context);
// }
// }
//
// @override
// void dispose() {
// WidgetsBinding.instance.removeObserver(this);
// super.dispose();
// }
// ...
//
class CameraState extends ChangeNotifierPlus {
DeviceOrientation? startupOrientation;

void init(BuildContext context) {
if (startupOrientation != null) {
return;
}
startupOrientation = getOrientation(context);
}

void onChanged() {
notifyListenersMsg("camera state changed");
}

DeviceOrientation getOrientation(BuildContext context,
{bool setPreferred = false}) {
DeviceOrientation initialOrientation;
final orientation = MediaQuery.of(context).orientation;
if (orientation == Orientation.portrait) {
if (setPreferred) {
SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
}
var view = View.of(context);
view.viewInsets.bottom == 0
? initialOrientation = DeviceOrientation.portraitUp
: initialOrientation = DeviceOrientation.portraitDown;
} else {
if (setPreferred) {
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeLeft,
DeviceOrientation.landscapeRight
]);
}

initialOrientation =
MediaQuery.of(context).size.width > MediaQuery.of(context).size.height
? DeviceOrientation.landscapeLeft
: DeviceOrientation.landscapeRight;
}
return initialOrientation;
}
}
1 change: 1 addition & 0 deletions lib/smashlibs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import 'package:video_player/video_player.dart';

part 'com/hydrologis/dartlibs/dartlibs.dart';
part 'com/hydrologis/flutterlibs/camera/camera.dart';
part 'com/hydrologis/flutterlibs/camera/camera_state.dart';
part 'com/hydrologis/flutterlibs/camera/camera_advanced.dart';
part 'com/hydrologis/flutterlibs/gps/gps.dart';
part 'com/hydrologis/flutterlibs/filesystem/filemanagement.dart';
Expand Down

0 comments on commit 7d9f9d5

Please sign in to comment.