Flutter: Show different content based on device orientation

Last updated on October 16, 2020 A Goodman Loading... Post a comment

In Flutter, you can show different content based on the device orientation. To determine the device orientation, just use:

MediaQuery.of(context).orientation

Sample Code:

body: MediaQuery.of(context).orientation == Orientation.landscape
            ?
            // Landscape
            Text(
                'Your phone is on landscape mode',
                style: TextStyle(fontSize: 30, color: Colors.purple),
              )
            :
            // Portrait
            Text(
                'Your phone is on portrait mode',
                style: TextStyle(fontSize: 18, color: Colors.red),
           )

Screenshots:

The complete code in main.dart:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // Remove the debug banner
      debugShowCheckedModeBanner: false,
      title: "Kindacode.com",
      home: HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('Kindacode.com'),
        ),
        body: MediaQuery.of(context).orientation == Orientation.landscape
            ?
            // Landscape
            Text(
                'Your phone is on landscape mode',
                style: TextStyle(fontSize: 30, color: Colors.purple),
              )
            :
            // Portrait
            Text(
                'Your phone is on portrait mode',
                style: TextStyle(fontSize: 18, color: Colors.red),
              ));
  }
}

Related Articles

guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x