Flutter: How to set and lock screen orientation on-demand

Cover Image for Flutter: How to set and lock screen orientation on-demand
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

πŸ“±πŸ’»πŸ”’Flutter: How to set and lock screen orientation on-demand πŸ”πŸ”πŸ”

Are you a Flutter developer in need of controlling screen orientation? πŸ“±πŸ’» Look no further! In this guide, we will explore how to set and lock the screen orientation on-demand, specifically for one page in your Flutter app. Let's dive in! πŸŠβ€β™‚οΈπŸ’¦

The Challenge: Setting and Locking the Screen Orientation for a Single Page

One of our fellow Flutter developers was faced with an interesting problem. They needed to set their screen to landscape mode and lock it, so it couldn't rotate back into portrait mode. However, this change was only required for a single page within their app. πŸ˜“πŸ’‘

The Solution: Enabling On-The-Fly Orientation Control

After a bit of digging and experimentation, our intrepid developer found the perfect solution! Here's how you can achieve the same result in your Flutter app:

  1. Import the necessary package: Start by adding the flutter/services package to your app's dependencies in the pubspec.yaml file. This package allows you to access platform-specific services, including screen orientation control. πŸ“¦πŸ‘¨β€πŸ’»

dependencies:
  flutter:
    sdk: flutter
  flutter/services.dart // import the services package
  1. Writing orientation control logic: Navigate to the page where you want to set and lock the screen's orientation. In this example, let's call it LandscapePage. In the initState method of the widget, add the following code:

import 'package:flutter/services.dart';

class LandscapePage extends StatefulWidget {
  @override
  _LandscapePageState createState() => _LandscapePageState();
}

class _LandscapePageState extends State<LandscapePage> {
  @override
  void initState() {
    super.initState();
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.landscapeLeft,
      DeviceOrientation.landscapeRight,
    ]);
  }

  @override
  void dispose() {
    super.dispose();
    SystemChrome.setPreferredOrientations([]); // Reset orientation preferences
  }

  // Rest of the widget code...
}

Let's walk through the code. In the initState method, we use SystemChrome.setPreferredOrientations to set the preferred screen orientations as landscapeLeft and landscapeRight. This ensures that the device will only rotate between these two orientations while on this page. πŸ‘

  1. Reverting the changes: It's essential to clean up after ourselves, right? We don't want to mess up the orientation preferences for the entire app when navigating away from LandscapePage. In the dispose method of the widget, we call SystemChrome.setPreferredOrientations([]) to reset the orientation preferences. This ensures that the app reverts to its default behavior after leaving this page. πŸ”„πŸš€

Put it to the Test! πŸ§ͺ

Now that you have the necessary code, it's time to put it to the test! Run your Flutter app, navigate to the LandscapePage, and voila! Your screen should lock in landscape mode, preventing any annoyance of accidental portrait rotations. ✨πŸ’ͺ

Conclusion: Take Control of Your Screen Orientation!

Controlling screen orientation in Flutter doesn't have to be a headache! With the help of the flutter/services package and a little bit of code, you can set and lock the screen orientation on-demand for a single page. πŸ’‘πŸ”’

Don't forget to experiment and explore different possibilities. Maybe there's a need to set up a different orientation for another page in your app? The possibilities are endless! πŸ˜„

Now, it's your turn! Share your thoughts, experiences, and any questions you may have in the comments below. Are there any other Flutter topics you'd like us to cover? Let us know! πŸ—£οΈπŸ“’

Happy coding, Flutteristas! βœοΈπŸš€πŸ’™


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

πŸ”₯ πŸ’» πŸ†’ Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! πŸš€ Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings πŸ’₯βœ‚οΈ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide πŸš€ So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? πŸ€” Well, my

Matheus Mello
Matheus Mello