You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android系统UI自动关闭:Flutter设置edgeToEdge后失效如何解决?

解决SystemUiMode.edgeToEdge设置后系统UI自动消失的问题

问题原因

这种现象通常是因为仅在main函数中设置一次系统UI模式无法持久生效——Flutter页面组件(如Scaffold)、路由切换或应用生命周期变化时,系统UI模式可能被自动重置。

具体解决方法

1. 在根组件的initState中重复配置

将系统UI模式设置放到根组件的初始化方法中,确保页面渲染完成后仍能维持edge-to-edge状态,同时可配套设置UI覆盖样式避免内容遮挡:

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
    // 重新设置edge-to-edge模式
    SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
    // 适配透明状态栏/导航栏,避免内容被遮挡
    SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
      statusBarColor: Colors.transparent,
      navigationBarColor: Colors.transparent,
    ));
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        // 让页面内容延伸到状态栏下方
        extendBodyBehindAppBar: true,
        appBar: AppBar(title: const Text("Edge to Edge Demo")),
        body: const Center(child: Text("Hello Edge to Edge")),
      ),
    );
  }
}

2. 监听应用生命周期与路由变化

如果应用存在多页面切换,或需要在后台切回前台时维持设置,可添加生命周期与路由监听:

@override
void initState() {
  super.initState();
  _setupSystemUI();
  // 监听路由切换
  WidgetsBinding.instance.addObserver(_routeObserver);
}

void _setupSystemUI() {
  SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
  SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
    statusBarColor: Colors.transparent,
    navigationBarColor: Colors.transparent,
  ));
}

final RouteObserver<PageRoute> _routeObserver = RouteObserver<PageRoute>();

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
  super.didChangeAppLifecycleState(state);
  // 应用从后台恢复时重新设置
  if (state == AppLifecycleState.resumed) {
    _setupSystemUI();
  }
}

@override
void dispose() {
  WidgetsBinding.instance.removeObserver(_routeObserver);
  super.dispose();
}

3. 排查全局代码冲突

检查项目中是否有其他位置调用了SystemChrome.setEnabledSystemUIMode(如某些全屏页面的配置),确保这些代码不会覆盖edge-to-edge的设置。

内容的提问来源于stack exchange,提问作者BurakBK

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 10:21:12