means_pdf.dart 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import 'package:flutter_full_pdf_viewer/flutter_full_pdf_viewer.dart';
  2. import 'package:flutter_full_pdf_viewer/full_pdf_viewer_plugin.dart';
  3. import 'package:flutter_full_pdf_viewer/full_pdf_viewer_scaffold.dart';
  4. import 'package:liftmanager/res/resources.dart';
  5. import 'dart:async';
  6. import 'dart:io';
  7. import 'package:dio/dio.dart';
  8. import 'package:flutter/foundation.dart';
  9. import 'package:flutter/material.dart';
  10. import 'package:flutter_full_pdf_viewer/full_pdf_viewer_scaffold.dart';
  11. import 'package:liftmanager/widgets/app_bar.dart';
  12. import 'package:path_provider/path_provider.dart';
  13. import 'package:liftmanager/utils/toast.dart';
  14. import 'package:liftmanager/net/api_service.dart';
  15. // class MeansPDF extends StatefulWidget {
  16. // @override
  17. // MeansPDFState createState() => new MeansPDFState();
  18. // }
  19. // class MeansPDFState extends State<MeansPDF> {
  20. // String pathPDF = "";
  21. // @override
  22. // void initState() {
  23. // super.initState();
  24. // createFileOfPdfUrl().then((f) {
  25. // setState(() {
  26. // pathPDF = f.path;
  27. // print(pathPDF);
  28. // });
  29. // });
  30. // }
  31. // Future<File> createFileOfPdfUrl() async {
  32. // final url = "http://africau.edu/images/default/sample.pdf";
  33. // final filename = url.substring(url.lastIndexOf("/") + 1);
  34. // var request = await HttpClient().getUrl(Uri.parse(url));
  35. // var response = await request.close();
  36. // var bytes = await consolidateHttpClientResponseBytes(response);
  37. // String dir = (await getApplicationDocumentsDirectory()).path;
  38. // File file = new File('$dir/$filename');
  39. // await file.writeAsBytes(bytes);
  40. // return file;
  41. // }
  42. // @override
  43. // Widget build(BuildContext context) {
  44. // return Container(
  45. // child: PDFScreen(pathPDF)
  46. // )
  47. // ;
  48. // }
  49. // // Widget build(BuildContext context) {
  50. // // return Scaffold(
  51. // // appBar: AppBar(title: const Text('Plugin example app')),
  52. // // body: Center(
  53. // // child: RaisedButton(
  54. // // child: Text("Open PDF"),
  55. // // onPressed: () => Navigator.push(
  56. // // context,
  57. // // MaterialPageRoute(builder: (context) => PDFScreen(pathPDF)),
  58. // // ),
  59. // // ),
  60. // // ),
  61. // // );
  62. // // }
  63. // }
  64. class PDFScreen extends StatelessWidget {
  65. String pathPDF = "";
  66. String fileUrl = "";
  67. String id = "";
  68. PDFScreen(this.pathPDF, this.fileUrl,this.id);
  69. getUpdateDownLoadNum() {
  70. // showLoading(context);
  71. NewApiService().getUpdateDownLoadNum(id, onSuccess: (res) {
  72. }, onError: (code, msg) {
  73. // dismissLoading(context);
  74. toasts(msg);
  75. });
  76. }
  77. getViewNum() {
  78. // showLoading(context);
  79. NewApiService().getLiftFilesDetail(id, onSuccess: (res) {
  80. }, onError: (code, msg) {
  81. // dismissLoading(context);
  82. toasts(msg);
  83. });
  84. }
  85. @override
  86. Widget build(BuildContext context) {
  87. print("123456789");
  88. print(pathPDF);
  89. print(fileUrl);
  90. print(id);
  91. print("123456789");
  92. if(id!=null&&id!="null"){
  93. getViewNum();
  94. }
  95. Future<String> _findLocalPath() async {
  96. //这里根据平台获取当前安装目录
  97. final directory = Theme.of(context).platform == TargetPlatform.android
  98. ? await getExternalStorageDirectory()
  99. : await getApplicationDocumentsDirectory();
  100. return directory.path;
  101. }
  102. return PDFViewerScaffold(
  103. appBar: MyAppBar(
  104. centerTitle: fileUrl!=null&&fileUrl!="null"?'电梯资料库':"详情",
  105. // actions: <Widget>[
  106. // fileUrl!=null&&fileUrl!="null"? FlatButton(
  107. // child: Text("下载"),
  108. // textColor: Colours.dark_text,
  109. // highlightColor: Colors.transparent,
  110. // onPressed: () async {
  111. // String _localPath = (await _findLocalPath()) + '/Download/';
  112. // var pos = fileUrl.lastIndexOf("/");
  113. // if (pos == -1) {
  114. // pos = fileUrl.lastIndexOf("\\");
  115. // }
  116. // var filename = fileUrl.substring(pos + 1);
  117. // Response responce =
  118. // await Dio().download(fileUrl, _localPath + filename);
  119. // if (responce.statusCode == 200) {
  120. // // Scaffold.of(context).showSnackBar(SnackBar(content: Text("下载成功")));
  121. // getUpdateDownLoadNum();
  122. // toasts("下载成功");
  123. // } else {
  124. // // Scaffold.of(context).showSnackBar(SnackBar(content: Text("下载失败")));
  125. // toasts("下载失败");
  126. // }
  127. // },
  128. // ):Container(child:null,)
  129. // ],
  130. ),
  131. path: pathPDF);
  132. }
  133. }