# FlutterLaunchApp
**Repository Path**: scenario-samples/flutter-launch-app
## Basic Information
- **Project Name**: FlutterLaunchApp
- **Description**: 该项目包含两个Flutter应用,演示了如何使用url_launcher从一个应用跳转到另一个应用,并根据参数打开不同的页面。
- **Primary Language**: Unknown
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 1
- **Forks**: 0
- **Created**: 2025-12-01
- **Last Updated**: 2025-12-05
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# Flutter拉起应用并跳转到指定页面
## 介绍
跨应用拉起与深度链接跳转是移动端互联类应用的高频使用场景之一,如用户在工具应用中一键打开目标应用并直达指定功能页面。本示例主要基于深度链接能力,结合url_launcher与app_links等关键技术,实现在app1中拉起app2并依据URL参数跳转到不同页面的效果。
## 效果图预览
## 实现思路
1. app1使用url_launch插件拉起app2。
* pubspec.yaml引入url_launch:
```yaml
dependencies:
url_launcher:
git:
url: https://gitcode.com/openharmony-tpc/flutter_packages
path: packages/url_launcher/url_launcher
```
* main.dart拉起App:
```dart
Future _launchApp2(String page) async {
// 使用 myapp2:// 自定义 scheme 打开 app2
final Uri url = Uri.parse('myapp2://open?page=$page');
if (await canLaunchUrl(url)) {
await launchUrl(url, mode: LaunchMode.externalApplication);
} else {
print("102");
debugPrint('无法打开 App2: $url');
}
}
```
* module.json5配置querySchemes:
```json5
{
"module": {
"querySchemes": [
"myapp2"
],
}
}
```
2. app2使用app_links接收被拉起跳转的url。
* pubspec.yaml引入app_links:
```yaml
dependencies:
app_links: ^6.4.0
app_links_ohos: ^0.2.0
```
* main.dart接收到需要跳转的url解析并执行跳转:
```dart
@override
void initState() {
super.initState();
_initDeepLinkListener();
}
Future _initDeepLinkListener() async {
_appLinks = AppLinks();
// 获取初始链接(应用未运行时)
try {
final uri = await _appLinks.getInitialLink();
if (uri != null) {
_handleDeepLink(uri);
}
} catch (e) {
debugPrint('Failed to get initial link: $e');
}
// 监听新的链接(应用运行时)
_linkSubscription = _appLinks.uriLinkStream.listen(
(uri) {
_handleDeepLink(uri);
},
onError: (err) {
debugPrint('Deep link error: $err');
},
);
}
void _handleDeepLink(Uri uri) {
final page = uri.queryParameters['page'] ?? 'home';
// 导航到指定页面
WidgetsBinding.instance.addPostFrameCallback((_) {
_navigateToPage(page);
});
}
```
* module.json5配置scheme:
```json5
"skills": [
{
"entities": [
"entity.system.browsable"
],
"actions": [
"ohos.want.action.viewData"
],
"uris": [
{
"scheme": "myapp2",
"host": "open"
}
]
}
]
```
## 使用说明
- 进入app1目录,执行flutter pub get,使用DevecoStudio签完名后,运行flutter run。
- 进入app2目录,执行flutter pub get,使用DevecoStudio签完名后,运行flutter run。
- 在app1点击拉起跳转app2的不同页面。
## 约束与限制
- 本示例支持API Version 20 Release及以上版本。
- 本示例支持HarmonyOS 6.0.0 Release SDK及以上版本。
- 本示例需要使用DevEco Studio 6.0.0 Release及以上版本进行编译运行。
## 工程结构&模块类型
```markdown
FlutterLaunchApp/
├──README.md
├──app1/ // 启动器应用
│ ├──lib/
│ │ └──main.dart // App1 主程序
│ ├──pubspec.yaml // 依赖配置
│ └──ohos/ // HarmonyOS 配置
│ ├──AppScope/
│ ├──entry/
│ │ └──src/main/
│ │ ├──ets/ // ArkTS 代码
│ │ ├──module.json5
│ │ └──resources/ // 资源文件
│ └──build-profile.json5
│
└──app2/ // 目标应用(接收 Deep Link)
├──lib/
│ └──main.dart // App2 主程序
├──pubspec.yaml // 依赖配置
└──ohos/ // HarmonyOS 配置
├──AppScope/
├──entry/
│ └──src/main/
│ ├──ets/ // ArkTS 代码
│ ├──module.json5
│ └──resources/ // 资源文件
└──build-profile.json5
```
## 参考文档
- [Flutter环境搭建](https://gitcode.com/openharmony-tpc/flutter_flutter)
- [Flutter开发文档](https://gitcode.com/openharmony-tpc/flutter_samples/tree/master/ohos/docs)
- [ohos判断应用是否安装](https://developer.huawei.com/consumer/cn/doc/architecture-guides/shaking_to_dialog_1-ts_9-0000002328366029)