Skip to content

Commit

Permalink
try to guess if it is a tablet or not
Browse files Browse the repository at this point in the history
  • Loading branch information
moovida committed Nov 11, 2024
1 parent 255739d commit cedb611
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/com/hydrologis/flutterlibs/camera/camera_advanced.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ class _AdvancedCameraWidgetState extends State<AdvancedCameraWidget>
}

Widget buildMainWidget() {
var isTablet = ScreenUtilities.isTablet(context);
return Stack(
children: <Widget>[
Container(
Expand Down Expand Up @@ -1249,15 +1250,17 @@ class _AdvancedCameraWidgetState extends State<AdvancedCameraWidget>

/// A widget showing a live camera preview.
class CameraPreview2 extends StatelessWidget {
/// A widget to overlay on top of the camera preview
final Widget? child;
final bool isTablet;

/// Creates a preview widget for the given camera controller.
const CameraPreview2(this.controller, {super.key, this.child});
const CameraPreview2(this.controller,
{this.isTablet = false, super.key, this.child});

/// The controller for the camera that the preview is shown for.
final CameraController controller;

/// A widget to overlay on top of the camera preview
final Widget? child;

@override
Widget build(BuildContext context) {
return controller.value.isInitialized
Expand Down Expand Up @@ -1308,7 +1311,8 @@ class CameraPreview2 extends StatelessWidget {
DeviceOrientation.portraitDown: 2,
DeviceOrientation.landscapeLeft: 3,
};
return turns[_getApplicableOrientation()]! + 1;
int extraRotation = isTablet ? 1 : 0;
return turns[_getApplicableOrientation()]! + extraRotation;
}

DeviceOrientation _getApplicableOrientation() {
Expand Down
15 changes: 15 additions & 0 deletions lib/com/hydrologis/flutterlibs/utils/screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ class ScreenUtilities {
}
}

/// Check if the screen is in tablet mode
///
/// Try to detect if the device is a tablet or not, based on the diagonal size.
static bool isTablet(BuildContext context) {
// Get the diagonal size in inches
final mediaQuery = MediaQuery.of(context);
final size = mediaQuery.size;
final diagonalInches =
sqrt(size.width * size.width + size.height * size.height) /
mediaQuery.devicePixelRatio;

// Define a threshold for tablet detection (e.g., 7 inches and above)
return diagonalInches >= 7.0;
}

/// Check if the screen is in large width mode, i.e. tablet or phone landscape
static bool isLargeScreen(BuildContext context) {
return MediaQuery.of(context).size.width > 600;
Expand Down

0 comments on commit cedb611

Please sign in to comment.