Flutter - Managing Device Orientation
Last Updated :
02 Jun, 2022
All the applications developers create in flutter run on multiple devices with a variety of screen pixels and sizes. Managing screen orientation in flutter plays an important role in establishing a successful app that runs on multiple devices. In flutter, you can pick the best way to deal with such challenges of device orientation in an easy and handy way. In this article, we will walk you through Screen Orientation in Flutter.
What is the need for orientation?
To learn orientation management, first, you need to understand the need to do so. Screen orientation management improves the user experience to a great extent. When you change the screen orientation, the action currently running over the app changes and re-builds itself according to the new orientation. For example, Changing orientation to landscape mode while playing a car racing game.
How to set device orientation?
Flutter provides namely 2 ways to orient the device namely:
- Portrait
- LandScape
Given below are the steps illustrated to change your mobile application orientation according to your suitability.
Step 1: Include the services packages in your main file.
Step 2: Call "WidgetsFlutterBinding.ensureInitialized()" method after the main function call.
Step 3: Call " SystemChrome.setPreferredOrientations" after you "WidgetsFlutterBinding.ensureInitialized()" method.
Now to set a portrait mode, flutter provides two orientation options namely:
- DeviceOrientation.portraitUp,
- DeviceOrientation.portraitDown,
Sample Code
void main() {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
runApp(HomeApp());
}
To set landscape mode, flutter provides two orientation options:
- DeviceOrientation.landscapeLeft
- DeviceOrientation.landscapeRight
void main() {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeLeft,
DeviceOrientation.landscapeRight
]);
runApp(HomeApp());
}
How to manage Device Orientation?
Step 1:
Open the screen on which you want to manage the orientation.
Step 2:
Add an init() method and call SystemChrome.setPreferredOrientations in the mode you want to apply.
Step 3:
Add a dispose() method and call SystemChrome.setPreferredOrientations in the mode you want to remove.
Let's look at the steps closely with the help of examples
Example 1: To set the device orientation to Landscape Mode
Dart
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(HomeApp());
}
class HomeApp extends StatefulWidget {
HomeApp({Key? key}) : super(key: key);
@override
State<HomeApp> createState() => _HomeAppState();
}
class _HomeAppState extends State<HomeApp> {
void initState() {
super.initState();
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeLeft,
DeviceOrientation.landscapeRight,
]);
}
@override
dispose() {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
super.dispose();
}
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
backgroundColor: Colors.green,
title: const Text('GeeksforGeeks'),
),
body: const FirstScreen()));
}
}
class FirstScreen extends StatelessWidget {
const FirstScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
child: Center(
child: ElevatedButton(
child: Text('Click here'),
style: OutlinedButton.styleFrom(
backgroundColor: Colors.green,
primary: Colors.black,
textStyle: TextStyle(fontSize: 15, fontStyle: FontStyle.normal),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(10)))),
onPressed: () => Navigator.of(context)
.push(MaterialPageRoute(builder: (context) => const NewScreen())),
),
),
);
}
}
class NewScreen extends StatefulWidget {
const NewScreen({Key? key}) : super(key: key);
@override
State<NewScreen> createState() => _NewScreenState();
}
class _NewScreenState extends State<NewScreen> {
TextEditingController textEditingController = TextEditingController();
@override
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.green,
title: const Text('New Screen'),
),
body: Center(child: Text('You shifted to Landscape mode')),
);
}
}
Output:
Example 2: To Set the device orientation in Portrait mode
Dart
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(HomeApp());
}
class HomeApp extends StatefulWidget {
HomeApp({Key? key}) : super(key: key);
@override
State<HomeApp> createState() => _HomeAppState();
}
class _HomeAppState extends State<HomeApp> {
void initState() {
super.initState();
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitDown,
DeviceOrientation.portraitUp,
]);
}
@override
dispose() {
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeRight,
DeviceOrientation.landscapeLeft,
]);
super.dispose();
}
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
backgroundColor: Colors.green,
title: const Text('GeeksforGeeks'),
),
body: const FirstScreen()));
}
}
class FirstScreen extends StatelessWidget {
const FirstScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
child: Center(
child: ElevatedButton(
child: Text('Click here'),
style: OutlinedButton.styleFrom(
backgroundColor: Colors.green,
primary: Colors.black,
textStyle: TextStyle(fontSize: 15, fontStyle: FontStyle.normal),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(10)))),
onPressed: () => Navigator.of(context)
.push(MaterialPageRoute(builder: (context) => const NewScreen())),
),
),
);
}
}
class NewScreen extends StatefulWidget {
const NewScreen({Key? key}) : super(key: key);
@override
State<NewScreen> createState() => _NewScreenState();
}
class _NewScreenState extends State<NewScreen> {
TextEditingController textEditingController = TextEditingController();
@override
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.green,
title: const Text('New Screen'),
),
body: Center(child: Text('You shifted to landscape mode')),
);
}
}
Output:
Similar Reads
Flutter - Animate Image Rotation
In Flutter, Image.assets() or Image.network() Widget is used to display images from locally or from the URL. Images can be locally stored in the program or fetched from a network and can be displayed using the Image Widget. Animations can be applied to Images via many techniques. Hence In this artic
4 min read
Flutter - UI Orientation
All applications should be able to adjust their User Interface (UI) based on the orientation of the phone. By orientation, we implicate the portrait and landscape mode in smartphones rather the physical orientation. In, Flutter it is done so by using the OrientationBuilder, which determines the app'
2 min read
Flutter - Navigation Drawer
The mobile applications that use Material Designs have two primary options for navigation. These are namely the 'Tabs' and the 'Drawers'. This article will primarily focus on Drawers. A drawer is used to provide access to different destinations and functionalities provided in your application. It is
4 min read
Flutter - Get the Unique Device ID
Flutter is a UI toolkit developed and maintained by Google. It was released in May 2017 to the public. It is now one of the most popular cross-platform development frameworks among beginners and experienced application developers. It has a straightforward learning curve and uses the Dart programming
2 min read
Flutter - State Management Provider
In this article, we are going to learn how state management is achieved in Flutter using providers. But before that, we need to know what a state is. As we know that everything in Flutter is a widget, and there are mainly two kinds of widgets: Stateless Widgets and Stateful Widgets. Stateless widget
8 min read
Application Localization in Flutter
In mobile applications, Localization is the process of adapting an application to support multiple languages and regional settings. This allows your app to be more accessible to users from different parts of the world. In Flutter, for implementing Localization flutter_localization is a package used
4 min read
Flutter - Grouping Navigation Drawer Menu Items
Grouping navigation drawer menu items involves organizing the items into sections or categories. This makes it easier for users to navigate through the app and find the options that the user wants. Grouping Drawer Menu Items, In Flutter, we can group navigation drawer menu items using the ExpansionT
5 min read
Flutter - Material Design
If someone wants to quickly build beautiful, scalable apps across platforms? Then Material Design is the way to go. What is Material design? Material is an adaptable design system, backed by open-source code, that helps developers easily build high-quality, digital experiences. From design guideli
7 min read
How to Get MAC Address of Device in Flutter?
Flutter SDK is an open-source software development kit by Google to develop applications for multiple platforms from a single codebase. Sometimes, your app needs to know the MAC address of the device. MAC (Media Access Control) address is the unique identifier of a device on a LAN network. Approach:
2 min read
State management using Redux in Flutter
State management is a crucial aspect of building dynamic and responsive applications. While building applications, we sometimes need to maintain a state between multiple screens. Here comes the part of State Management. There are different state management techniques in Flutter like GetX, Provider,
6 min read