Skip to content

letrungdo/flutter_orientation_manager

Repository files navigation

Flutter Orientation Manager

A comprehensive Flutter plugin for managing device orientation with real-time orientation change detection.

Features

  • πŸ”„ 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

Getting Started

Installation

Add this to your package's pubspec.yaml file:

dependencies:
  flutter_orientation_manager: ^1.0.0

iOS Setup

No additional setup required for iOS.

Android Setup

No additional setup required for Android.

Usage

Import the package

import 'package:flutter_orientation_manager/flutter_orientation_manager.dart';

Listen to orientation changes

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'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Available Methods

Orientation Detection

// 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;

Orientation Control

// 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();

Example

See the example directory for a complete sample app demonstrating all features.

API Reference

FlutterOrientationManager

Static Properties

  • isPortrait β†’ bool: Returns true if device is in portrait mode
  • isLandscape β†’ bool: Returns true if device is in landscape mode
  • orientationStream β†’ Stream<Orientation>: Stream of orientation changes

Static Methods

  • getCurrentOrientation() β†’ Orientation: Get current device orientation
  • toggleOrientation() β†’ void: Toggle between portrait and landscape
  • lockOrientation() β†’ void: Lock the current orientation
  • enableAutoRotation() β†’ void: Enable automatic rotation
  • resetToPortrait() β†’ void: Force device to portrait mode
  • forceToLandscape() β†’ void: Force device to landscape mode

Platform Support

Platform Support
Android βœ…
iOS βœ…
Web ❌
macOS ❌
Windows ❌
Linux ❌

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

If you like this package, please give it a ⭐ on GitHub and pub.dev!

About

A comprehensive Flutter plugin for managing device orientation with real-time orientation change detection.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published