How to detect orientation change in layout in Flutter?
DartOrientationFlutterFlutter LayoutDart Problem Overview
How to find out Orientation is portrait or landscape in Flutter
if(portrait){
return ListView.builder()
}else{
return GridView.count()
}
Dart Solutions
Solution 1 - Dart
In order to determine the Orientation of the screen, we can use the OrientationBuilder
Widget. The OrientationBuilder will determine the current Orientation and rebuild when the Orientation changes.
new OrientationBuilder(
builder: (context, orientation) {
return new GridView.count(
// Create a grid with 2 columns in portrait mode, or 3 columns in
// landscape mode.
crossAxisCount: orientation == Orientation.portrait ? 2 : 3,
);
},
);
you can find the complete example here: https://flutter.io/cookbook/design/orientation/
Solution 2 - Dart
You can use MediaQuery
to check orientation:
var isPortrait = MediaQuery.of(context).orientation == Orientation.portrait
Solution 3 - Dart
it's quite easy
if (MediaQuery.of(context).orientation == Orientation.portrait){
// is portrait
}else{
// is landscape
}
Solution 4 - Dart
@override
Widget build(BuildContext context) {
return Scaffold(
body: OrientationBuilder(builder: (_, orientation) {
if (orientation == Orientation.portrait)
return _buildPortraitLayout(); // if orientation is portrait, show your portrait layout
else
return _buildLandscapeLayout(); // else show the landscape one
}),
);
}
Solution 5 - Dart
OrientationBuilder(
builder: (context, orientation) {
return orientation == Orientation.portrait
? SafeArea(
child: Scaffold(
body: PortraitMode(context)
)
)
: LandscapeMode(context);
}
);
Solution 6 - Dart
For completeness sake, I would like to add another way to detect orientation in Flutter. Two ways to detect have been mentioned in the answers already. They are
- Media Query
- Orientation Builder
There is a third way which I came across when learning Flutter from Responsive Design video (skip to minute 2:34) by Google Engineers. It's called Layout Builder
. Here is short snippet:
return Padding(
padding: _padding,
child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
if(constraints.maxHeight > constraints.maxWidth) {
return _getPortraitLayout();
}
else {
return _getLandscapeLayout();
}
},
),
);
Solution 7 - Dart
Mediaquery cannot be used in initState()
and OrientationBuilder needs a widget, So I have created the following class which can be used anywhere in the project.
if(IsPortrait.isPortrait){
}else{
}
IsPortrait.class
class IsPortrait{
static bool isPortrait = true;
void init(BoxConstraints constraints, Orientation orientation) {
if (orientation == Orientation.portrait) {
isPortrait = true;
}
else{
isPortrait = false;
}
}
}
following functions can be used to change the orientation
Portrait Mode Only
/// blocks rotation; sets orientation to: portrait
void _portraitModeOnly() {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
}
Landscape mode only
/// blocks rotation; sets orientation to: landscape
void _landscapeModeOnly() {
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeLeft,
DeviceOrientation.landscapeRight,
]);
}
Enable Portrait and LandScape
void _enableRotation() {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
DeviceOrientation.landscapeLeft,
DeviceOrientation.landscapeRight,
]);
}
Solution 8 - Dart
My variant.
1) In global scope set global ValueNotifier:
final ValueNotifier<bool> IS_PORTRAIT = ValueNotifier<bool>(true);
2) In a scaffold scope change value of our global ValueNotifier
return Scaffold(
key: scaffoldKey,
body: OrientationBuilder(
builder: (BuildContext context, Orientation orientation) {
IS_PORTRAIT.value = orientation == Orientation.portrait;
return MyBody();
},
),
);
3) Any where listen current orientation
return ValueListenableBuilder(
valueListenable: IS_PORTRAIT,
builder: (BuildContext context, value, Widget child) {
return Container(
color: Colors.green[100],
child: Container(),
height: (IS_PORTRAIT.value)? 150: 80,
);
},
);