深度链接
当应用收到新 URL 时导航到对应路由。
深度链接不仅能打开应用,还能将用户带到应用内「深层」的特定位置。例如,一双运动鞋广告中的深度链接可能会打开购物应用,并显示那双鞋的商品页面。
Flutter 在 iOS、Android 和 Web 上均支持深度链接。打开 URL 会在你的应用中显示对应界面。按照以下步骤,你可以使用命名路由(通过 routes
参数或
onGenerateRoute),或使用
Router widget
来启动并显示路由。
若在 Web 浏览器中运行应用,无需额外配置。路由路径的处理方式与 iOS 或 Android 深度链接相同。默认情况下,Web 应用使用 /#/path/to/app/screen 模式从 URL 片段读取深度链接路径,但你可以通过配置应用的 URL 策略来更改这一点。
若你偏好视觉学习,请观看以下视频:
入门
#入门请参阅我们为 Android 和 iOS 准备的 Cookbook 教程:
从基于 plugin 的深度链接迁移
#
若你已按 Medium 上的免费文章
Deep Links and Flutter applications
所述编写了处理深度链接的 plugin,应退出 Flutter 的默认深度链接处理器。为此,在 Info.plist 中将 FlutterDeepLinkingEnabled 设为 false,或
在 AndroidManifest.xml 中将 flutter_deeplinking_enabled 设为 false。
行为
#行为会因平台以及应用是否已启动并运行而略有不同。
| Platform / Scenario | Using Navigator | Using Router |
|---|---|---|
| iOS (not launched) | App gets initialRoute ("/") and a short time after gets a pushRoute | App gets initialRoute ("/") and a short time after uses the RouteInformationParser to parse the route and call RouterDelegate.setNewRoutePath, which configures the Navigator with the corresponding Page. |
| Android - (not launched) | App gets initialRoute containing the route ("/deeplink") | App gets initialRoute ("/deeplink") and passes it to the RouteInformationParser to parse the route and call RouterDelegate.setNewRoutePath, which configures the Navigator with the corresponding Pages. |
| iOS (launched) | pushRoute is called | Path is parsed, and the Navigator is configured with a new set of Pages. |
| Android (launched) | pushRoute is called | Path is parsed, and the Navigator is configured with a new set of Pages. |
| 平台 / 场景 | 使用 Navigator | 使用 Router |
|---|---|---|
| iOS(未启动) | 应用获得 initialRoute("/"),稍后收到 pushRoute | 应用获得 initialRoute("/"),稍后使用 RouteInformationParser 解析路由并调用 RouterDelegate.setNewRoutePath,从而用对应的 Page 配置 Navigator。 |
| Android(未启动) | 应用获得包含路由("/deeplink")的 initialRoute | 应用获得 initialRoute("/deeplink")并传给 RouteInformationParser 解析路由,调用 RouterDelegate.setNewRoutePath,从而用对应的 Pages 配置 Navigator。 |
| iOS(已启动) | 调用 pushRoute | 解析路径,并用新的 Pages 集合配置 Navigator。 |
| Android(已启动) | 调用 pushRoute | 解析路径,并用新的 Pages 集合配置 Navigator。 |
使用 Router widget 时,当应用运行期间打开新的深度链接,你的应用可以替换当前的页面集合。
延伸阅读
#-
Learning Flutter's new navigation and routing system provides an introduction to the Router system.
-
Deep dive into Flutter deep linking video from Google I/O 2023
-
Flutter Deep Linking: The Ultimate Guide, a step-by-step tutorial showing how to implement deep links in Flutter.
-
Learning Flutter's new navigation and routing system 介绍了 Router 系统。
-
Google I/O 2023 的 Deep dive into Flutter deep linking 视频
-
Flutter Deep Linking: The Ultimate Guide,分步教程,展示如何在 Flutter 中实现深度链接。
除非另有说明,本文档之所提及适用于 Flutter 3.44.0 版本。本页面最后更新时间:2026-06-04。查看文档源码 或者 为本页面内容提出建议。