How does Flutter handle orientation change

according to this doc you can use OrientationBuilder.

OrientationBuilder(
  builder: (context, orientation) {
    if (orientation == Orientation.portrait) 
         // return A
    else 
        // return B
});

Basically - yes! Now, more specifically the MediaQuery widget listens for orientation/size/layout changes and rebuilds it's children. This widget is already a part of the MaterialApp and WidgetsApp widget so you probably don't need to include it.

If you want your widget to take advantage of the device orientation, you can use the MediaQuery.of static member to access a MediaQueryData, which contains the device orientation. For example, a simple widget which displays different text in portrait and landscape (needs to be the child of MaterialApp, WidgetsApp, or MediaQuery).

class MyWidget extends StatelessWidget {
  Widget build(BuildContext context) {
    final mediaQueryData = MediaQuery.of(context);
    if (mediaQueryData.orientation == Orientation.landscape) {
      return const Text('landscape');
    }
    return const Text('portrait!');
  }
}

This will help you to force the Flutter application to remain in Portrait (vertical) mode even if the user is rotating the SmartPhone

    void main(){
  ///
  /// Force the layout to Portrait mode
  /// 
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown
  ]);

  runApp(new MyApp());
}

Tags:

Flutter