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.
Related
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,
]);
}
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();
}
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.
My iPhone app is all in portrait mode at the moment.
But I need to play a video that in widescreen mode and draw some ui elements that work best in portrait mode. Also there will be some text input in the screen at times, so keyboard will need to appear as portrait too.
Is it possible to tell a UIViewController when it loads that it needs to go to Landscape, the parent view on the stack will be Portrait.
I dont want to detect the user rotating, I want to decide which screens will be landscape and which will be portrait.
So to recap, everyscreen/view on my app will be portrait, except one that I would like to make Landscape. Can it be done?
Many Thanks,
-Code
In your UIViewController you implement this method
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
BOOL result = NO;
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
result = YES;
}
return result;
}
And you return YES for the orientation you want to support.
This method will be call on your UIViewController when it load, so it won't get call every time you enter in that UIViewController.
And if you want to be absolutely certain that all other UIViewController are the orientation you want, implement this method in them as well.
Yup, use [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight]]; or portrait for portrait orientation.
override func viewWillAppear(animated: Bool) {
let value = UIInterfaceOrientation.LandscapeRight.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
UIApplication.sharedApplication().statusBarHidden = true
}
override func shouldAutorotate() -> Bool {
return true
}
and change it to what you want (LandscapeRight , LandscapeLeft or Portrait)