flutter打开第三方应用

添加依赖

url_launcher: ^5.4.1

————————main.dart

import 'package:url_launcher/url_launcher.dart';

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

const String TITLE='whqtest';

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: TITLE,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: TITLE),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String _title = TITLE;

  void _pressed() async{
    // Android
    print("open weixin");
    const url = 'vnd.weixin://';
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      //  Ios
      const url = 'weixin://';
      if(await canLaunch(url)){
        await launch(url);
      }else{
      throw 'Could not launch $url';
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            RaisedButton(
              child: Text('微信'),
              color: Colors.blue,
              textColor: Colors.white,
              onPressed: _pressed,
            )
          ]
       ),
      ),
    );
  }
}
发布了426 篇原创文章 · 获赞 33 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/whq12789/article/details/103629715