通过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. asch相关的linux基础知识分享
  2. SGU 113 Nearly prime numbers
  3. 第三篇 :Mysql存储引擎、数据导入导出、管理表记录、匹配条件
  4. java图片预览上传_Java实现图片上传预览 (使用ajax提交)
  5. (45)FPGA同步复位与异步复位(同步复位)
  6. linux shell 后台执行脚本的方法 脚本后台运行 后台运行程
  7. rails 2.3.5 + jquery ui datepicker 不能显示中文
  8. vue3移动端腾讯地图坐标拾取,获取当前定位(腾讯、高德、百度、天地图),火星坐标GCJ-02–>百度坐标BD-09,根据坐标经纬度计算两点距离的方法,点击链接打开地图导航的方法
  9. java毕业设计_高校后勤管理系统
  10. 【经典递归问题 汉诺塔 求解】
  11. yourshelf是什么意思中文_[英语shelf的中文是什么意思]英语shelf的中文是什么意思...
  12. 工信部发布八项互联网新通用顶级域名服务技术要求
  13. 基于ARM裸机的知识点总结(9)------基于S5PV210的定时器、看门狗和RTC
  14. 应聘华为各类工程师通信基础题库以及答案(转)
  15. win10环境下基于OpenVINO部署yolov5模型
  16. juniper常用命令
  17. IOS下载资源zip到本地然后读取
  18. 【缺陷检测】基于形态学实现印刷电路板缺陷检测技术附matlab代码
  19. PR字幕怎么去黑色背景
  20. dos命令行-禁用和启用本地连接

热门文章

  1. 传统软件架构与微服务架构
  2. Intellij IDEA 简单配置字体大小
  3. 9.2.3 Python图像处理之图像数学形态学-二值形态学应用-区域填充
  4. MultipartFile.transferTo(dest) 报 FileNotFoundException
  5. 勒让德函数(Legendre多项式)
  6. sql server中UNION 运算符随笔(几个需要注意的地方小总结)
  7. SpringBoot实现身份证实名认证(阿里云实现)
  8. 平板如何调色? 手把手教你校正iPad屏幕
  9. 什么是ajax异步刷新
  10. 基于Ubuntu + nextCloud 搭建自己的私人网盘