Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flutter- How to know the device is DeviceOrientation is up or down?

Tags:

flutter

dart

When I am rotating screen its rotates camera along with the screen, so I am getting Inverted camera screen.

return new AspectRatio(
    aspectRatio: controller.value.aspectRatio,
    child: new CameraPreview(controller),
  );
like image 801
satish Avatar asked Jul 11 '18 11:07

satish


People also ask

How do I change my screen orientation on flutter?

In Flutter we have SystemChrome. setPreferredOrientation() (see documentation) to define preferred orientation. Exactly this method you can find in all those articles about setup orientation in Flutter.


2 Answers

You can build responsive apps using OrientationBuilder

OrientationBuilder(
  builder: (context, orientation) {
    return GridView.count(
      // Create a grid with 2 columns in portrait mode, or 3 columns in landscape mode.
      crossAxisCount: orientation == Orientation.portrait ? 2 : 3,
    );
  },
);

or

Orientation currentOrientation = MediaQuery.of(context).orientation;
if(currentOrientation == Orientation.portrait){
    /* ... */
}

To enforce a particular orientation see this https://stackoverflow.com/a/50322184/9664127

other useful links:

https://flutter.io/cookbook/design/orientation/

https://docs.flutter.io/flutter/services/DeviceOrientation-class.html

https://medium.com/@kr1uz/how-to-restrict-device-orientation-in-flutter-65431cd35113

Updated

Flutter does not currently support 'orientation sides', but this plugin may help in that area https://pub.dartlang.org/packages/native_device_orientation

like image 90
riftninja Avatar answered Sep 22 '22 05:09

riftninja


You can use this plugin: https://pub.dartlang.org/packages/native_device_orientation

(I'm having some issues with iOS - reporting opposite orientation, but this can be worked around)

like image 35
Assaf S. Avatar answered Sep 26 '22 05:09

Assaf S.