If my user change the orientation of the device from portrait to landscape, it is working well. But then if he wants to come back to portrait, the screen is locked to landscape.
#override
void initState() {
super.initState();
_setPreferredOrientation();
}
_setPreferredOrientation(){
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
DeviceOrientation.landscapeRight,
DeviceOrientation.landscapeLeft,
]);
}
Related
How to enable landscape and portrait orientation to tablets and iPad, but small devices just portrait mode?
Set orientations
void main() async {
await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
runApp(
// ..
);
}
Another approach is to check the width of device.
Use MediaQuery or WidgetsBinding intance (see here)
From research, you can find the widest phone dimension(lets say 480px)
and add it as a breakpoint. check manually all devices or use the breakpoints mentioned in this article for html
your condition could be as follows:
final double deviceWidth=WidgetsBinding.instance.window.physicalSize.width;
final double breakPointWidth=480;
if(deviceWidth<=breakPointWidth){
//set portait only options
}
else {
//set both.
}
You can set the desired orientation for your Flutter app by using the SystemChrome.setPreferredOrientations method. To enable landscape and portrait orientation for tablets and iPads, but only portrait mode for smaller devices, you can use the following code:
import 'package:flutter/services.dart';
import 'dart:io' show Platform;
// ...
void main() {
runApp(MyApp());
if (Platform.isTablet || Platform.isIOS) {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
DeviceOrientation.landscapeLeft,
DeviceOrientation.landscapeRight,
]);
} else {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
}
}
In this code, Platform.isTablet and Platform.isIOS are used to determine whether the device is a tablet or an iPad. If the device is a tablet or an iPad, the SystemChrome.setPreferredOrientations method is called with an array of DeviceOrientation values that allow both landscape and portrait orientations. If the device is not a tablet or an iPad, the method is called with an array of DeviceOrientation values that allow only portrait orientation.
I have created Flutter Webview for the game. game is working on landscape mood. Webview also shows landscape but when I back the Webview app also showed in landscape mood
Its because you are not changing the system view after the game screen.
Try anyone of the following,
1.Add this in the .then(){} call back of the navigation call to the gmescreen,
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeRight,
DeviceOrientation.landscapeLeft,
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
OR
2.Add this at the dispose method of game screen,
#override
void dispose() {
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeRight,
DeviceOrientation.landscapeLeft,
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
super.dispose();
}
How to get the Screen height in flutter regardless of the device orientation? As I know with the media query the height changes with the device orientation.
You can check if orientation is landscape or portrait:
Orientation currentOrientation = MediaQuery.of(context).orientation;
if(currentOrientation == Orientation.portrait){
height = MediaQuery.of(context).size.height
} else {
height = MediaQuery.of(context).size.width
}
I have a flutter app which is basically a book with 210 pages created by a flutter page builder. But the problem is after 150 pages when I rotate it to portrait to Landscape its just blank. How can I solve these problems?
Setting the orientations you want explicitly in the app's 'main()` function could work...
void main() {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
DeviceOrientation.landscapeLeft,
DeviceOrientation.landscapeRight,
]);
runApp(MyApp());
}
With iOS7, even though the app has the Device Orientation under General set to landscape and portrait. There are some screens in my app that I do NOT want to be able to rotate.
How can you pick and choose which views get rotated when the device turns?
I tried to use shouldAutorotateToInterfaceOrientation but I could not get it to work.
Anyone accomplished this with the new software?
try the following code in the views that you want it to be just landscape and change the landscape part to portrait in the views that you want it to be only portrait.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeLeft;
}
Try overriding - (BOOL)shouldAutorotate and return NO.