首页 > 解决方案 > 如何以编程方式从我的 Flutter 应用程序启动应用程序?

问题描述

我知道这里可能有一些重复,但我发现的其他解决方案并不能完全满足我的需要。我想要做的是启动一个特定的应用程序,如果没有安装它,在 Playstore 和 AppStore 上启动应用程序页面,如果使用 iPhone。我已经接近完成这项工作,但我需要一些帮助才能越线。

我有一个加载所选应用程序的 FloatingAction 按钮,如果未安装,则加载 PlayStore,但它不会直接进入应用程序页面。我还没有在 iPhone 上进行测试,所以如果该代码是垃圾,我也会很感激那里的一些帮助。

如何让我的应用程序直接进入 Playstore 应用程序和 appstore 应用程序上的应用程序页面?

这是我的代码

import 'package:flutter/material.dart';
import 'dart:io';
import 'package:flutter_appavailability/flutter_appavailability.dart';

void main() => runApp(new MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('App Availability'),
        ),
        body: FloatingActionButton(
          backgroundColor: Colors.indigo,
          onPressed: () => openApp(context),
          child: Icon(Icons.open_in_new),
          heroTag: "Open App",
        ),
      ),
    );
  }
}

void openApp(BuildContext context) {
  try {
    AppAvailability.launchApp(Platform.isIOS 
      ? "appname://" 
      : "com.company.appname"
    ).then((_) {
      print("App Launched!");
    }).catchError((err) {
      AppAvailability.launchApp(Platform.isIOS 
        ? "appstore://" 
        : "com.android.vending"
      ) 
// I think I will need to add package name to the playstore package name
// in the line above, but not sure how.
// I have tried com.android.vending?id=com.criticalarc.safezoneapp
// and com.android.vending/com.criticalarc.safezoneapp

      .then((_) {});
      print(err);
    });
  } catch (e) {
    Scaffold.of(context).showSnackBar(
      SnackBar(
        content: Text("App Not Installed!"),
      ),
    );
    print("App Not Installed!");
  }
}

谢谢

标签: androidiosflutterdart

解决方案



推荐阅读