A comprehensive Flutter plugin for managing device orientation with real-time orientation change detection.
- π Real-time orientation change detection
- π Lock orientation to portrait or landscape
- π― Toggle between orientations programmatically
- π± Cross-platform support (iOS & Android)
- π Easy to integrate and use
- πͺ Built with native performance
Add this to your package's pubspec.yaml
file:
dependencies:
flutter_orientation_manager: ^1.0.0
No additional setup required for iOS.
No additional setup required for Android.
import 'package:flutter_orientation_manager/flutter_orientation_manager.dart';
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_orientation_manager/flutter_orientation_manager.dart';
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
StreamSubscription<Orientation>? _orientationSubscription;
Orientation _currentOrientation = Orientation.portrait;
@override
void initState() {
super.initState();
// Listen to orientation changes
_orientationSubscription = FlutterOrientationManager.orientationStream.listen((newOrientation) {
setState(() {
_currentOrientation = newOrientation;
});
print('Orientation changed: $newOrientation');
});
}
@override
void dispose() {
_orientationSubscription?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Orientation Manager Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Current Orientation:',
style: TextStyle(fontSize: 18),
),
Text(
_currentOrientation == Orientation.portrait ? 'Portrait' : 'Landscape',
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: FlutterOrientationManager.toggleOrientation,
child: Text('Toggle Orientation'),
),
ElevatedButton(
onPressed: FlutterOrientationManager.lockOrientation,
child: Text('Lock Current Orientation'),
),
ElevatedButton(
onPressed: FlutterOrientationManager.enableAutoRotation,
child: Text('Enable Auto Rotation'),
),
],
),
),
),
);
}
}
// Get current orientation
Orientation current = FlutterOrientationManager.getCurrentOrientation();
// Check if portrait
bool isPortrait = FlutterOrientationManager.isPortrait;
// Check if landscape
bool isLandscape = FlutterOrientationManager.isLandscape;
// Listen to orientation changes
Stream<Orientation> orientationStream = FlutterOrientationManager.orientationStream;
// Toggle between portrait and landscape
FlutterOrientationManager.toggleOrientation();
// Lock current orientation
FlutterOrientationManager.lockOrientation();
// Enable automatic rotation
FlutterOrientationManager.enableAutoRotation();
// Force to portrait
FlutterOrientationManager.resetToPortrait();
// Force to landscape
FlutterOrientationManager.forceToLandscape();
See the example directory for a complete sample app demonstrating all features.
isPortrait
βbool
: Returns true if device is in portrait modeisLandscape
βbool
: Returns true if device is in landscape modeorientationStream
βStream<Orientation>
: Stream of orientation changes
getCurrentOrientation()
βOrientation
: Get current device orientationtoggleOrientation()
βvoid
: Toggle between portrait and landscapelockOrientation()
βvoid
: Lock the current orientationenableAutoRotation()
βvoid
: Enable automatic rotationresetToPortrait()
βvoid
: Force device to portrait modeforceToLandscape()
βvoid
: Force device to landscape mode
Platform | Support |
---|---|
Android | β |
iOS | β |
Web | β |
macOS | β |
Windows | β |
Linux | β |
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
If you like this package, please give it a β on GitHub and pub.dev!