flutter apps blank when I rotate portrait to landscape - flutter

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());
}

Related

Set orientations Flutter

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.

Impossible to come back to the previous device orientation

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,
]);
}

My app in landscape When i back my flutter webview

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();
}

Customize Rotation Behavior For Specific Widgets In Flutter

In my Flutter Application, I am using a OrientationBuilder widget to change my layout based on the current orientation.
But the animation occuring when the widgets change position doesn't look smooth, and the scaling on UI elements temporarely becomes weird.
Is there a way to implement an orientation change, such that the elements just stay in their positions, and rotate themselves?
Current behavior:
Preferred Behavior:
I have left out the camera screen in the flutter App, as this is only about the onscreen controlls. The camera view itself isn't the issue!
I've been having the same issue, the camera preview rotation works weird. What I ended up doing is using this package native orientation which provides the following API: Stream<NativeDeviceOrientation> onOrientationChanged(useSensor: false) which will trigger orientation changes even if you lock the screen using: SystemChrome.setPreferredOrientations()
Basically in the init of your component: _orientation = NativeDeviceOrientationCommunicator() .onOrientationChanged(useSensor: true) .listen((event) { switch (event) { case NativeDeviceOrientation.portraitUp: case NativeDeviceOrientation.unknown: _actualOrientation = DeviceOrientation.portraitUp; break; case NativeDeviceOrientation.portraitDown: _actualOrientation = DeviceOrientation.portraitDown; break; case NativeDeviceOrientation.landscapeLeft: _actualOrientation = DeviceOrientation.landscapeLeft; break; case NativeDeviceOrientation.landscapeRight: _actualOrientation = DeviceOrientation.landscapeRight; break; } }); and then use the actual orientation to animate the icons or know the actual orientation of the captured image.
Remember to close the subscription on dispose _orientation.cancel();

Screen rotation on a per view basis

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.