Flutter: Show different content based on device orientation
(240 Articles)
In Flutter, you can show different content based on the device orientation. To determine the device orientation, just use:
MediaQuery.of(context).orientation
Sample Code:
body: MediaQuery.of(context).orientation == Orientation.landscape
?
// Landscape
Text(
'Your phone is on landscape mode',
style: TextStyle(fontSize: 30, color: Colors.purple),
)
:
// Portrait
Text(
'Your phone is on portrait mode',
style: TextStyle(fontSize: 18, color: Colors.red),
)
Screenshots:
The complete code in main.dart:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
// Remove the debug banner
debugShowCheckedModeBanner: false,
title: "Kindacode.com",
home: HomePage(),
);
}
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Kindacode.com'),
),
body: MediaQuery.of(context).orientation == Orientation.landscape
?
// Landscape
Text(
'Your phone is on landscape mode',
style: TextStyle(fontSize: 30, color: Colors.purple),
)
:
// Portrait
Text(
'Your phone is on portrait mode',
style: TextStyle(fontSize: 18, color: Colors.red),
));
}
}
Related Articles
0 Comments