iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🤸‍♂️

How to change and simulate screen orientation in Flutter

に公開

TL;DR

Setting the Screen Orientation

import 'package:flutter/services.dart';  // Required

// Something like this. Change `_landscape` wherever you need to.
// I used a switch case instead of a ternary operator because I thought it might be easier to read as a sample, though I'm not entirely sure if it is...
void changeLandscape() {
  switch (_landscape) {
    case false:
      SystemChrome.setPreferredOrientations(
          [DeviceOrientation.portraitDown, DeviceOrientation.portraitUp]);
      break;
    case true:
      // Use this when you want the right side to be the top.
      // If you want the left side to be the top, specify DeviceOrientation.landscapeLeft.
      SystemChrome.setPreferredOrientations(
          [DeviceOrientation.landscapeRight]);
      break;
  }
}

Fixing the Screen Orientation

Rotating with RotatedBox

import 'package:flutter/material.dart';

...omitted
// Something like this. It's quite straightforward...
// Put your required Widget in YourWidget
// Change `_landscape` wherever you need to.
@override
Widget build(BuildContext context) {
  return RotatedBox(
            child: YourWidget(
              ),
            quarterTurns: _landscape ? 1 : 0);
...omitted

Discussion