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

GetX路由Deep Link使用异常:点击返回键屏幕黑屏求助

Flutter Deeplink跳转后返回黑屏的解决方法

问题原因

通过Deep Link直接打开应用内页面时,Flutter路由栈中只会存在被跳转的目标页面,没有初始路由(如splash、home)。当点击返回键时,路由栈被清空,Flutter没有页面可渲染,就会显示Android窗口的默认背景(也就是你看到的黑屏)。

虽然你在GetInformationParser中设置了initialRoute: AppRoutes.splash,但通过Deep Link启动应用时,这个初始路由不会自动加入路由栈,导致栈结构异常。

解决方案

方案一:自定义返回键逻辑,栈空时直接退出应用

修改GetDelegate的配置,添加返回键处理逻辑,当路由栈只剩最后一个页面时,直接退出应用而不是让页面黑屏:

import 'package:flutter/services.dart'; // 导入系统服务包

// ...

routerDelegate: GetDelegate(
  backButtonPopMode: PopMode.Page,
  // 添加自定义返回逻辑
  onBackPressed: () {
    // 获取当前路由栈长度
    final stackLength = Get.rootDelegate.navigator?.widget.children.length ?? 0;
    if (stackLength == 1) {
      // 栈中只剩一个页面,退出应用
      SystemNavigator.pop();
      return true; // 告诉GetX已处理返回事件
    }
    // 正常返回上一页
    return false;
  },
  notFoundRoute: GetPage(
    name: AppRoutes.unknown, 
    page: () => const UnknownPage()
  ),
),

方案二:处理Deep Link时先加载初始路由

在应用启动时先获取Deep Link,确保初始路由被加入栈后再跳转到目标页面。修改main函数和MyApp:

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter/services.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  // 获取启动时的Deep Link
  final Uri? initialUri = await getInitialUri();
  runApp(MyApp(initialUri: initialUri));
}

class MyApp extends StatelessWidget {
  final Uri? initialUri;
  const MyApp({super.key, this.initialUri});

  @override
  Widget build(BuildContext context) {
    return GetMaterialApp.router(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      routeInformationParser: GetInformationParser(
        initialRoute: AppRoutes.splash,
      ),
      routerDelegate: GetDelegate(
        backButtonPopMode: PopMode.Page,
        notFoundRoute: GetPage(
          name: AppRoutes.unknown, 
          page: () => const UnknownPage()
        ),
        // 监听路由初始化完成事件
        onInitialRoute: (route) {
          if (initialUri != null) {
            // 初始路由加载完成后,延迟跳转到Deep Link指定页面
            Future.delayed(const Duration(milliseconds: 300), () {
              Get.rootDelegate.toNamed(initialUri!.path);
            });
          }
        },
      ),
      getPages: AppPages.routes,
    );
  }
}

同时确保你的Splash页面在初始化完成后自动跳转到Home页面:

class SplashPage extends StatelessWidget {
  const SplashPage({super.key});

  @override
  Widget build(BuildContext context) {
    Future.delayed(const Duration(seconds: 1), () {
      Get.rootDelegate.toNamed(AppRoutes.home);
    });
    return const Scaffold(
      body: Center(child: CircularProgressIndicator()),
    );
  }
}

方案三:调整Android主题背景色(可选)

如果上述方案仍有偶尔黑屏的情况,可以修改NormalTheme的背景色,让窗口背景和应用主题一致:

在res/values/styles.xml中:

<style name="NormalTheme" parent="@android:style/Theme.Light.NoTitleBar">
    <item name="android:windowBackground">@color/white</item> <!-- 设置为应用常用背景色 -->
</style>

这样即使路由栈为空,窗口背景也会显示设置的颜色,不会出现纯黑屏。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 03:42:23