繁体   English   中英

如何以编程方式从我的 Flutter 应用程序启动应用程序?

[英]How do I launch an app from my Flutter app programmatically?

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

我有一个 FloatingAction Button 来加载所选的应用程序,如果它没有安装,则加载 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!");
  }
}

谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM