Flutter打开第三方app应用

738 阅读1分钟

找到url_launch包pub.dev/packages/ur…

安装后使用:

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
​
final Uri _url = Uri.parse('https://www.devio.com');
​
class LaunchUrl extends StatelessWidget {
  const LaunchUrl({Key? key}) : super(key: key);
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'statefulWidget基础组件',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: Scaffold(
          appBar: AppBar(
            // Here we take the value from the MyHomePage object that was created by
            // the App.build method, and use it to set our appbar title.
            title: Text('plugin使用'),
            leading: GestureDetector(
              onTap: (){
                Navigator.pop(context);
              },
              child: Icon(Icons.arrow_back),
            ),
          ),
          body: Center(
            child:
            Column(
              children: <Widget>[
                ElevatedButton(
                  onPressed: _launchUrl,
                  child: Text('打开浏览器'),
                ),
                ElevatedButton(
                  onPressed: _openMap,
                  child: Text('打开地图'),
                ),
              ],
            )
​
          ),
        )
    );
  }
​
  _launchUrl() async {
    if (!await launchUrl(_url)) throw 'Could not launch $_url';
  }
​
  _openMap() async{
    // Android
    Uri url = Uri(scheme: 'geo:52.32,4.917'); // APP提供者提供的shema
    if (await canLaunchUrl(url)) {
      await launchUrl(url);
    } else {
      // ios
      Uri url = Uri(scheme: 'https://maps.apple.com/?ll=52.32,4.917');
      if (await canLaunchUrl(url)) {
        await launchUrl(url);
      } else {
        throw 'Could not open';
      }
    }
  }
}

\