Reputation: 129
I want to apply screen orientation on only one of the screens in my application. I have tried to apply it using if condition and orientation builder but somehow, it is not working. For Example, If a user presses the auto-rotation button, the screen should behave according to the rotation but I want it only on one screen.
//main.dart
void main(
{
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setPreferredOrientations
([DeviceOrientation.portraitUp]);
runApp(IframeScreen());
}
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:flutter/services.dart';
import 'package:modal_progress_hud/modal_progress_hud.dart';
import 'package:provider/provider.dart';
class IframeScreen extends StatefulWidget {
@override
_IframeScreenState createState() => _IframeScreenState();
}
class _IframeScreenState extends State<IframeScreen> {
InAppWebViewController _webViewController;
IframeScreen iframe = new IframeScreen();
_IframeScreenState(
@override
void initState() {
onLoadingTesting().then((value) {
print('check modalprogress');
});
super.initState();
}
@override
void dispose(){
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeLeft,
DeviceOrientation.portraitUp
]);
super.dispose();
}
InAppWebView webViewMethod() {
WidgetsFlutterBinding.ensureInitialized();
Permission.camera.request();
Permission.microphone.request();
}
}
@override
Widget build(BuildContext context) {
return ModalProgressHUD(
inAsyncCall: _saving,
child: WillPopScope(
onWillPop: (){
if(MediaQuery.of(context).orientation==Orientation.landscape){
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
}
return Future.value(true);
},
child: Scaffold(
body: OrientationBuilder(
builder: (BuildContext context,orientation) {
return Column(
children: <Widget>[
Expanded(
child: Container(
child: InAppWebView(
initialUrl:
"https://appr.tc/r/158489234",
initialOptions: InAppWebViewGroupOptions(
crossPlatform: InAppWebViewOptions(
mediaPlaybackRequiresUserGesture: false,
debuggingEnabled: true,
),
),
onWebViewCreated: (
InAppWebViewController controller) {
_webViewController = controller;
},
androidOnPermissionRequest:
(InAppWebViewController controller, String origin,
List<String> resources) async {
return PermissionRequestResponse(
resources: resources,
action: PermissionRequestResponseAction.GRANT);
}),
),
)
],
);
}
)
),
),
);
}
Upvotes: 3
Views: 4134
Reputation: 306
For this you should set a preferred orientation in you main() method as
WidgetsFlutterBinding.ensureInitialized();
await SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitDown, DeviceOrientation.portraitUp]);
This will ensure that your app will be on portrait (as set in this sample code), until you change it.
A suggestion here is, in the screen which you need to be landscape, rather than changing the orientation inside the Dispose function, wrap the scaffold with a "WillPopScope", and implement the OnWillPop() method as follows
onWillPop: () {
if (MediaQuery.of(context).orientation == Orientation.landscape) {
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
}
return Future.value(true);
},
This will give a smoother transition from the landscape screen to the portrait screen.
Cheers!
Upvotes: 3