Flutter 打开外部第三方应用
作者:互联网
Flutter 打开外部第三方应用:url_launcher 插件的使用
https://pub.flutter-io.cn/packages/url_launcher/versions pubspec.yaml 文件中添加 dev_dependencies: flutter_test: sdk: flutter url_launcher: ^6.0.10
Scaffold中 SnackBar的使用
// 需要设置Scaffold的key才能弹出SnackBar return Scaffold( key: _scaffoldkey,// appBar: AppBar( title: Text(title), ), body: ...,);
IOS 中需要添加白名单
Scheme 跳转协议:
- 如跳转微信、QQ、高德地图等,都是改变相应的url协议地址即可,跳转原理参照原生开发使用的url scheme,比如一些常用的
- 微信: weixin://
- 京东: openapp.jdmoble://
- 淘宝: taobao://
- Chrome: googlechrome://
- 百度地图: baidumap://
- 高德地图:androidamap://、iosamap://
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
class OpenApplication extends StatelessWidget {
List<Map> buttons = [
{'title': '打开浏览器', 'scheme': 'https://www.baidu.com'},
{'title': '打开地图', 'scheme': 'geo:52.32.4.917'},
{'title': '打开微信', 'scheme': 'weixin://'},
{'title': '打开京东', 'scheme': 'openapp.jdmoble://'},
{'title': '打开淘宝', 'scheme': 'taobao://'},
{'title': '打开百度地图', 'scheme': ' baidumap://'},
];
// 需要设置Scaffold的key才能弹出SnackBar
var _scaffoldkey = GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
String title = (ModalRoute.of(context)!.settings.arguments as Map)['desc'];
// TODO: implement build
return Scaffold(
key: _scaffoldkey,
appBar: AppBar(
title: Text(title),
),
body: Column(
children: [
Column(
children: buttons
.map((item) => _launchButton(
title: item['title'],
scheme: item['scheme'],
context: context))
.toList(),
),
// FlatButton(
// onPressed: () {
// print('SnackBar');
// // final snackBar =
// // new SnackBar(content: new Text('这是一个SnackBar'));
// // Scaffold.of(context).showSnackBar(snackBar);
// _scaffoldkey.currentState!.showSnackBar(
// SnackBar(content: new Text('这是一个SnackBar')));
// },
// child: Text('SnackBar'))
],
));
}
void _launchURL() async {
const url = 'https://baidu.com';
if (await canLaunch(url)) {
await launch(url);
} else {}
}
void _launchMap() async {
//android
const url = 'geo:52.32.4.917'; //App 提供的 schema
if (await canLaunch(url)) {
await launch(url);
} else {}
}
Widget _launchButton(
{required String title,
required String scheme,
required BuildContext context}) {
return Container(
child: FlatButton(
onPressed: () {
_launchScheme(scheme: scheme, context: context);
},
child: Container(
margin: EdgeInsets.only(top: 18),
width: double.infinity,
height: 50,
decoration: BoxDecoration(
color: Color(0xff88ff22),
borderRadius: BorderRadius.circular(25),
boxShadow: [
BoxShadow(
color: Colors.redAccent, blurRadius: 3, offset: Offset(0, 1))
],
),
child: Center(
child: Text(
title,
style: TextStyle(
fontSize: 20,
color: Colors.black,
),
),
),
),
),
);
}
void _launchScheme(
{required String scheme, required BuildContext context}) async {
if (await canLaunch(scheme)) {
await launch(scheme);
} else {
print('不支持打开');
// 需要设置Scaffold的key才能弹出SnackBar
_scaffoldkey.currentState!
.showSnackBar(SnackBar(content: new Text('无法打开')));
// Scaffold.of(context).showSnackBar(SnackBar(
// content: Text('老孟,一枚有态度的程序员'),
// ));
}
}
}
标签:title,外部,SnackBar,Scaffold,第三方,url,context,scheme,Flutter 来源: https://blog.csdn.net/nicepainkiller/article/details/121303253