auto_orientation_v2 2.4.4
auto_orientation_v2: ^2.4.4 copied to clipboard
Set and control device orientation programmatically for Flutter apps on iOS and Android.
auto_orientation_v2 #
A Flutter plugin to programmatically control device orientation on iOS and Android.
This package is an improved version of the original:
➡️ https://pub.dev/packages/auto_orientation
🚀 Features #
- Instantly switch between Portrait, Landscape, and Auto modes.
- No need to manually call
SystemChrome.setPreferredOrientations. - Supports forceSensor on Android (overrides user's rotation settings, similar to YouTube fullscreen).
- Includes declarative
AutoOrientationScopewidget to auto-lock and revert orientation. - Includes orientation helper getters (
isLandscape,isPortrait). - Ideal for video players, games, reading apps, and custom UI scenarios.
📦 Installation #
Add this to your pubspec.yaml:
dependencies:
auto_orientation_v2: ^2.4.0
Import the package:
import 'package:auto_orientation_v2/auto_orientation_v2.dart';
📲 Usage #
🎞 Landscape Modes #
AutoOrientation.landscapeLeftMode();
AutoOrientation.landscapeRightMode();
📱 Portrait Modes #
AutoOrientation.portraitUpMode();
AutoOrientation.portraitDownMode(); // may not work on some devices
🧭 Auto Modes (Android Only) #
Follow device/user rotation preferences:
AutoOrientation.portraitAutoMode();
AutoOrientation.landscapeAutoMode();
Force sensor-based orientation (ignore user rotation preference):
AutoOrientation.portraitAutoMode(forceSensor: true);
AutoOrientation.landscapeAutoMode(forceSensor: true);
🔄 Full Auto Mode #
Allow all orientations:
AutoOrientation.fullAutoMode();
🛠️ New: Enum-based API (Recommended) #
Since version 2.3.8, you can use the setOrientation method with the AutoOrientationMode enum for a cleaner syntax.
import 'package:auto_orientation_v2/auto_orientation_v2.dart';
// Switch to landscape
AutoOrientation.setOrientation(AutoOrientationMode.landscapeRight);
// Switch to portrait
AutoOrientation.setOrientation(AutoOrientationMode.portraitUp);
// Use auto mode with force sensor (Android)
AutoOrientation.setOrientation(
AutoOrientationMode.landscapeAuto,
forceSensor: true,
);
// Reset to user/system default
AutoOrientation.setOrientation(AutoOrientationMode.user);
🛡️ AutoOrientationScope (Declarative Widget) #
Automatically lock orientation when entering a page and restore orientation when leaving:
@override
Widget build(BuildContext context) {
return AutoOrientationScope(
targetMode: AutoOrientationMode.landscapeRight,
onDisposeMode: AutoOrientationMode.portraitUp,
child: Scaffold(
body: VideoPlayerWidget(),
),
);
}
🔍 Orientation Helpers #
Utility methods to check the current device orientation:
if (AutoOrientation.isLandscape(context)) {
// Render landscape UI
}
if (AutoOrientation.isPortrait(context)) {
// Render portrait UI
}
Orientation current = AutoOrientation.currentOrientation(context);
❗ Important Notes #
- Do NOT call
SystemChrome.setPreferredOrientationsmanually. The plugin manages orientation internally to avoid Android auto-rotation issues.
📘 Example #
A common use case: Flutter video player fullscreen.
@override
void initState() {
super.initState();
AutoOrientation.landscapeRightMode(); // switch to fullscreen landscape
}
@override
void dispose() {
AutoOrientation.portraitUpMode(); // revert to portrait when closed
super.dispose();
}
More examples are available inside the example/ folder.
📄 License #
MIT License © 2025