添加依赖
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,
)
]
),
),
);
}
}