通过flutter预览本地的pdf文件,选了多个flutter插件后最终选择了pdfx,

首先看一下我的需求吧,左侧是所有的文件列表右侧是文件的内容,左侧比较简单我们读取指定的目录后将文件名显示出来,点击选中时候切换文件显示

说说实现过程吧,首先是通过flutter读取本地的文件夹目录,通过path_provider插件读取本地文件目录

  /*** 获取本地目录getExternalStorageDirectory()* /storage/emulated/0/Android/data/com.example.read_database/files* 通过list获取的是包含文件夹的*/static Future<Stream<FileSystemEntity>?> getLocalPdfFile() async {var externalStoragePath = await getExternalStorageDirectory();var dis = Directory('${externalStoragePath?.path}/mipdf/');if (dis.existsSync()) {return dis.list(recursive: true)//过滤文件,不显示文件夹.where((event) => FileSystemEntity.isFileSync(event.path));} else {dis.create();return null;}}
具体实现代码如下
class MuBiaoZiYuanBody extends StatefulWidget {@overrideState<StatefulWidget> createState() => MuBiaoZiYuanBodyState();
}class MuBiaoZiYuanBodyState extends State<MuBiaoZiYuanBody> {List<FileSystemEntity>? _stream;int _itemCount = 0;int _selected = 0;//Android 和ios用 PdfControllerPinch window用PdfControllerPdfControllerPinch? _controller;@overridevoid initState() {_initData();super.initState();}/// 初始化数据Future<void> _initData() async {_stream = await (await ReadFileUtil.getLocalPdfFile())?.toList();int length = (_stream?.length)!;_controller = PdfControllerPinch(document: PdfDocument.openFile(_stream![0].uri.toFilePath(windows: false),),);setState(() {_itemCount = length;});}Future<void> _ChangePdf(int path) async {//注意此处不可重新初始化,否则会出现刷新切换不了文件,实现不出来// PdfControllerPinch(//       document: PdfDocument.openFile(//         _stream![0].uri.toFilePath(windows: false),//       ),//     )//_controller?.loadDocument(PdfDocument.openFile(_stream![path].uri.toFilePath(windows: false)!));setState(() {_selected = path;});}@overrideWidget build(BuildContext context) {return Container(child: Row(children: [_buildListFile(),Expanded(flex: 1,child: Container(padding: const EdgeInsets.only(left: 4, top: 10, bottom: 10, right: 10),// child: PdfPreViewWidget(path: _selectFilePath),// child: PdfxWidget(path:'assets/kotlin.pdf'),child: (_controller == null)? const Center(child: Text("正在加载"),): buildPdfViewPinch(),),)],),);}Container _buildListFile() {return Container(width: 182,padding:const EdgeInsets.only(left: 4, top: 10, bottom: 10, right: 10),child: ListView.separated(separatorBuilder: (BuildContext context, int index) =>const Divider(color: Colors.transparent,height: 10,),itemCount: _itemCount,itemBuilder: (context, index) {return _buildItem(index, context);},),);}PdfViewPinch buildPdfViewPinch() {return PdfViewPinch(controller: _controller!,builders: PdfViewPinchBuilders<DefaultBuilderOptions>(options: const DefaultBuilderOptions(loaderSwitchDuration: Duration(milliseconds: 300)),documentLoaderBuilder: (_) =>Center(child: LoadingDialog(text: "加载文档")),pageLoaderBuilder: (_) => Center(child: LoadingDialog(text: "加载页")),errorBuilder: (_, error) => Center(child: Text(error.toString())),),);}Widget _buildItem(int index, BuildContext context) {var path = _stream?[index].path;return ElevatedButton(onPressed: () async {_ChangePdf(index);},style: ButtonStyle(padding: MaterialStateProperty.all(const EdgeInsets.all(15)),backgroundColor: MaterialStateProperty.all(_selected == index ? Color(0xfff8b651) : Colors.white),elevation: MaterialStateProperty.all(4),),child: Text(basename(path!),style: const TextStyle(color: MyColor.textColor,fontWeight: FontWeight.bold,fontSize: 16),),);}
}

此处需要避坑:

避坑1:

在change时候,就是重新加载一个新的文件的时候,我们只需要用controller重新loadDocument即可,不可以重新初始化,会出现加载不出来的情况。

_controller?.loadDocument(PdfDocument.openFile(_stream![path].uri.toFilePath(windows: false)));

避坑2:openFile(String path),这里的蚕食我们要用,不可以直接传入_stream![path]

_stream![path].uri.toFilePath(windows: false)

Flutter加载本地pdf文件相关推荐

  1. uniapp 使用pdf.js 加载本地pdf文件报错问题

    最近有在做一个移动端的应用,应用里涉及到大量的js游戏开发,使用Android原生做不太合适,就选择了uniapp,其中有一个功能是读取本地的pdf文件并展示.在网上查了点资料都是用pdf.js来开发 ...

  2. js修改本地json文件_Flutter加载本地JSON文件教程建议收藏

    今天农村老家的天气不是很好 而且外面还下雨了,每天只能坐在老家 打开电脑,看看文章,写写文章 今天我给大家带来一篇Flutter加载本地JSON文件教程 本头条核心宗旨 欢迎来到「技术刚刚好」作者,「 ...

  3. flutter html 加载_Flutter 加载本地 HTML 文件

    Flutter加载本地网页 1. 添加依赖到 pubspec.yaml: dependencies: webview_flutter: ^0.3.22+1 2. 将HTML文件放到assets中 3. ...

  4. flutter html 加载_Flutter开发:项目加载本地html文件的步骤

    Flutter开发会遇到各种各样的技术,而且flutter开发带来了新的"技术革命",解放了iOS单一开发和Android单一开发所带来的巨大成本问题,一套flutter代码可以适 ...

  5. uiwebview 读取本地html,UIWebView加载本地HTML文件

    一.准备HTML文件及其资源文件 使用UIWebView加载本地的HTML文件 index.html,在index.html中引用了本地的图片.CSS文件.JS文件以及外部的图片. index.htm ...

  6. Android中使用WebChromeClient显示Openlayers加载本地GeoJson文件显示地图(跨域问题解决)

    场景 Openlayers中加载GeoJson文件显示地图: Openlayers中加载GeoJson文件显示地图_BADAO_LIUMANG_QIZHI的博客-CSDN博客 上面加载显示GeoJso ...

  7. 加载本地json文件,并利用批处理调用Chrome显示html

    加载本地json文件 1.加载本地json文件 为了调试保存在本地的json数据,需要进行读入.一般使用jQuery来进行,但需要对浏览器进行一点设置. Chrome浏览器中有一个启动选项--allo ...

  8. 互联网浏览本地html,如何加载本地HTML文件,如果没有互联网连接

    我有一个uiwebview在我的应用程序,显示服务器上承载的HTML文件的数量.如果没有互联网连接,我还想要显示将显示的html文件的本地副本,但我不知道如何执行此操作.我有的.m文件与下面类似.如何 ...

  9. google支持本地ajax,360chrome,google chrome浏览器使用jquery.ajax加载本地html文件

    使用360chrome和google chrome浏览器加载本地html文件时,会报错,提示: XMLHttpRequest cannot load file:///Y:/jswg/code/html ...

最新文章

  1. 优秀代码的必知必会(一)?
  2. 【深度学习】陶大程等人编写!最新41页深度学习理论综述
  3. [小技巧][Java]Arrays.fill() 初始化 二维数组
  4. ChannelHandler 与 ChannelPipeline 详解
  5. 基于游戏开发的C语言大作业视频集锦
  6. 问题:lapack.so
  7. “SecureCRT遇到一个致命的错误且必须关闭”处理办法
  8. amazon sqs java_Amazon SQS消息多传递
  9. python编程实践-Python编程实践(1)
  10. 蚂蚁算法matlab
  11. Springboot 整合 Netty 实战
  12. 【评价模型】层次分析法(AHP) 个人总结 续更
  13. Tier2 上1K Node的准备
  14. 清明html模板,HTML5清明节柳叶飘动特效
  15. 远程计算机蓝屏,什么原因造成了蓝屏 电脑蓝屏错误代码介绍
  16. spring boot+thymeleaf+bootstrap 简单实现后台管理系统界面
  17. 两台路由器的连接方法和无线路由桥接
  18. Java Web学习(1)
  19. Spring中RedisTemplate方法中,redis相关操作笔记。[redis生成指定长度自增批次号,删除、设置过期时间等]
  20. 计算机小游戏有哪些,为你解答电脑小游戏有哪些

热门文章

  1. mac brew安装,brew卸载重装
  2. 大规模数据集的读存技巧
  3. 计算机毕业设计ssm+vue基本微信小程序的个人健康管理系统
  4. Windows10 设置文件显示扩展名后缀
  5. 围观五四青年节优秀借势文案-爱豆子
  6. 计算机管理账户英语,系统管理员英文单词【处置技巧】
  7. 2018年最好用的智能门禁,智慧社区运营软件都在这里!
  8. [转]使你更有思想的20本书
  9. 【SequoiaDB巨杉数据库】Cmd-getInfo
  10. 为啥我学的python需要下载pycharm_python学习一定用pycharm吗?