punchin.dart 15 KB

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