confirm_order.dart 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. import 'dart:io';
  2. import 'package:chewie/chewie.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter/services.dart';
  5. import 'package:flutter_screenutil/flutter_screenutil.dart';
  6. import 'package:image_picker/image_picker.dart';
  7. import 'package:liftmanager/internal/wode/wode_router.dart';
  8. import 'package:liftmanager/net/api_service.dart';
  9. import 'package:liftmanager/routers/fluro_navigator.dart';
  10. import 'package:liftmanager/utils/fast_notification.dart';
  11. import 'package:liftmanager/utils/oss_upload.dart';
  12. import 'package:liftmanager/utils/theme_utils.dart';
  13. import 'package:liftmanager/utils/toast.dart';
  14. import 'package:liftmanager/utils/utils.dart';
  15. import 'package:liftmanager/widgets/app_bar.dart';
  16. import 'package:liftmanager/widgets/selected_video_change.dart';
  17. import 'package:video_player/video_player.dart';
  18. class ConfirmOrder extends StatefulWidget {
  19. ConfirmOrder(this.id);
  20. final String id;
  21. @override
  22. State<StatefulWidget> createState() {
  23. return ConfirmOrderState();
  24. }
  25. }
  26. class ConfirmOrderState extends State<ConfirmOrder> {
  27. VideoPlayerController _controller;
  28. VideoPlayerController _controllerFile;
  29. ChewieController _chewieController;
  30. // final FijkPlayer player = FijkPlayer();
  31. // List<String> imagesUrl = [];
  32. // List<String> videoUrl = [];
  33. String videoUrl;
  34. String str;
  35. String imagesUrl;
  36. double percent = 0.0;
  37. @override
  38. void initState() {
  39. super.initState();
  40. }
  41. @override
  42. void dispose() {
  43. // TODO: implement dispose
  44. _controller?.pause();
  45. _controller?.dispose();
  46. _chewieController?.dispose();
  47. super.dispose();
  48. }
  49. ///选择图片
  50. void selectPicker() {
  51. showDialog(
  52. context: context,
  53. builder: (BuildContext context) {
  54. return SimpleDialog(
  55. title: Text("选择方式"),
  56. children: ["拍照", '从手机相册选择'].map((String value) {
  57. print("$value");
  58. return SimpleDialogOption(
  59. child: Text(
  60. "${value}",
  61. style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
  62. ),
  63. onPressed: () {
  64. _getImage(value == '拍照' ? 1 : 0);
  65. Navigator.of(context).pop();
  66. },
  67. );
  68. }).toList(),
  69. );
  70. },
  71. );
  72. }
  73. void _getImage(int key) async {
  74. print(key);
  75. print(333);
  76. try {
  77. await ImagePicker.pickVideo(
  78. source: key == 1 ? ImageSource.camera : ImageSource.gallery,
  79. ).then((File f) async {
  80. if (f != null) {
  81. _controllerFile = VideoPlayerController.file(f);
  82. _controllerFile.initialize().then((val) {
  83. _controllerFile.setLooping(true);
  84. int seconds = _controllerFile.value.duration.inSeconds;
  85. print("视频时长:$seconds");
  86. int fileSize = f.lengthSync(); //单位B
  87. print("视频大小:${fileSize}");
  88. print("视频大小:${f.path}");
  89. if (seconds <= 300) {
  90. _uploadImage(f.path);
  91. // upLoadFileOnce(_imageFile.path);
  92. // setState(() {});
  93. } else {
  94. toasts("视频时长不能大于5分钟!");
  95. }
  96. }).catchError((error) {
  97. print(error);
  98. print("error");
  99. toasts("上传失败,不支持此格式");
  100. });
  101. }
  102. });
  103. } catch (e) {
  104. toasts("没有权限,无法打开相册!");
  105. }
  106. }
  107. void _uploadImage(filePath) async {
  108. showPercent(context, () {
  109. dismissLoading(context);
  110. toasts("上传失败");
  111. }, () {
  112. if (videoUrl == null && str != null) {
  113. setState(() {
  114. videoUrl = str;
  115. _controller?.pause();
  116. _controller?.dispose();
  117. _controller =
  118. VideoPlayerController.network(Utils.getImagePath(videoUrl)
  119. // imgFontUrl + detailObj.url
  120. );
  121. print("videoUrl:" + videoUrl);
  122. dismissLoading(context);
  123. toasts("上传成功");
  124. });
  125. }
  126. });
  127. String uploadName = OssUtil.instance.getImageUploadName(filePath);
  128. await NewApiService.uploadImage(context, uploadName, filePath).then((data) {
  129. if (data.statusCode == 200) {
  130. str = NewApiUrl.URL_UPLOAD_IMAGE_OSS + "/" + uploadName;
  131. print("str:" + str);
  132. print(videoUrl);
  133. if (str != null) {
  134. Map obj = {"uploadName": uploadName, "success": true};
  135. FastNotification.push("percent", obj);
  136. }
  137. } else {
  138. Map obj = {"uploadName": uploadName, "success": false};
  139. FastNotification.push("percent", obj);
  140. }
  141. }).catchError((data) {
  142. Map obj = {"uploadName": uploadName, "success": false};
  143. FastNotification.push("percent", obj);
  144. });
  145. }
  146. upLoadFileOnce(path) {
  147. showLoading(context, "正在上传...");
  148. NewApiService().upload(path, onSuccess: (res) {
  149. dismissLoading(context);
  150. setState(() {
  151. videoUrl = res.pathUrl;
  152. imagesUrl = res.coverUrl;
  153. settingVideoVc();
  154. });
  155. }, onError: (code, msg) {
  156. dismissLoading(context);
  157. toasts(msg);
  158. });
  159. }
  160. submitApply() {
  161. if (videoUrl == null || videoUrl == '') {
  162. toasts("请上传视频");
  163. return;
  164. }
  165. showLoading(context);
  166. NewApiService().chargeMakeSureCase({
  167. "id": widget.id,
  168. "afterRepair": videoUrl,
  169. }, onSuccess: (res) {
  170. dismissLoading(context);
  171. toasts("确认完成");
  172. if (_controller != null) {
  173. _controller.pause();
  174. }
  175. NavigatorUtils.push(context, "${WodeRouter.orderPageMaster}?checkType=0");
  176. setState(() {});
  177. }, onError: (code, msg) {
  178. dismissLoading(context);
  179. toasts(msg);
  180. });
  181. }
  182. settingVideoVc() {
  183. _controller?.pause();
  184. _controller?.dispose();
  185. _chewieController?.dispose();
  186. _controller = VideoPlayerController.network(Utils.getImagePath(videoUrl));
  187. _chewieController = ChewieController(
  188. videoPlayerController:
  189. // VideoPlayerController.network(
  190. // imgFontUrl + detailObj.url
  191. // ),
  192. _controller,
  193. aspectRatio: 3 / 2,
  194. allowFullScreen: false,
  195. autoPlay: false,
  196. looping: true,
  197. // startAt: Duration(seconds: 1,minutes: 1),
  198. showControls: true,
  199. deviceOrientationsAfterFullScreen: [DeviceOrientation.portraitUp],
  200. // 占位图
  201. // placeholder: Image.network(
  202. // imgFontUrl+detailObj.cover,
  203. // fit: BoxFit.contain,
  204. // ),
  205. // 是否在 UI 构建的时候就加载视频
  206. autoInitialize: true,
  207. // 拖动条样式颜色
  208. materialProgressColors: new ChewieProgressColors(
  209. playedColor: Colors.red,
  210. handleColor: Colors.blue,
  211. backgroundColor: Colors.grey,
  212. bufferedColor: Colors.lightGreen,
  213. ),
  214. );
  215. }
  216. // 文本编辑控制
  217. GlobalKey _formKey = new GlobalKey<FormState>();
  218. @override
  219. Widget build(BuildContext context) {
  220. double width = MediaQuery.of(context).size.width;
  221. return Scaffold(
  222. resizeToAvoidBottomPadding: false, //不让键盘弹上去
  223. appBar: MyAppBar(
  224. centerTitle: "确认订单",
  225. ),
  226. body: Container(
  227. child: ListView(
  228. children: <Widget>[
  229. Form(
  230. key: _formKey, //设置globalKey,用于后面获取FormState
  231. // autovalidate: true, //开启自动校验
  232. child: Column(
  233. children: <Widget>[
  234. Container(
  235. width: width,
  236. padding: EdgeInsets.only(left: 15, bottom: 15),
  237. child: Text(
  238. "(建议时长3分钟,建议大小50M)",
  239. style: TextStyle(
  240. color: Colors.red,
  241. fontSize: ScreenUtil().setSp(14),
  242. ),
  243. textAlign: TextAlign.left,
  244. ),
  245. ),
  246. // Container(
  247. // color: Colors.white,
  248. // child: GridView.builder(
  249. // shrinkWrap: true,
  250. // padding: const EdgeInsets.fromLTRB(8.0, 12, 8.0, 12.0),
  251. // physics: NeverScrollableScrollPhysics(),
  252. // gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
  253. // crossAxisCount: 3, childAspectRatio: 1.18),
  254. // itemCount: 1,
  255. // itemBuilder: (_, index) {
  256. // return Stack(
  257. // children: <Widget>[
  258. // Center(
  259. // child: SelectedImage(
  260. // image: imagesUrl,
  261. // index: index,
  262. // onTap: () {
  263. // selectPicker();
  264. // FocusScope.of(context).requestFocus(FocusNode());
  265. // },
  266. // ),
  267. // ),
  268. // videoUrl != null
  269. // ? Positioned(
  270. // top: 0,
  271. // right: 0,
  272. // child: GestureDetector(
  273. // onTap: () {
  274. // print(index);
  275. // videoUrl = null;
  276. // imagesUrl = null;
  277. // setState(() {});
  278. // },
  279. // child: Icon(
  280. // IconData(0xe62a, fontFamily: "Iconfont"),
  281. // size: 24.0,
  282. // color: Color(0xff999999),
  283. // ),
  284. // ),
  285. // )
  286. // : Container(
  287. // child: null,
  288. // )
  289. // ],
  290. // );
  291. // },
  292. // ),
  293. // ),
  294. Container(
  295. color: ThemeUtils.getDialogTextFieldColor(context),
  296. child: GridView.builder(
  297. shrinkWrap: true,
  298. padding: const EdgeInsets.fromLTRB(8.0, 12, 8.0, 12.0),
  299. physics: NeverScrollableScrollPhysics(),
  300. gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
  301. crossAxisCount: 1, childAspectRatio: 1.18),
  302. itemCount: 1,
  303. itemBuilder: (_, index) {
  304. return Stack(
  305. children: <Widget>[
  306. Center(
  307. child: SelectedVideo(
  308. image: videoUrl,
  309. index: index,
  310. videoPlay: videoPlay(),
  311. onTap: () {
  312. if (videoUrl == null) {
  313. selectPicker();
  314. }
  315. },
  316. ),
  317. ),
  318. videoUrl != null
  319. ? Positioned(
  320. top: 0,
  321. right: 0,
  322. child: GestureDetector(
  323. onTap: () {
  324. print(index);
  325. // imagesUrl = null;
  326. setState(() {
  327. videoUrl = null;
  328. str = null;
  329. _controller.pause();
  330. // player.reset();
  331. });
  332. },
  333. child: Icon(
  334. const IconData(0xe651,
  335. fontFamily: "Iconfont"),
  336. size: 24.0,
  337. color: Color(0xff999999),
  338. ),
  339. ),
  340. )
  341. : Container(
  342. child: null,
  343. )
  344. ],
  345. );
  346. },
  347. ),
  348. ),
  349. Container(
  350. height: ScreenUtil().setWidth(44),
  351. decoration: BoxDecoration(
  352. borderRadius:
  353. BorderRadius.circular(ScreenUtil().setWidth(22)),
  354. gradient: const LinearGradient(
  355. colors: [Color(0xFF00D9FF), Color(0xFF0287FF)]),
  356. ),
  357. margin: EdgeInsets.all(20.0),
  358. width: double.infinity,
  359. child: FlatButton(
  360. // padding: EdgeInsets.all(15.0),
  361. child: Text("提交"),
  362. // color: Theme
  363. // .of(context)
  364. // .primaryColor,
  365. textColor: Colors.white,
  366. onPressed: () {
  367. /*
  368. * 如果:context不对。可以使用GlobalKey,
  369. * 通过_formKey.currentState 获取FormState后,
  370. * 调用validate()方法校验用户名密码是否合法,校验
  371. * 通过后再提交数据。
  372. */
  373. if ((_formKey.currentState as FormState).validate()) {
  374. submitApply();
  375. }
  376. },
  377. ),
  378. ),
  379. ],
  380. ),
  381. ),
  382. ],
  383. ),
  384. ),
  385. );
  386. }
  387. Widget videoPlay() {
  388. if (_controller == null) return Container();
  389. double width = MediaQuery.of(context).size.width;
  390. return Container(
  391. padding: EdgeInsets.only(
  392. left: ScreenUtil().setWidth(15),
  393. right: ScreenUtil().setWidth(15),
  394. top: ScreenUtil().setWidth(15)),
  395. child: ClipRRect(
  396. borderRadius: BorderRadius.circular(5),
  397. child: new Chewie(controller: _chewieController),
  398. ));
  399. }
  400. }