How to set Flutter Orientation Portrait Or Landscape Only



If you want to set Fixed Flutter Orientation for your application You need import first service.

import 'package:flutter/services.dart';

Code Example:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await SystemChrome.setPreferredOrientations(
    [DeviceOrientation.portraitUp],
  );
runApp(MyApp());
}
Flutter Orientations

Some Value For Device Orientation :

 [DeviceOrientation.portraitUp],
 [DeviceOrientation.portraitDown],
 [DeviceOrientation.landscapeLeft],
 [DeviceOrientation.landscapeRight],

Orientation Project Code Example:

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await SystemChrome.setPreferredOrientations(
    [DeviceOrientation.landscapeRight],
  );
runApp(MyApp());
}
class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  _MyAppState createState() => _MyAppState();
}
class _MyAppState extends State {
  bool _myFirst = true;
  double _myFontSize = 60;
  Color _myColor = Colors.blue;
  FontWeight _myfontWeight = FontWeight.bold;
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: ""MyTestApp"",
      home: Scaffold(
        body: Center(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Container(
                height: 120,
                child: AnimatedDefaultTextStyle(
                  duration: const Duration(milliseconds: 300),
                  style: TextStyle(
                    fontSize: _myFontSize,
                    color: _myColor,
                    fontWeight: _myfontWeight,
                  ),
                  child: Text('Animated'),
                ),
              ),
              FlatButton(
                onPressed: () {
                  setState(() {
                    _myFontSize = _myFirst ? 70 : 50;
                    _myColor = _myFirst ? Colors.blue : Colors.red;
                    _myfontWeight = _myFirst ? FontWeight.bold : FontWeight.w100;
                    _myFirst = !_myFirst;
                  });
                },
                child: Text(
                  ""SHOW!"",
                ),
              )
            ],
          ),
        ),
      )
    );
  }
}

Flutter Orientation Portrait Or Landscape Only

Leave a Comment