set device orientation flutter code example

Example 1: flutter set orientation

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  // We need to call it manually,
  // because we going to call setPreferredOrientations()
  // before the runApp() call
  WidgetsFlutterBinding.ensureInitialized();
  
  // Than we setup preferred orientations,
  // and only after it finished we run our app
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
      .then((value) => runApp(MyApp()));
}

Example 2: flutter display widget based on device orientation

build(BuildContext context) {
    final isLandscape = MediaQuery.of(context).orientation ==
        Orientation.landscape; // check if the orientation is landscape
        
        
        // further down your code you can use 'if' or 'ternary' or both
        if (!isLandscape) txListWidget,
            if (isLandscape)
            _ternaryCondition ? _showFirstWidget : _showSecondWidget
            // the above widgets would be in a 'Row' or 'Column'
        }

Tags:

Dart Example