简体   繁体   中英

How do I launch an app from my Flutter app programmatically?

I understand there maybe some duplication here, but the other solutions I have found don't do exactly what I need. What I want to do is to launch a particular app and if it isn't installed launch the apps page on Playstore and also AppStore, if using an iPhone. I am pretty close to getting this working, but I need a little help getting over the line.

I have a FloatingAction Button that loads the chosen app and if it isn't installed, loads PlayStore, but it doesn't go directly to the App's page. I haven't testing on iPhone yet, so if that code is garbage, I would appreciate some help there too.

How can I get my app to go straight to the app's page on playstore app and appstore app?

Here is my code

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!");
  }
}

thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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