EN VI

Why orientation == Orientation.portrait is always true although my device is already in landscape

2022-12-11 20:38:02

I am trying to use OrientationBuilder in flutter, but the first if statement in my code is always true.

        OrientationBuilder(
          builder: (context, orientation) {
            if (orientation == Orientation.portrait) {
              return _portraitMode();
            } else {
              return _landscapeMode();
            }
          },
        ),

I am trying to display two different things depending on the screen orientation of my phone. The problem is that, the first if statement is always true.

Solution:

To get the current orientation of your device, you can take help of the MediaQuery class and get the orientation like :-

    var currOrientation = MediaQuery.of(context).orientation

And optimise your code like so,

    return currOrientation == Orientation.potrait ? _potraitMode() : _landscapeMode();

Hope this helps!

Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login