punchin.dart 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. import 'dart:async';
  2. import 'package:amap_location_flutter_plugin/amap_location_flutter_plugin.dart';
  3. import 'package:amap_location_flutter_plugin/amap_location_option.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:liftmanager/net/api_service.dart';
  6. import 'package:liftmanager/utils/toast.dart';
  7. import 'package:liftmanager/widgets/app_bar.dart';
  8. import 'package:liftmanager/routers/fluro_navigator.dart';
  9. import 'package:liftmanager/widgets/selected_video_change.dart';
  10. import 'package:image_picker/image_picker.dart';
  11. import 'package:flutter_screenutil/flutter_screenutil.dart';
  12. import 'package:liftmanager/internal/wode/wode_router.dart';
  13. import 'package:permission_handler/permission_handler.dart';
  14. import 'package:liftmanager/utils/theme_utils.dart';
  15. import 'package:liftmanager/utils/url.dart';
  16. import 'package:liftmanager/utils/utils.dart';
  17. import 'package:video_player/video_player.dart';
  18. import 'package:chewie/chewie.dart';
  19. import 'package:flutter/services.dart';
  20. import 'package:orientation/orientation.dart';
  21. class Punchin extends StatefulWidget {
  22. Punchin(this.id);
  23. final String id;
  24. @override
  25. State<StatefulWidget> createState() {
  26. return PunchinState();
  27. }
  28. }
  29. class PunchinState extends State<Punchin> {
  30. String videoUrl;
  31. String imagesUrl;
  32. Map<String, Object> _locationResult;
  33. StreamSubscription<Map<String, Object>> _locationListener;
  34. AmapLocationFlutterPlugin _locationPlugin = new AmapLocationFlutterPlugin();
  35. var latitude;
  36. var longitude;
  37. String address;
  38. @override
  39. void initState() {
  40. super.initState();
  41. ///移除定位监听
  42. if (null != _locationListener) {
  43. _locationListener.cancel();
  44. }
  45. ///销毁定位
  46. if (null != _locationPlugin) {
  47. _locationPlugin.destroy();
  48. }
  49. _locationListener = _locationPlugin
  50. .onLocationChanged()
  51. .listen((Map<String, Object> result) {
  52. setState(() {
  53. _locationPlugin.stopLocation();
  54. _locationResult = result;
  55. // address latitude longitude
  56. latitude = _locationResult["latitude"];
  57. longitude = _locationResult["longitude"];
  58. address = _locationResult["address"];
  59. });
  60. });
  61. initGetLocation();
  62. }
  63. void _setLocationOption() {
  64. if (null != _locationPlugin) {
  65. AMapLocationOption locationOption = new AMapLocationOption();
  66. ///是否单次定位
  67. locationOption.onceLocation = true;
  68. ///是否需要返回逆地理信息
  69. locationOption.needAddress = true;
  70. ///逆地理信息的语言类型
  71. locationOption.geoLanguage = GeoLanguage.DEFAULT;
  72. ///设置Android端连续定位的定位间隔
  73. locationOption.locationInterval = 20000;
  74. ///设置Android端的定位模式<br>
  75. ///可选值:<br>
  76. ///<li>[AMapLocationMode.Battery_Saving]</li>
  77. ///<li>[AMapLocationMode.Device_Sensors]</li>
  78. ///<li>[AMapLocationMode.Hight_Accuracy]</li>
  79. locationOption.locationMode = AMapLocationMode.Hight_Accuracy;
  80. ///设置iOS端的定位最小更新距离<br>
  81. locationOption.distanceFilter = -1;
  82. ///设置iOS端期望的定位精度
  83. /// 可选值:<br>
  84. /// <li>[DesiredAccuracy.Best] 最高精度</li>
  85. /// <li>[DesiredAccuracy.BestForNavigation] 适用于导航场景的高精度 </li>
  86. /// <li>[DesiredAccuracy.NearestTenMeters] 10米 </li>
  87. /// <li>[DesiredAccuracy.Kilometer] 1000米</li>
  88. /// <li>[DesiredAccuracy.ThreeKilometers] 3000米</li>
  89. locationOption.desiredAccuracy = DesiredAccuracy.NearestTenMeters;
  90. ///设置iOS端是否允许系统暂停定位
  91. locationOption.pausesLocationUpdatesAutomatically = false;
  92. ///将定位参数设置给定位插件
  93. _locationPlugin.setLocationOption(locationOption);
  94. }
  95. }
  96. VideoPlayerController _controller;
  97. ///选择图片
  98. void selectPicker() {
  99. showDialog(
  100. context: context,
  101. builder: (BuildContext context) {
  102. return SimpleDialog(
  103. title: Text("选择方式"),
  104. children: ["拍照", '从手机相册选择'].map((String value) {
  105. print("$value");
  106. return SimpleDialogOption(
  107. child: Text(
  108. "${value}",
  109. style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
  110. ),
  111. onPressed: () {
  112. _getImage(value == '拍照' ? 1 : 0);
  113. Navigator.of(context).pop();
  114. },
  115. );
  116. }).toList(),
  117. );
  118. },
  119. );
  120. }
  121. void _getImage(int key) async {
  122. try {
  123. var _imageFile = await ImagePicker.pickVideo(
  124. source: key == 1 ? ImageSource.camera : ImageSource.gallery,
  125. );
  126. if (_imageFile != null) {
  127. upLoadFileOnce(_imageFile.path);
  128. }
  129. } catch (e) {
  130. toasts("没有权限,无法打开相册!");
  131. }
  132. }
  133. upLoadFileOnce(path) {
  134. showLoading(context, "正在上传...");
  135. NewApiService().upload(path, onSuccess: (res) {
  136. dismissLoading(context);
  137. setState(() {
  138. videoUrl = res.pathUrl;
  139. imagesUrl = res.coverUrl;
  140. });
  141. }, onError: (code, msg) {
  142. dismissLoading(context);
  143. toasts(msg);
  144. });
  145. }
  146. // 获取位置信息
  147. initGetLocation() async {
  148. if (await requestPermission()) {
  149. if (null != _locationPlugin) {
  150. ///开始定位之前设置定位参数
  151. _setLocationOption();
  152. _locationPlugin.startLocation();
  153. }
  154. }
  155. }
  156. // 获取定位权限
  157. Future<bool> requestPermission() async {
  158. final permissions = await PermissionHandler()
  159. .requestPermissions([PermissionGroup.location]);
  160. if (permissions[PermissionGroup.location] == PermissionStatus.granted) {
  161. return true;
  162. } else {
  163. toasts('需要定位权限!');
  164. return false;
  165. }
  166. }
  167. // 获取位置信息
  168. Future<void> getLocation() async {
  169. if (await requestPermission()) {
  170. if (null != _locationPlugin) {
  171. ///开始定位之前设置定位参数
  172. _setLocationOption();
  173. _locationPlugin.startLocation();
  174. if(longitude!=null && latitude!=null){
  175. if (videoUrl == null || videoUrl == '') {
  176. toasts("请上传视频");
  177. return;
  178. }
  179. showLoading(context);
  180. NewApiService().chargeToClock({
  181. "id": widget.id,
  182. "lng": longitude,
  183. "lat": latitude,
  184. "beforeRepair": videoUrl,
  185. }, onSuccess: (res) {
  186. dismissLoading(context);
  187. toasts("确认打卡成功");
  188. NavigatorUtils.push(context, "${WodeRouter.orderPageMaster}?checkType=0");
  189. setState(() {});
  190. }, onError: (code, msg) {
  191. dismissLoading(context);
  192. toasts(msg);
  193. });
  194. }
  195. }
  196. }
  197. }
  198. submitApply() {
  199. getLocation();
  200. }
  201. // 文本编辑控制
  202. GlobalKey _formKey = new GlobalKey<FormState>();
  203. @override
  204. void dispose() {
  205. _controller.pause();
  206. _controller.dispose();
  207. ///移除定位监听
  208. if (null != _locationListener) {
  209. _locationListener.cancel();
  210. }
  211. ///销毁定位
  212. if (null != _locationPlugin) {
  213. _locationPlugin.destroy();
  214. }
  215. // SystemChrome.setPreferredOrientations([
  216. // DeviceOrientation.portraitUp,
  217. // ]);
  218. OrientationPlugin.forceOrientation(DeviceOrientation.portraitUp);
  219. super.dispose();
  220. }
  221. @override
  222. Widget build(BuildContext context) {
  223. double width = MediaQuery.of(context).size.width;
  224. double height = MediaQuery.of(context).size.height;
  225. if(width > height){
  226. // SystemChrome.setPreferredOrientations([
  227. // DeviceOrientation.portraitUp,
  228. // ]);
  229. OrientationPlugin.forceOrientation(DeviceOrientation.portraitUp);
  230. }
  231. return Scaffold(
  232. resizeToAvoidBottomPadding: false, //不让键盘弹上去
  233. appBar: MyAppBar(
  234. centerTitle: "打卡",
  235. ),
  236. body: Container(
  237. child: ListView(
  238. children: <Widget>[
  239. Form(
  240. key: _formKey, //设置globalKey,用于后面获取FormState
  241. // autovalidate: true, //开启自动校验
  242. child: Column(
  243. children: <Widget>[
  244. Container(
  245. width: width,
  246. padding: EdgeInsets.only(left:15,top:15,bottom:15),
  247. child: Text(
  248. "(建议时长3分钟,建议大小50M)",
  249. style: TextStyle(
  250. color: Colors.red,
  251. fontSize:
  252. ScreenUtil()
  253. .setSp(14),
  254. ),
  255. textAlign: TextAlign.left,
  256. ),
  257. ),
  258. Container(
  259. color: ThemeUtils.getDialogTextFieldColor(context),
  260. child: GridView.builder(
  261. shrinkWrap: true,
  262. padding: const EdgeInsets.fromLTRB(8.0, 12, 8.0, 12.0),
  263. physics: NeverScrollableScrollPhysics(),
  264. gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
  265. crossAxisCount: 1, childAspectRatio: 1.18),
  266. itemCount: 1,
  267. itemBuilder: (_, index) {
  268. return Stack(
  269. children: <Widget>[
  270. Center(
  271. child: SelectedVideo(
  272. image: videoUrl,
  273. index: index,
  274. videoPlay:videoPlay(),
  275. onTap: () {
  276. selectPicker();
  277. },
  278. ),
  279. ),
  280. videoUrl != null
  281. ? Positioned(
  282. top: 0,
  283. right: 0,
  284. child: GestureDetector(
  285. onTap: () {
  286. print(index);
  287. videoUrl = null;
  288. imagesUrl = null;
  289. setState(() {});
  290. },
  291. child: Icon(
  292. IconData(0xe62a, fontFamily: "myfont"),
  293. size: 24.0,
  294. color: Color(0xff999999),
  295. ),
  296. ),
  297. )
  298. : Container(
  299. child: null,
  300. )
  301. ],
  302. );
  303. },
  304. ),
  305. ),
  306. Container(
  307. width: width,
  308. padding: EdgeInsets.all(15),
  309. child: Text(
  310. "当前打卡位置:"+ (address??""),
  311. textAlign: TextAlign.start,
  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 Container(
  359. width: width,
  360. height: width*2/3,
  361. padding: EdgeInsets.only(
  362. left: ScreenUtil().setWidth(15),
  363. right: ScreenUtil().setWidth(15),
  364. top: ScreenUtil().setWidth(15)),
  365. child: ClipRRect(
  366. borderRadius: BorderRadius.circular(5),
  367. child: new Chewie(
  368. controller: ChewieController(
  369. videoPlayerController:
  370. // VideoPlayerController.network(
  371. // imgFontUrl + detailObj.url
  372. // ),
  373. _controller,
  374. aspectRatio: 3 / 2,
  375. autoPlay: false,
  376. looping: true,
  377. showControls: true,
  378. deviceOrientationsAfterFullScreen:[DeviceOrientation.portraitUp,],
  379. // 占位图
  380. // placeholder: Image.network(
  381. // imgFontUrl+detailObj.cover,
  382. // fit: BoxFit.contain,
  383. // ),
  384. // 是否在 UI 构建的时候就加载视频
  385. autoInitialize: true,
  386. // 拖动条样式颜色
  387. materialProgressColors:
  388. new ChewieProgressColors(
  389. playedColor: Colors.red,
  390. handleColor: Colors.blue,
  391. backgroundColor: Colors.grey,
  392. bufferedColor: Colors.lightGreen,
  393. ),
  394. ),
  395. ),
  396. ));
  397. }
  398. }