8

I wonder why Flutter does wonderfully rotate from Portrait to Landscape if run on an iOS device but does not rotate at all on my Android device ??

As a code example you find a "hello world" example below. Again, also for this simple code-example, Flutter rotates for iOS device out of the box but it does not rotate for Android. Why ??

The same ist true for more complex Apps adding ListViews or other (i.e. iOS rotates nicely the screen, Android does not).

I found something called OrientationBuilder (link1) or MediaQuery.orientation (link2). But this seems only offering the capability to distinguish the device's orientations and then you have to act in code accordingly. This seems like a possible solution but I wonder why I have to invest extra Work for Android devices but not for iOS ?

What I am looking for is an "AUTOMATIC" rotation possibility for Android under Flutter (just as iOS does it out of the box). How could I achieve this ?

Here is two screenshots of my "hello world" example. (iOS rotates out of the box / Android does not):

enter image description hereenter image description here

And here is the Dart-code:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter = _counter + 2;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}
iKK
  • 6,394
  • 10
  • 58
  • 131

3 Answers3

18

Thanks to the answers of Jagraj Singh and also user1462442, I finally found a way to rotate the Android Emulator.

It is not enough to just rotate the Android Emulator (for example with CMD <- key press or by the settings-menu) - No! In addition, you have to select the small icon on the Android-Emulator phone menu-bar (see red circle on image)...

Don't know why AVD is making it this way....

enter image description here

iKK
  • 6,394
  • 10
  • 58
  • 131
  • 1
    You only need to do this (just like on a regular device) if you don't have Auto-rotate enabled in the settings. The emulator might install with it disabled, just enable it. – Gábor Oct 24 '20 at 18:17
  • Just in case that some people are confused: Some emulators might don't have it, Pixel XL has the little rotation button, Pixel 2 doesn't. So use a Pixel XL – Boommeister May 23 '22 at 18:07
  • I have done this, but still, the app does not rotate. Strange. – Zenko Sep 22 '22 at 08:54
8

In notification panel or system settings of your phone(Android) change the device orientation to automatic. another method is using system chrome lib of flutter , you can change or set preferred orientation , may be this could help,flutter docs

Jagraj Singh
  • 4,031
  • 4
  • 15
  • 33
  • Thank you Jigraj, for your answer. I know how to fix it to either portrait or landscape (using chrome lib). But I will check the "automatic" setting under system settings once I have an actual device. Inside Emulator, I did not find such a setting. – iKK Dec 17 '18 at 18:58
  • 1
    I have a screen that changes based on the orientation and this saved me hours of pain. I am using a pixel emulator and the slider to change is in settings > Display > Auto-rotate screen. – its_broke_again Oct 26 '21 at 05:45
1

Are you running flutter in an emulator? I believe rotate is automatic when I install it into the device. You probably need to trigger it on android emulator settings.

user1462442
  • 7,672
  • 1
  • 24
  • 27
  • yes, I am running it on an emulator. I don't have an actual Android device at hand. But if you are telling me that rotation works on an actual device, that calms me :). Thank you very much for your answer. – iKK Dec 17 '18 at 18:57
  • You have to trigger it manually on a android emulator. Look at Jahraj Singh answer – user1462442 Dec 17 '18 at 18:59