means_pdf.dart 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. import 'package:flutter_full_pdf_viewer/flutter_full_pdf_viewer.dart';
  2. import 'package:flutter_full_pdf_viewer/full_pdf_viewer_scaffold.dart';
  3. import 'dart:async';
  4. import 'package:flutter/foundation.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:liftmanager/res/iconfont.dart';
  7. import 'package:liftmanager/routers/fluro_navigator.dart';
  8. import 'package:liftmanager/utils/theme_utils.dart';
  9. import 'package:liftmanager/widgets/app_bar.dart';
  10. import 'package:liftmanager/widgets/load_image.dart';
  11. import 'package:liftmanager/widgets/popup_window.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. GlobalKey _addKey = GlobalKey();
  70. getUpdateDownLoadNum() {
  71. // showLoading(context);
  72. NewApiService().getUpdateDownLoadNum(id, onSuccess: (res) {},
  73. onError: (code, msg) {
  74. // dismissLoading(context);
  75. toasts(msg);
  76. });
  77. }
  78. getViewNum() {
  79. // showLoading(context);
  80. NewApiService().getLiftFilesDetail(id, onSuccess: (res) {},
  81. onError: (code, msg) {
  82. // dismissLoading(context);
  83. toasts(msg);
  84. });
  85. }
  86. @override
  87. Widget build(BuildContext context) {
  88. print(pathPDF);
  89. print(fileUrl);
  90. print(id);
  91. if (id != null && id != "null") {
  92. getViewNum();
  93. }
  94. Future<String> _findLocalPath() async {
  95. //这里根据平台获取当前安装目录
  96. final directory = Theme.of(context).platform == TargetPlatform.android
  97. ? await getExternalStorageDirectory()
  98. : await getApplicationDocumentsDirectory();
  99. return directory.path;
  100. }
  101. return PDFViewerScaffold(
  102. appBar: MyAppBar(
  103. centerTitle: fileUrl != null && fileUrl != "null" ? '电梯资料库' : "文件详情",
  104. // actions: [
  105. // IconButton(
  106. // key: _addKey,
  107. // icon: Icon(Iconfont.gengduo),
  108. // onPressed: () {
  109. // _showAddMenu(context);
  110. // })
  111. // ],
  112. // actions: <Widget>[
  113. // fileUrl!=null&&fileUrl!="null"? FlatButton(
  114. // child: Text("下载"),
  115. // textColor: Colours.dark_text,
  116. // highlightColor: Colors.transparent,
  117. // onPressed: () async {
  118. // String _localPath = (await _findLocalPath()) + '/Download/';
  119. // var pos = fileUrl.lastIndexOf("/");
  120. // if (pos == -1) {
  121. // pos = fileUrl.lastIndexOf("\\");
  122. // }
  123. // var filename = fileUrl.substring(pos + 1);
  124. // Response responce =
  125. // await Dio().download(fileUrl, _localPath + filename);
  126. // if (responce.statusCode == 200) {
  127. // // Scaffold.of(context).showSnackBar(SnackBar(content: Text("下载成功")));
  128. // getUpdateDownLoadNum();
  129. // toasts("下载成功");
  130. // } else {
  131. // // Scaffold.of(context).showSnackBar(SnackBar(content: Text("下载失败")));
  132. // toasts("下载失败");
  133. // }
  134. // },
  135. // ):Container(child:null,)
  136. // ],
  137. ),
  138. path: pathPDF);
  139. }
  140. _showAddMenu(BuildContext context) {
  141. final RenderBox button = _addKey.currentContext.findRenderObject();
  142. final RenderBox overlay = Overlay.of(context).context.findRenderObject();
  143. var a = button.localToGlobal(
  144. Offset(button.size.width - 8.0, button.size.height - 12.0),
  145. ancestor: overlay);
  146. var b = button.localToGlobal(button.size.bottomLeft(Offset(0, -12.0)),
  147. ancestor: overlay);
  148. final RelativeRect position = RelativeRect.fromRect(
  149. Rect.fromPoints(a, b),
  150. Offset.zero & overlay.size,
  151. );
  152. final Color backgroundColor = ThemeUtils.getBackgroundColor(context);
  153. final Color _iconColor = ThemeUtils.getIconColor(context);
  154. showPopupWindow(
  155. context: context,
  156. fullWidth: false,
  157. isShowBg: true,
  158. position: position,
  159. elevation: 2,
  160. child: GestureDetector(
  161. onTap: () => NavigatorUtils.goBack(context),
  162. child: Column(
  163. crossAxisAlignment: CrossAxisAlignment.end,
  164. children: <Widget>[
  165. Padding(
  166. padding: const EdgeInsets.only(right: 12.0),
  167. child: LoadAssetImage(
  168. "icon_jt",
  169. width: 8.0,
  170. height: 4.0,
  171. ),
  172. ),
  173. SizedBox(
  174. width: 120.0,
  175. height: 40.0,
  176. child: FlatButton.icon(
  177. textColor: Theme.of(context).textTheme.body1.color,
  178. onPressed: () {
  179. Navigator.pop(context);
  180. // NavigatorUtils.pushResult(
  181. // context, '${TeamRouter.teamCreatePage}', (res) {
  182. // _onRefresh();
  183. // });
  184. },
  185. color: backgroundColor,
  186. shape: RoundedRectangleBorder(
  187. borderRadius: BorderRadius.only(
  188. topLeft: Radius.circular(8.0),
  189. topRight: Radius.circular(8.0)),
  190. ),
  191. icon: LoadAssetImage(
  192. "wode/icon_team_add",
  193. width: 16.0,
  194. height: 16.0,
  195. color: _iconColor,
  196. ),
  197. label: const Text("创建团队")),
  198. ),
  199. Divider(),
  200. SizedBox(
  201. width: 120.0,
  202. height: 40.0,
  203. child: FlatButton.icon(
  204. textColor: Theme.of(context).textTheme.body1.color,
  205. color: backgroundColor,
  206. onPressed: () {
  207. Navigator.pop(context);
  208. // NavigatorUtils.push(
  209. // context, '${TeamRouter.teamSearchPage}');
  210. },
  211. shape: RoundedRectangleBorder(
  212. borderRadius: BorderRadius.only(
  213. bottomLeft: Radius.circular(8.0),
  214. bottomRight: Radius.circular(8.0)),
  215. ),
  216. icon: LoadAssetImage(
  217. "wode/icon_team_add",
  218. width: 16.0,
  219. height: 16.0,
  220. color: _iconColor,
  221. ),
  222. label: const Text("加入团队")),
  223. ),
  224. ],
  225. ),
  226. ),
  227. );
  228. }
  229. }