### Install switch_orientation Plugin in pubspec.yaml Source: https://context7.com/linxunfeng/flutter_switch_orientation/llms.txt Add the switch_orientation plugin to your Flutter project's dependencies in the pubspec.yaml file. This ensures the plugin is available for use in your application. ```yaml name: my_app data: A Flutter application with orientation control environment: sdk: '>=3.0.0 <4.0.0' flutter: '>=3.7.0' dependencies: flutter: sdk: flutter switch_orientation: ^0.0.1 dev_dependencies: flutter_test: sdk: flutter flutter_lints: ^3.0.0 ``` -------------------------------- ### Add Dependency to pubspec.yaml Source: https://github.com/linxunfeng/flutter_switch_orientation/blob/main/README.md To use the switch_orientation plugin, add it as a dependency in your Flutter project's pubspec.yaml file. Ensure you use the latest version available. ```yaml dependencies: switch_orientation: latest_version ``` -------------------------------- ### Import switch_orientation Plugin in Dart Source: https://github.com/linxunfeng/flutter_switch_orientation/blob/main/README.md After adding the dependency, import the `switch_orientation` package into your Dart files where you intend to use its functionalities. This makes the plugin's classes and methods accessible. ```dart import 'package:switch_orientation/switch_orientation.dart'; ``` -------------------------------- ### Configure iOS AppDelegate for Dynamic Orientations Source: https://context7.com/linxunfeng/flutter_switch_orientation/llms.txt Configures the iOS AppDelegate to enable dynamic orientation changes for the switch_orientation plugin. This involves overriding the application(_:supportedInterfaceOrientationsFor:) method to return the current view controller's orientation mask, ensuring correct orientation handling on iOS devices. Requires importing 'UIKit', 'Flutter', and 'LXFProtocolTool'. ```swift import UIKit import Flutter import LXFProtocolTool @UIApplicationMain @objc class AppDelegate: FlutterAppDelegate { override func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool { GeneratedPluginRegistrant.register(with: self) return super.application(application, didFinishLaunchingWithOptions: launchOptions) } // Required: Override to support dynamic orientation changes override func application( _ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow? ) -> UIInterfaceOrientationMask { return UIApplication.shared.lxf.currentVcOrientationMask } } ``` -------------------------------- ### Set Preferred Device Orientations in Flutter Source: https://context7.com/linxunfeng/flutter_switch_orientation/llms.txt Dynamically sets the device's preferred screen orientations at runtime using a list of DeviceOrientation values. On iOS, it communicates with native code via Pigeon-generated APIs, while on Android, it delegates to Flutter's SystemChrome.setPreferredOrientations. Requires importing 'package:flutter/material.dart', 'package:flutter/services.dart', and 'package:switch_orientation/switch_orientation.dart'. ```dart import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:switch_orientation/switch_orientation.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: const Text('Orientation Control')), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ ElevatedButton( onPressed: () async { // Lock to portrait mode only await SwitchOrientation.setPreferredOrientations([ DeviceOrientation.portraitUp, DeviceOrientation.portraitDown, ]); }, child: const Text('Lock to Portrait'), ), ElevatedButton( onPressed: () async { // Lock to landscape mode only await SwitchOrientation.setPreferredOrientations([ DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight, ]); }, child: const Text('Lock to Landscape'), ), ElevatedButton( onPressed: () async { // Allow all orientations await SwitchOrientation.setPreferredOrientations([ DeviceOrientation.portraitUp, DeviceOrientation.portraitDown, DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight, ]); }, child: const Text('Allow All Orientations'), ), ], ), ), ), ); } } ``` -------------------------------- ### Configure iOS Orientation in AppDelegate Source: https://github.com/linxunfeng/flutter_switch_orientation/blob/main/README.md For iOS applications, configure the device orientation by modifying the `AppDelegate.swift` file. This involves returning the current device orientation mask provided by the plugin. ```swift import LXFProtocolTool func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask { return UIApplication.shared.lxf.currentVcOrientationMask } ``` -------------------------------- ### Manage Screen Orientation Based on Route in Flutter Source: https://context7.com/linxunfeng/flutter_switch_orientation/llms.txt This Dart code demonstrates how to manage preferred device orientations for different screens in a Flutter application. It uses `SwitchOrientation.setPreferredOrientations` in `initState` to enforce specific orientations (e.g., portrait-only, landscape-only) and resets to all orientations in `dispose`. This pattern is useful for creating route-specific orientation policies. ```dart import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:switch_orientation/switch_orientation.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( home: const HomeScreen(), routes: { '/portrait': (context) => const PortraitOnlyScreen(), '/landscape': (context) => const LandscapeOnlyScreen(), '/any': (context) => const AnyOrientationScreen(), }, ); } } class HomeScreen extends StatelessWidget { const HomeScreen({super.key}); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('Home')), body: ListView( children: [ ListTile( title: const Text('Portrait Only Screen'), onTap: () => Navigator.pushNamed(context, '/portrait'), ), ListTile( title: const Text('Landscape Only Screen'), onTap: () => Navigator.pushNamed(context, '/landscape'), ), ListTile( title: const Text('Any Orientation Screen'), onTap: () => Navigator.pushNamed(context, '/any'), ), ], ), ); } } class PortraitOnlyScreen extends StatefulWidget { const PortraitOnlyScreen({super.key}); @override State createState() => _PortraitOnlyScreenState(); } class _PortraitOnlyScreenState extends State { @override void initState() { super.initState(); SwitchOrientation.setPreferredOrientations([ DeviceOrientation.portraitUp, ]); } @override void dispose() { SwitchOrientation.setPreferredOrientations([ DeviceOrientation.portraitUp, DeviceOrientation.portraitDown, DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight, ]); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('Portrait Only')), body: const Center(child: Text('This screen is locked to portrait')), ); } } class LandscapeOnlyScreen extends StatefulWidget { const LandscapeOnlyScreen({super.key}); @override State createState() => _LandscapeOnlyScreenState(); } class _LandscapeOnlyScreenState extends State { @override void initState() { super.initState(); SwitchOrientation.setPreferredOrientations([ DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight, ]); } @override void dispose() { SwitchOrientation.setPreferredOrientations([ DeviceOrientation.portraitUp, DeviceOrientation.portraitDown, DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight, ]); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('Landscape Only')), body: const Center(child: Text('This screen is locked to landscape')), ); } } class AnyOrientationScreen extends StatefulWidget { const AnyOrientationScreen({super.key}); @override State createState() => _AnyOrientationScreenState(); } class _AnyOrientationScreenState extends State { @override void initState() { super.initState(); SwitchOrientation.setPreferredOrientations([ DeviceOrientation.portraitUp, DeviceOrientation.portraitDown, DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight, ]); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('Any Orientation')), body: const Center(child: Text('This screen supports all orientations')), ); } } ``` -------------------------------- ### Lock Device to Single Orientation (Landscape Left) in Dart Source: https://context7.com/linxunfeng/flutter_switch_orientation/llms.txt Demonstrates how to lock the device to a single orientation, such as landscape left, typically used for video players. It sets the preferred orientation in `initState` and restores all orientations in `dispose`. ```dart import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:switch_orientation/switch_orientation.dart'; class VideoPlayerScreen extends StatefulWidget { const VideoPlayerScreen({super.key}); @override State createState() => _VideoPlayerScreenState(); } class _VideoPlayerScreenState extends State { @override void initState() { super.initState(); // Lock to landscape left when entering video player SwitchOrientation.setPreferredOrientations([ DeviceOrientation.landscapeLeft, ]); } @override void dispose() { // Restore all orientations when leaving SwitchOrientation.setPreferredOrientations([ DeviceOrientation.portraitUp, DeviceOrientation.portraitDown, DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight, ]); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( body: Center( child: Container( color: Colors.black, child: const Center( child: Text( 'Video Player (Landscape Only)', style: TextStyle(color: Colors.white, fontSize: 24), ), ), ), ), ); } } ``` -------------------------------- ### Set Preferred Device Orientations in Dart Source: https://github.com/linxunfeng/flutter_switch_orientation/blob/main/README.md Use the `SwitchOrientation.setPreferredOrientations` method in your Dart code to define the allowed device orientations for your Flutter application. This includes portrait and landscape modes. ```dart SwitchOrientation.setPreferredOrientations([ DeviceOrientation.portraitUp, DeviceOrientation.portraitDown, DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight, ]); ``` -------------------------------- ### Enable Dual Portrait Orientations (Up/Down) in Dart Source: https://context7.com/linxunfeng/flutter_switch_orientation/llms.txt Shows how to enable both portrait up and portrait down orientations, useful for applications like iPads. This allows users to rotate their device 180 degrees in portrait mode. Note: Portrait down may require additional iOS configuration. ```dart import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:switch_orientation/switch_orientation.dart'; class ReadingScreen extends StatefulWidget { const ReadingScreen({super.key}); @override State createState() => _ReadingScreenState(); } class _ReadingScreenState extends State { @override void initState() { super.initState(); // Allow both portrait orientations for reading comfort SwitchOrientation.setPreferredOrientations([ DeviceOrientation.portraitUp, DeviceOrientation.portraitDown, ]); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Reading Mode'), ), body: const Padding( padding: EdgeInsets.all(16.0), child: SingleChildScrollView( child: Text( 'This screen supports both portrait up and portrait down orientations. ' 'You can rotate your device 180 degrees and the content will adapt.', style: TextStyle(fontSize: 18, height: 1.5), ), ), ), ); } } ``` -------------------------------- ### Remove Orientation Restrictions in Flutter Source: https://context7.com/linxunfeng/flutter_switch_orientation/llms.txt This Dart code demonstrates how to remove all orientation restrictions by passing an empty list to `SwitchOrientation.setPreferredOrientations`. This allows the system to manage orientations automatically. It's useful for scenarios where you want to revert to the default behavior or allow all possible orientations. ```dart import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:switch_orientation/switch_orientation.dart'; class SystemControlledScreen extends StatefulWidget { const SystemControlledScreen({super.key}); @override State createState() => _SystemControlledScreenState(); } class _SystemControlledScreenState extends State { bool _isRestricted = false; Future _toggleOrientationControl() async { setState(() { _isRestricted = !_isRestricted; }); if (_isRestricted) { // Restrict to portrait only await SwitchOrientation.setPreferredOrientations([ DeviceOrientation.portraitUp, ]); } else { // Remove all restrictions - let system decide await SwitchOrientation.setPreferredOrientations([]); } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Orientation Control Toggle'), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( _isRestricted ? 'Orientation: Restricted to Portrait' : 'Orientation: System Controlled', style: const TextStyle(fontSize: 18), textAlign: TextAlign.center, ), const SizedBox(height: 20), ElevatedButton( onPressed: _toggleOrientationControl, child: Text(_isRestricted ? 'Remove Restrictions' : 'Restrict to Portrait'), ), ], ), ), ); } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.