flutter_orientation_manager 1.0.0 copy "flutter_orientation_manager: ^1.0.0" to clipboard
flutter_orientation_manager: ^1.0.0 copied to clipboard

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

example/lib/main.dart

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_orientation_manager/flutter_orientation_manager.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Orientation Manager Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Orientation Manager Demo'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  StreamSubscription<Orientation>? _orientationSubscription;
  Orientation _currentOrientation = Orientation.portrait;
  final List<String> _orientationHistory = [];

  @override
  void initState() {
    super.initState();
    _initializeOrientationManager();
  }

  void _initializeOrientationManager() {
    // Get initial orientation
    _currentOrientation = FlutterOrientationManager.getCurrentOrientation();
    _addToHistory('Initial: ${_getOrientationName(_currentOrientation)}');

    // Listen to orientation changes
    _orientationSubscription = FlutterOrientationManager.orientationStream.listen(
      (newOrientation) {
        setState(() {
          _currentOrientation = newOrientation;
        });
        _addToHistory('Changed to: ${_getOrientationName(newOrientation)}');
        _showOrientationSnackBar(newOrientation);
      },
      onError: (error) {
        _addToHistory('Error: $error');
      },
    );
  }

  void _addToHistory(String event) {
    setState(() {
      _orientationHistory.insert(0, '${DateTime.now().toLocal().toString().substring(11, 19)}: $event');
      if (_orientationHistory.length > 10) {
        _orientationHistory.removeLast();
      }
    });
  }

  void _showOrientationSnackBar(Orientation orientation) {
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        content: Text('Orientation changed to ${_getOrientationName(orientation)}'),
        duration: const Duration(seconds: 1),
        behavior: SnackBarBehavior.floating,
      ),
    );
  }

  String _getOrientationName(Orientation orientation) {
    return orientation == Orientation.portrait ? 'Portrait' : 'Landscape';
  }

  @override
  void dispose() {
    _orientationSubscription?.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
        elevation: 2,
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            // Current Status Card
            Card(
              elevation: 4,
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: Column(
                  children: [
                    const Text(
                      'Current Status',
                      style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
                    ),
                    const SizedBox(height: 8),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: [
                        Column(
                          children: [
                            Icon(
                              _currentOrientation == Orientation.portrait
                                  ? Icons.stay_current_portrait
                                  : Icons.stay_current_landscape,
                              size: 40,
                              color: Theme.of(context).primaryColor,
                            ),
                            const SizedBox(height: 4),
                            Text(
                              _getOrientationName(_currentOrientation),
                              style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
                            ),
                          ],
                        ),
                        Column(
                          children: [
                            Icon(
                              FlutterOrientationManager.isPortrait
                                  ? Icons.portrait
                                  : Icons.landscape,
                              size: 40,
                              color: Colors.grey[600],
                            ),
                            const SizedBox(height: 4),
                            Text(
                              'Is ${FlutterOrientationManager.isPortrait ? 'Portrait' : 'Landscape'}',
                              style: TextStyle(fontSize: 14, color: Colors.grey[600]),
                            ),
                          ],
                        ),
                      ],
                    ),
                  ],
                ),
              ),
            ),
            const SizedBox(height: 16),
            
            // Control Buttons
            const Text(
              'Orientation Controls',
              style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
            ),
            const SizedBox(height: 8),
            Wrap(
              spacing: 8,
              runSpacing: 8,
              children: [
                ElevatedButton.icon(
                  onPressed: () {
                    FlutterOrientationManager.toggleOrientation();
                    _addToHistory('Action: Toggle orientation');
                  },
                  icon: const Icon(Icons.screen_rotation),
                  label: const Text('Toggle'),
                ),
                ElevatedButton.icon(
                  onPressed: () {
                    FlutterOrientationManager.lockOrientation();
                    _addToHistory('Action: Lock current orientation');
                  },
                  icon: const Icon(Icons.screen_lock_rotation),
                  label: const Text('Lock Current'),
                ),
                ElevatedButton.icon(
                  onPressed: () {
                    FlutterOrientationManager.enableAutoRotation();
                    _addToHistory('Action: Enable auto rotation');
                  },
                  icon: const Icon(Icons.screen_rotation_outlined),
                  label: const Text('Auto Rotate'),
                ),
                ElevatedButton.icon(
                  onPressed: () {
                    FlutterOrientationManager.resetToPortrait();
                    _addToHistory('Action: Force portrait');
                  },
                  icon: const Icon(Icons.stay_current_portrait),
                  label: const Text('Portrait'),
                ),
                ElevatedButton.icon(
                  onPressed: () {
                    FlutterOrientationManager.forceToLandscape();
                    _addToHistory('Action: Force landscape');
                  },
                  icon: const Icon(Icons.stay_current_landscape),
                  label: const Text('Landscape'),
                ),
              ],
            ),
            const SizedBox(height: 16),
            
            // History Section
            const Text(
              'Event History',
              style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
            ),
            const SizedBox(height: 8),
            Expanded(
              child: Card(
                elevation: 2,
                child: _orientationHistory.isEmpty
                    ? const Center(
                        child: Text(
                          'No events yet...',
                          style: TextStyle(fontSize: 16, color: Colors.grey),
                        ),
                      )
                    : ListView.builder(
                        padding: const EdgeInsets.all(8),
                        itemCount: _orientationHistory.length,
                        itemBuilder: (context, index) {
                          return Padding(
                            padding: const EdgeInsets.symmetric(vertical: 2),
                            child: Text(
                              _orientationHistory[index],
                              style: const TextStyle(fontSize: 14, fontFamily: 'monospace'),
                            ),
                          );
                        },
                      ),
              ),
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            _orientationHistory.clear();
          });
          _addToHistory('History cleared');
        },
        tooltip: 'Clear history',
        child: const Icon(Icons.clear),
      ),
    );
  }
}
0
likes
0
points
272
downloads

Publisher

unverified uploader

Weekly Downloads

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

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on flutter_orientation_manager

Packages that implement flutter_orientation_manager