首页 > 解决方案 > 如果用户在移动设备上,则将用户重定向到我的应用的网络版本上的 Play 商店

问题描述

我有一个使用 Android 和 Web 构建目标的颤振应用程序。当我访问时:

这个怎么做?

标签: flutterdartflutter-web

解决方案


为了启动 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 会认为这是一个弹出窗口,用户大部分时间都会忽略它。最后一个问题是这会打开游戏商店的网页版,我希望它打开真正的游戏商店......


推荐阅读