Управление ориентацией экрана во Flutter: методы и примеры кода

  1. Использование пакета orientation:

    import 'package:orientation/orientation.dart';
    // Lock the screen orientation to portrait mode
    void lockOrientationPortrait() {
    Orientation.lockPortrait();
    }
    // Unlock the screen orientation
    void unlockOrientation() {
    Orientation.unlock();
    }
    // Lock the screen orientation to landscape mode
    void lockOrientationLandscape() {
    Orientation.lockLandscape();
    }
    // Lock the screen orientation to landscape mode, left only
    void lockOrientationLandscapeLeft() {
    Orientation.lockLandscapeLeft();
    }
    // Lock the screen orientation to landscape mode, right only
    void lockOrientationLandscapeRight() {
    Orientation.lockLandscapeRight();
    }
  2. Использование класса SystemChrome:

    import 'package:flutter/services.dart';
    // Lock the screen orientation to portrait mode
    void lockOrientationPortrait() {
    SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown,
    ]);
    }
    // Unlock the screen orientation
    void unlockOrientation() {
    SystemChrome.setPreferredOrientations([]);
    }
    // Lock the screen orientation to landscape mode
    void lockOrientationLandscape() {
    SystemChrome.setPreferredOrientations([
    DeviceOrientation.landscapeLeft,
    DeviceOrientation.landscapeRight,
    ]);
    }
  3. Использование пакета screen:

    import 'package:screen/screen.dart';
    // Lock the screen orientation to portrait mode
    void lockOrientationPortrait() {
    Screen.keepOn(true);
    Screen.setOrientation(DeviceOrientation.portraitUp);
    }
    // Unlock the screen orientation
    void unlockOrientation() {
    Screen.keepOn(false);
    }
    // Lock the screen orientation to landscape mode
    void lockOrientationLandscape() {
    Screen.keepOn(true);
    Screen.setOrientation(DeviceOrientation.landscapeLeft);
    }

Обратите внимание, что в приведенных примерах кода предполагается, что вы уже импортировали необходимые пакеты в свой проект Flutter.