This website will be permanently discontinued and will no longer be accessible.
FINAL DAY OF SERVICE: 26 November 2024
Thank You for Your Support
Dear valued users,
We want to inform you that this website will be permanently shutting down. After this date, all services and content will no longer be accessible.
We sincerely thank you for your support and trust throughout our journey.
Important Dates:
Last Day: 26/11/2024
Data Access:Until the last day
Dear valued users,
We want to inform you that this website will be permanently shutting down. After this date, all services and content will no longer be accessible.
We sincerely thank you for your support and trust throughout our journey. Important Dates: Last Day: 26/11/2024 Data Access:Until the last day
Flutter Change or Lock Device Orientation – Complete Tutorial
Device orientation is the device’s app UI position with respect to gravity. It detects the physical orientation of the device and shows the responsive user interface to the user.
Today you will learn how to change the device orientation behavior in Flutter. You can also lock the device orientation so that if the user rotates the physical device the orientation will remain locked as we set in the flutter code.
In this Article
Lock Flutter App Orientation on All Pages
You can lock a flutter app device orientation by following three steps.
Step 1: Import flutter services
Step 2: Ensure initialization of flutter bindings
Step 3: Set preferred orientation (Main Step)
Step 4: Rebuild the flutter project
Step 1: Import flutter services
You will import the flutter services file which exposes the platform services to the flutter app.
import 'package:flutter/services.dart';
Step 2: Ensure initialization of flutter bindings
Now you will ensure the initialization of flutter widget bindings. Remember you will initialize the bindings before the runApp().
Code snippet:
WidgetsFlutterBinding.ensureInitialized();
You will initialize the flutter widget binding in the main function of the app.
These enums value list you can pass to the setPreferredOreintation(), so that when you rotate the physical device you can only preview the result that you specify in the list.
For Example:
If you want to show your App UI only Vertical Portrait position, so you can pass DeviceOrientation.portraitUp from the list to the setPreferredOrientations() method.
As you already study about the flutter SystemChrome.setPreferredOrientations() service which will change the device orientation according to the specified settings.
Dispose() Can be Super Useful
Good thing is that you can also reset the device orientation settings to the default.
Know you want to change device orientation programmatically which means that when certain event trigger your UI orientation will behave certain way.
How you can add this on demand UI resposiveness in your flutter apps.
Change Device Orientation Programmatically
Now you will create an example app which has two buttons (Portrait, Landscape) at the center when you press portrait button device orientation will change to portrait and when you press landscape device orientation will become landscape.
Now inside the elevated buttons you can use the onPressed property. Here you will use the SystemChrome.setPreferredOrientations() property to set the orientation of the screen on the buttons pressed.
You just explored how you can change or lock the Orientation of the App by changing Physical device position. You learn different approaches to solve this problem.