Open In App

Flutter – OrientationBuilder Widget

Last Updated : 30 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

OrientationBuilder is a Flutter widget that allows you to build a widget tree based on the orientation (portrait or landscape) of the device. It’s useful when you want to create a different UI layout or make adjustments based on the device’s orientation. In this article, we are going to implement the OrientationBuilder widget. A sample video is given below to get an idea about what we are going to do in this article.

Basic Syntax of OrientationBuilder Widget

OrientationBuilder(
builder: (BuildContext context, Orientation orientation) {
// Return a widget tree based on the orientation
return YourWidget();
},
)

Required Tools

To build this app, you need the following items installed on your machine:

  • Visual Studio Code / Android Studio
  • Android Emulator / iOS Simulator / Physical Device device.
  • Flutter Installed
  • Flutter plugin for VS Code / Android Studio.

Step By Step Implementation

Step 1: Create a New Project in Android Studio

To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter.

Step 2: Import the Package

First of all import material.dart file.

import 'package:flutter/material.dart';

Step 3: Execute the main Method

Here the execution of our app starts.

Dart




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


Step 4: Create MyApp Class

In this class we are going to implement the MaterialApp , here we are also set the Theme of our App.

Dart




class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
       theme: ThemeData(
        primarySwatch: Colors.green, // Set the app's primary theme color
      ),
      debugShowCheckedModeBanner: false,
      home: MyOrientationDemo(),
    );
  }
}


Step 5: Create MyOrientationDemo Class

In this class we are going to Implement the OrientationBuilder widget .The OrientationBuilder ,the builder callback function takes two arguments: context and orientation. It allows you to conditionally build different UI elements based on the device’s orientation.In this article , we display an icon and a text that changes based on whether the device is in portrait or landscape mode. We use the Icons.phone_android icon for portrait mode and Icons.phone_iphone for landscape mode. We also use a Text widget to display the current orientation(i.e Landscape or portrait). Comments are added for better understanding.

OrientationBuilder(
builder: (context, orientation) {
// Build UI elements based on device orientation
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(
// Display different icons based on orientation
orientation == Orientation.portrait
? Icons.phone_android
: Icons.phone_iphone,
size: 100.0,
),
SizedBox(height: 20.0),
Text(
// Display different text based on orientation
'Device is in ${orientation == Orientation.portrait ? 'Portrait' : 'Landscape'} mode',
style: TextStyle(fontSize: 20.0),
),
],
);
},
),

Dart




class MyOrientationDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('OrientationBuilder Example'),
      ),
      body: Center(
        child: OrientationBuilder(
          builder: (context, orientation) {
            // Build UI elements based on device orientation
            return Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Icon(
                  // Display different icons based on orientation
                  orientation == Orientation.portrait
                      ? Icons.phone_android
                      : Icons.phone_iphone,
                  size: 100.0,
                ),
                SizedBox(height: 20.0),
                Text(
                  // Display different text based on orientation
                  'Device is in ${orientation == Orientation.portrait ? 'Portrait' : 'Landscape'} mode',
                  style: TextStyle(fontSize: 20.0),
                ),
              ],
            );
          },
        ),
      ),
    );
  }
}


Here is the full Code of main.dart file

Dart




import 'package:flutter/material.dart';
  
void main() {
  runApp(MyApp());
}
  
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
       theme: ThemeData(
        primarySwatch: Colors.green,
      ),
      debugShowCheckedModeBanner: false,
      home: MyOrientationDemo(),
    );
  }
}
  
class MyOrientationDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('OrientationBuilder Example'),
      ),
      body: Center(
        child: OrientationBuilder(
          builder: (context, orientation) {
            // Build UI elements based on device orientation
            return Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Icon(
                  // Display different icons based on orientation
                  orientation == Orientation.portrait
                      ? Icons.phone_android
                      : Icons.phone_iphone,
                  size: 100.0,
                ),
                SizedBox(height: 20.0),
                Text(
                  // Display different text based on orientation
                  'Device is in ${orientation == Orientation.portrait ? 'Portrait' : 'Landscape'} mode',
                  style: TextStyle(fontSize: 20.0),
                ),
              ],
            );
          },
        ),
      ),
    );
  }
}


Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads