flutter - 如果用户在移动设备上,则将用户重定向到我的应用的网络版本上的 Play 商店
问题描述
我有一个使用 Android 和 Web 构建目标的颤振应用程序。当我访问时:
- Android 手机上的网络版本,我想将用户重定向到 Google Play 商店
- 桌面上的网络版本,我想显示一个按钮去播放商店,宣传一个应用程序存在的事实
- 在 Android 版本上,什么都不做,什么也不显示。
这个怎么做?
解决方案
为了启动 URL,我使用了 url_launcher 包。要检测网页版本,可以使用基础包导出的kIsWeb。请注意,在网络上经常看到的建议,检查 Platform is not working on Flutter Web(未实现)。同样,Chrome Mobile 上的 defaultTargetPlatform 是 TargetPlatform.android,因此不能用于检测应用程序版本。
import 'package:url_launcher/url_launcher.dart';
import 'package:flutter/foundation.dart';
@override
Widget build(BuildContext context) {
//Launch the app store URL if we're on web and on android
if (defaultTargetPlatform == TargetPlatform.android && kIsWeb) {
launch(
"https://play.google.com/store/apps/details?id=com.myapp.id",
webOnlyWindowName: '_self',
);
}
[... return my normal web page design, the following is part of a Column() ... ]
!kIsWeb
? []
: <Widget>[
SizedBox(height:8),
Text("or, if you have an Android Mobile phone:"),
SizedBox(height: 8),
ElevatedButton(
onPressed: () {
launch(
"https://play.google.com/store/apps/details?id=com.app.id");
},
child: Text(
"Open the App on the Google Play store!")
)
],
编辑:自动重定向时的“webOnlyWindowName:'_self'”将替换当前选项卡。以前我没有这个,chrome mobile 会认为这是一个弹出窗口,用户大部分时间都会忽略它。最后一个问题是这会打开游戏商店的网页版,我希望它打开真正的游戏商店......
推荐阅读
- python - Angular CLI + Flask 应用程序使用 ng serve 运行和显示,但不通过 Flask 显示
- javascript - 如何修复 HTTP 状态 500 - Servlet.init() 用于 servlet jersey 抛出异常?
- c# - 无法使用 Xunit 在异步方法中断言异常
- python - 使用 fastai 确定图像分割神经网络中的输入神经元数量?
- c# - 在只读对象属性上编辑 DataGridViewCell
- javascript - 反应(表情符号)收集并获得确切的数字
- python - 为什么这个网站的请求模块随机失败
- python - Pollard 的 p-1 算法的第二阶段
- javascript - 多次执行“clearInterval”时计时器不停止
- typescript - TypeScript 声明文件(函数构造函数)