Restrict Landscape mode in Flutter

 Our app is designed to run in portrait mode; if we switch to landscape mode, the user interface might not change accordingly. There are two scenarios: either your programme can be configured to run in landscape mode, or it may not need to be in landscape mode. Thus, we can limit the landscape orientation in the second scenario of Mobile application.

Syntax

 SystemChrome.setPreferredOrientations([DeviceOrientation])

List of Orientations 

  • landscapeLeft
  • portraitDown
  • landscapeRight
  • portraitUp

Step-by-Step Implementation

Step 1: Create a New Project in Android Studio (File >new flutter project)


Step 2: Adding material package

Import method the runApp method in the main function call first while run the application.

 import 'package:flutter/material.dart';

  void main() {

      runApp(RunMyApp());

  }

Step 3: Final code 
 import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp]);
runApp(HomeScreen());
}

class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Text('Rotated to Landscape mode',
style: TextStyle(color: Colors.blue,
fontSize: 17),
),
),
),
);
}
}
Step 4: Output of above example


Happy coding!

365Bloggy June 4, 2024
Share this post
Tags
SUBSCRIBE THIS FORM


Archive