confirm_order.dart 17 KB

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