flutter: 用url_launcher在线打开pdf
一,下载
库的地址:
https://pub.dev/packages/url_launcher
编辑pubspec.yaml
dependencies:
flutter:
sdk: flutter
url_launcher: ^6.3.1
然后点击 pub get
二,配置权限
配置权限:android
编辑android/app/src/main/AndoridManifest.xml
在queries一项中增加:
<!-- If your app checks for SMS support -->
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="sms" />
</intent>
<!-- If your app checks for call support -->
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="tel" />
</intent>
<!-- If your application checks for inAppBrowserView launch mode support -->
<intent>
<action android:name="android.support.customtabs.action.CustomTabsService" />
</intent>
三,代码
///代码清单 1-1
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
class DownPage extends StatefulWidget {
@override
State<DownPage> createState() {
return _DownPageState();
}
}
class _DownPageState extends State<DownPage> {
//打开游览器/电话/短信等
//会使用手机中关联的app打开相应的pdf
Future<void> _launchUrl(String url) async {
final Uri _url = Uri.parse(url);
if (!await launchUrl(_url)) {
throw Exception('Could not launch $_url');
}
}
//文档地址
String pdfUrl = "http://www.test.com/test.pdf";
@override
Widget build(BuildContext context) {
///页面主体脚手架
return Scaffold(
/// 处理滑动
body:Center(
child:Column(
children:[
Container(
height: 300,
),
ElevatedButton(
child: Text("下载pdf文件"),
onPressed: () {
_launchUrl(pdfUrl);
},
),
],
),
),
);
}
}
四,测试效果:
界面

浙公网安备 33010602011771号