Can any one tell me how to open another app using flutter?

走远了吗. 提交于 2021-02-15 11:04:23

问题


I want to open a bunch of music app links using links data I have in firebase. I want to open, amazonPrimeMusic, Ganna, Spotify, Wynk, JioSavaan to name some.

Widget buildResultCard(data) {
  List items = [Text(data['Ganna']),
    IconButton(icon:Icon(Icons.all_inclusive),
      onPressed: ()=> {Text("Ganna")}
    ),

    Text(data['Wynk']),
    IconButton(icon:Icon(Icons.all_inclusive),
      onPressed: ()=> {Text("Ganna")}
    ),

    Text(data['JioSavaan']),
    IconButton(icon:Icon(Icons.all_inclusive),
      onPressed: ()=> {Text("Ganna")}
    ),

    Text(data['PrimeMusic']),
    IconButton(icon:Icon(Icons.all_inclusive),
      onPressed: ()=> {Text("Ganna")}
    )
  ];

  return ListView.builder(
    padding: EdgeInsets.only(top: 20),
    itemCount: items.length,
    itemBuilder: (BuildContext context, int index) {
      return items[index];
    },
  );
}

when I tap the button in the list it should open up the particular app for which the link is, for example for AmazonPrimeMusic link, it should open the Amazon music app.


回答1:


add this to the pubspec.yaml file under dependencies-

  device_apps:
  android_intent:
  url_launcher:

and add these to the top -

import 'package:device_apps/device_apps.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:android_intent/android_intent.dart';

and here is the sample code -

_openJioSavaan (data) async
{String dt = data['JioSavaan'] as String;
  bool isInstalled = await DeviceApps.isAppInstalled('com.jio.media.jiobeats');
if (isInstalled != false)
 {
    AndroidIntent intent = AndroidIntent(
      action: 'action_view',
      data: dt
  );
  await intent.launch();
 }
else
  {
  String url = dt;
  if (await canLaunch(url)) 
    await launch(url);
   else 
    throw 'Could not launch $url';
}
}



回答2:


You can use flutter_appavailability package. This plugin allows you to check if an app is installed in mobile and using this plugin you can launch an app.

If already installed then launch otherwise open link in WebView using url_launcher.




回答3:


To implement this functionality in Flutter, create a native platform integration, or use an existing plugin, such as external_app_launcher.

external_app_launcher Flutter plugin helps you to open another app from your app

  1. Add this to your package's pubspec.yaml file:

    dependencies:
       external_app_launcher: ^0.0.1 // add letest version
    
  2. Import in your Dart code, you can use:

    import 'package:external_app_launcher/external_app_launcher.dart';
    
  3. Getting Started

  • For opening apps in android

    For opening an external app from your app in android, you need provide packageName of the app.

    If the pluggin found the app in the device it will be be opened but if the the app is not installed in the device then it let the user to playstore link of the app.

    But if you don't want to navigate to playstore if app is not installed then make the openStore property to false.

  • For opening apps in ios

    In Ios, for opening an external app from your app, you need to provide URLscheme of the target app.

    In your deployment target is greater than or equal to 9 then also need to update external app information in infoPlist.

    <key>LSApplicationQueriesSchemes</key>
    <array>
        <string>pulsesecure</string> // url scheme name of the app
    </array>
    

    But like in Android it will not navifate to store(appStore) if app is not found in the device.

    For doing so You need to provide the itunes link of the app.

    More about getting started: https://pub.dev/packages/external_app_launcher#getting-started

Code Illustration

import 'package:flutter/material.dart';
import 'package:external_app_launcher/external_app_launcher.    dart';

void main() {
  runApp(MyApp());
}

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

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

  Color containerColor = Colors.red;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Container(
            height: 50,
            width: 150,
            child: RaisedButton(
                color: Colors.blue,
                onPressed: () async {
                  await LaunchApp.openApp(
                    androidPackageName: 'net.pulsesecure.   pulsesecure',
                    iosUrlScheme: 'pulsesecure://',
                    appStoreLink:
                        'itms-apps://itunes.apple.com/us/   app/pulse-secure/id945832041',
                    // openStore: false
                  );
                  // Enter thr package name of the App you  want to open and for iOS add the     URLscheme to the Info.plist file.
                  // The second arguments decide wether the     app redirects PlayStore or AppStore.
                  // For testing purpose you can enter com. instagram.android
                },
                child: Container(
                    child: Center(
                  child: Text(
                    "Open",
                    textAlign: TextAlign.center,
                  ),
                ))),
          ),
        ),
      ),
    );
  }
}


来源:https://stackoverflow.com/questions/55771211/can-any-one-tell-me-how-to-open-another-app-using-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!