gpt4 book ai didi

flutter - 单击后退按钮时设置方向

转载 作者:行者123 更新时间:2023-12-03 03:01:29 24 4
gpt4 key购买 nike

我在第一个屏幕上设置了2个屏幕,我设置了横向,在第二个屏幕上,当我弹出第二个屏幕时又应用了纵向,回到第一个屏幕仍然设置为纵向,我该如何解决此问题,我应该将这些行放在哪里代码,这样我回到主屏幕时就可以重新定向。

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
home: won(),
));
}
class won extends StatelessWidget {
@override
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations(
[DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight]);
return Scaffold(
body: Center(
child: RaisedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => won1()),
);
},
child: Text('Forward'),
),
),
);
}
}
class won1 extends StatelessWidget {
@override
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
return Scaffold(
body: Center(
child: RaisedButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('Back'),
),
),
);
}
}

最佳答案

我通过使用RouteObserver(https://api.flutter.dev/flutter/widgets/RouteObserver-class.html)成功处理了它
导航时使用此选项传递屏幕名称:

Navigator.of(context)
.push(MaterialPageRoute(
builder: (context) => w,
settings: RouteSettings(name: w.runtimeType.toString())))
.then((value) {
if (value == null) {
widget.getHomepageData();
}
});
然后通过向MaterialApp添加观察者来检测和处理屏幕变化(向前或向后导航):
return MaterialApp(
builder: (BuildContext context, Widget child) {
return MediaQuery(
data: MediaQuery.of(context).copyWith(
textScaleFactor: 1.0),
child: child,
);
},
home: GeneralStatelessWidget((ctx) => getHome(ctx)),
navigatorObservers: [
CustomRouteObserver()
],
onGenerateTitle: generateAppTitle);
这是自定义观察者:
import 'package:flutter/material.dart';
import 'package:tangerine/utils/flutter_device_type.dart';
import 'package:tangerine/utils/screen_functions.dart';

class CustomRouteObserver extends RouteObserver<PageRoute<dynamic>> {
void _sendScreenView(PageRoute<dynamic> route) {
var screenName = route.settings.name;
final landscapeOnlyOnTablet = [
"LandingPage",
"/",
"ProductScreen",
"SearchScreen"
];
if (landscapeOnlyOnTablet.contains(screenName)) {
if (Device.get().isTablet) {
allowLandscape();
} else {
denyLandscape();
}
} else {
allowLandscape();
}
}

@override
void didPush(Route<dynamic> route, Route<dynamic> previousRoute) {
super.didPush(route, previousRoute);
if (route is PageRoute) {
_sendScreenView(route);
}
}

@override
void didReplace({Route<dynamic> newRoute, Route<dynamic> oldRoute}) {
super.didReplace(newRoute: newRoute, oldRoute: oldRoute);
if (newRoute is PageRoute) {
_sendScreenView(newRoute);
}
}

@override
void didPop(Route<dynamic> route, Route<dynamic> previousRoute) {
super.didPop(route, previousRoute);
if (previousRoute is PageRoute && route is PageRoute) {
_sendScreenView(previousRoute);
}
}
}

void allowLandscape() {
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeRight,
DeviceOrientation.landscapeLeft,
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
}

void denyLandscape() {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
}

关于flutter - 单击后退按钮时设置方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58029804/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com