Flutter设置横屏致Android页面重置问题及解决方案
问题:设置指定页面为横屏后Activity重置返回首页
我需要将指定页面的屏幕方向设置为DeviceOrientation.landscapeLeft,但设置后,屏幕方向改变、UI渲染完成后,Activity会立即重置,应用直接返回首页。无论build方法返回什么内容,都会发生跳转,且无任何报错信息。
我尝试在进入该页面之前修改屏幕方向,但无论在哪个页面设置,最终都会跳转回主页面。
补充:已找到可行解决方案
需修改AndroidManifest文件:
修改前
<activity android:name=".MainActivity" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
修改后
<activity android:name=".MainActivity" android:configChanges="orientation|keyboardHidden"
该修改可阻止页面重置问题。
相关代码
横屏页面(ConsumptionExpandedScreen)状态类代码
class _ConsumptionExpandedScreenState extends State<ConsumptionExpandedScreen> with WidgetsBindingObserver { @override void initState() { super.initState(); WidgetsBinding.instance.addObserver(this); WidgetsFlutterBinding.ensureInitialized(); // 锁定屏幕方向为左侧横屏 _setLandscapeOrientation(); } @override void dispose() { logger.d("In the dispose function of the expanded screen"); // 离开页面时重置屏幕方向为竖屏 SystemChrome.setPreferredOrientations([ DeviceOrientation.portraitUp, ]); WidgetsBinding.instance.removeObserver(this); super.dispose(); } @override void didChangeAppLifecycleState(AppLifecycleState state) { if (state == AppLifecycleState.resumed) { _setLandscapeOrientation(); } } void _setLandscapeOrientation() { SystemChrome.setPreferredOrientations([ DeviceOrientation.landscapeLeft, ]); } @override Widget build(BuildContext context) { double screenWidth = MediaQuery.of(context).size.width; double screenHeight = MediaQuery.of(context).size.height; logger.d( "Expanded consumption the current size(width : height) is $screenWidth : $screenHeight"); ...
主页面(MainApp)状态类代码
class _MainAppState extends State<MainApp> with WidgetsBindingObserver { Locale myLocale = const Locale('en', 'US'); @override void didChangeAppLifecycleState(AppLifecycleState state) { // 按需处理应用生命周期变化 } @override void didChangeMetrics() { final newSize = WidgetsBinding.instance.window.physicalSize; logger.d("New size: ${newSize.width} : ${newSize.height}"); // 按需处理屏幕方向变化 setState(() {}); // 触发重建以调整布局 } @override void initState() { super.initState(); _loadData(); } void _loadData() async { final prefs = await SharedPreferences.getInstance(); indexLanguage = prefs.getInt(langIndexKey); // print("The number of the index $indexLanguage"); if (indexLanguage != null) {} setState(() {}); } @override void dispose() { // _adapterStateStateSubscription.cancel(); super.dispose(); } @override Widget build(BuildContext context) { return ChangeNotifierProvider<ValueChangeProvider>( create: (context) => ValueChangeProvider(), child: Consumer<ValueChangeProvider>( builder: (context, valueChangeProvider, _) { logger.d("The main screen "); double screenWidth = MediaQuery.of(context).size.width; double screenHeight = MediaQuery.of(context).size.height; logger.d( "The current size(width : height) is $screenWidth : $screenHeight"); updatePaddingValues(context); return PaddingConfig( sidePadding: globalPaddingSide, topPadding: paddingTop, topHeaderPadding: paddingHeaderTop, child: MaterialApp( color: AppColors.backgroundColor, navigatorObservers: [ BluetoothAdapterStateObserver(valueChangeProvider), routeObserver ], localizationsDelegates: AppLocalizations.localizationsDelegates, supportedLocales: AppLocalizations.supportedLocales, locale: valueChangeProvider.currentLocale, theme: lightTheme, debugShowCheckedModeBanner: false, initialRoute: initialAppRoute, routes: AppRoutes.routes, ), ); }, ), ); } }
跳转页面时的日志
I/flutter (18518): [D] Expanded consumption the current size(width : height) is 360.0 : 708.0 I/flutter (18518): [D] The main screen I/flutter (18518): [D] The current size(width : height) is 672.0 : 360.0 I/flutter (18518): [D] In the pairing list screen I/flutter (18518): [D] Expanded consumption the current size(width : height) is 672.0 : 360.0 I/flutter (18518): [D] Going to the pairing list I/flutter (18518): [D] In the pairing list screen I/flutter (18518): [D] Expanded consumption the current size(width : height) is 672.0 : 360.0 I/flutter (18518): [D] The main screen I/flutter (18518): [D] The current size(width : height) is 360.0 : 708.0 I/flutter (18518): [D] In the pairing list screen I/flutter (18518): [D] In the pairing list screen I/flutter (18518): [D] Expanded consumption the current size(width : height) is 360.0 : 708.0 I/flutter (18518): [D] Going to the pairing list I/flutter (18518): [D] In the pairing list screen I/flutter (18518): [D] In the pairing list screen
内容的提问来源于stack exchange,提问作者Dries Vandenbussche
相关产品推荐
相关产品推荐

