video_detail.dart 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. import 'package:flutter/material.dart';
  2. import 'package:liftmanager/res/gaps.dart';
  3. import 'package:liftmanager/net/api_service.dart';
  4. import 'package:liftmanager/utils/toast.dart';
  5. import 'package:liftmanager/widgets/app_bar.dart';
  6. import 'package:liftmanager/internal/search/search_router.dart';
  7. import 'package:liftmanager/widgets/app_search_bar.dart';
  8. import 'package:liftmanager/res/resources.dart';
  9. import 'package:liftmanager/routers/fluro_navigator.dart';
  10. import 'package:liftmanager/widgets/load_image.dart';
  11. import 'package:liftmanager/widgets/selected_image.dart';
  12. import 'package:image_picker/image_picker.dart';
  13. import 'package:liftmanager/internal/bbs/bbs_router.dart';
  14. import 'dart:io';
  15. import 'package:flutter_screenutil/flutter_screenutil.dart';
  16. import 'package:video_player/video_player.dart';
  17. import 'package:chewie/chewie.dart';
  18. import 'package:liftmanager/internal/bbs/model/video_detail.dart';
  19. import 'package:liftmanager/utils/url.dart';
  20. import 'package:liftmanager/utils/time_format.dart';
  21. import 'package:liftmanager/utils/fast_notification.dart';
  22. import 'dart:convert';
  23. import 'package:shared_preferences/shared_preferences.dart';
  24. import 'package:liftmanager/utils/utils.dart';
  25. import 'dart:math';
  26. import 'package:liftmanager/internal/account/account_router.dart';
  27. import 'package:flustars/flustars.dart' as FlutterStars;
  28. import 'package:liftmanager/common/common.dart';
  29. class VideoDetail extends StatefulWidget {
  30. VideoDetail(this.id);
  31. final String id;
  32. @override
  33. State<StatefulWidget> createState() {
  34. return VideoDetailState();
  35. }
  36. }
  37. class VideoDetailState extends State<VideoDetail> {
  38. @override
  39. void initState() {
  40. super.initState();
  41. getVideoDetail();
  42. }
  43. bool _hasData = false;
  44. VideoDetailModel detailObj;
  45. // var _storageLikeId;
  46. // var _storageFavId;
  47. VideoPlayerController _controller;
  48. Future getVideoDetail() async {
  49. await NewApiService().getVideoDetail(int.parse(widget.id), 1,
  50. onSuccess: (res) {
  51. if (res != null) {
  52. _hasData = true;
  53. detailObj = res;
  54. _controller = VideoPlayerController.network(
  55. Utils.getImagePath(detailObj.url)
  56. // imgFontUrl + detailObj.url
  57. );
  58. setState(() {});
  59. }
  60. }, onError: (code, msg) {
  61. toasts(msg);
  62. });
  63. }
  64. Future changeLike(type) async {
  65. await NewApiService().videoLike(detailObj.id, type, 1, onSuccess: (res) {
  66. // if (res != null) {
  67. // // saveLikeId(res.id,type);
  68. // }
  69. String textLike;
  70. if (type == 2) {
  71. textLike = "点赞";
  72. } else if (type == 3) {
  73. textLike = "收藏";
  74. initCollect();
  75. }
  76. toasts("$textLike成功");
  77. getVideoDetail();
  78. }, onError: (code, msg) {
  79. toasts(msg);
  80. });
  81. }
  82. showAlertEvent(){
  83. showAlert(
  84. context,
  85. "提示",
  86. "确定登录?",
  87. "确定",
  88. () {
  89. NavigatorUtils.push(context, AccountRouter.loginPage, clearStack: true);
  90. },
  91. txt2: "取消",
  92. onPre2: () {
  93. NavigatorUtils.goBack(context);
  94. },
  95. );
  96. }
  97. Future cancelLike(type) async {
  98. // _storageFavId
  99. // _storageLikeId
  100. var idChiose;
  101. String textLike;
  102. if (type == 2) {
  103. idChiose = detailObj.likeId;
  104. textLike = "点赞";
  105. } else if (type == 3) {
  106. idChiose = detailObj.favoriteId;
  107. textLike = "收藏";
  108. }
  109. await NewApiService().videoLikeCancel(idChiose, onSuccess: (res) {
  110. toasts("取消$textLike成功");
  111. if(type == 3){
  112. initCollect();
  113. }
  114. getVideoDetail();
  115. }, onError: (code, msg) {
  116. toasts(msg);
  117. });
  118. }
  119. // Future saveLikeId(id,type) async {
  120. // SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
  121. // if(type==2){
  122. // sharedPreferences.setInt(
  123. // "video_like_id", id);
  124. // }else if(type==3){
  125. // sharedPreferences.setInt(
  126. // "video_fav_id", id);
  127. // }
  128. // }
  129. // Future getLikeCancelId(type) async {
  130. // SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
  131. // if(type==2){
  132. // _storageLikeId = sharedPreferences.get("video_like_id");
  133. // cancelLike(type);
  134. // }else if(type==3){
  135. // _storageFavId = sharedPreferences.get("video_fav_id");
  136. // cancelLike(type);
  137. // }
  138. // }
  139. initCollect(){
  140. String collectInit = randomInt(1111,9999).toString() + DateTime.now().millisecondsSinceEpoch.toString();
  141. FastNotification.push("collectAction",collectInit);
  142. }
  143. randomInt(int min, int max) {
  144. return new Random().nextInt(max) % (max - min + 1) + min;
  145. }
  146. @override
  147. void dispose() {
  148. _controller.pause();
  149. _controller.dispose();
  150. super.dispose();
  151. }
  152. @override
  153. Widget build(BuildContext context) {
  154. double width = MediaQuery.of(context).size.width;
  155. return Scaffold(
  156. resizeToAvoidBottomPadding: false, //不让键盘弹上去
  157. appBar: MyAppBar(
  158. centerTitle: "电梯学堂",
  159. ),
  160. body: _hasData
  161. ? Stack(
  162. children: <Widget>[
  163. Container(
  164. child: ListView(children: <Widget>[
  165. Column(
  166. mainAxisAlignment: MainAxisAlignment.start,
  167. crossAxisAlignment: CrossAxisAlignment.start,
  168. children: <Widget>[
  169. Container(
  170. padding: EdgeInsets.only(
  171. left: ScreenUtil().setWidth(15),
  172. right: ScreenUtil().setWidth(15),
  173. top: ScreenUtil().setWidth(15)),
  174. child: ClipRRect(
  175. borderRadius: BorderRadius.circular(5),
  176. child: new Chewie(
  177. controller: ChewieController(
  178. videoPlayerController:
  179. // VideoPlayerController.network(
  180. // imgFontUrl + detailObj.url
  181. // ),
  182. _controller,
  183. aspectRatio: 3 / 2,
  184. autoPlay: false,
  185. looping: true,
  186. showControls: true,
  187. // 占位图
  188. // placeholder: Image.network(
  189. // imgFontUrl+detailObj.cover,
  190. // fit: BoxFit.contain,
  191. // ),
  192. // 是否在 UI 构建的时候就加载视频
  193. autoInitialize: true,
  194. // 拖动条样式颜色
  195. materialProgressColors:
  196. new ChewieProgressColors(
  197. playedColor: Colors.red,
  198. handleColor: Colors.blue,
  199. backgroundColor: Colors.grey,
  200. bufferedColor: Colors.lightGreen,
  201. ),
  202. ),
  203. ),
  204. )),
  205. Container(
  206. width: width,
  207. padding: EdgeInsets.only(
  208. left: ScreenUtil().setWidth(15),
  209. right: ScreenUtil().setWidth(15),
  210. top: ScreenUtil().setWidth(10),
  211. bottom: ScreenUtil().setWidth(10)),
  212. decoration: BoxDecoration(
  213. border: Border(
  214. bottom: BorderSide(width: 0.5, color: Colours.line),
  215. ),
  216. ),
  217. child: Row(
  218. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  219. crossAxisAlignment: CrossAxisAlignment.start,
  220. children:<Widget>[
  221. Column(
  222. mainAxisAlignment: MainAxisAlignment.start,
  223. crossAxisAlignment: CrossAxisAlignment.start,
  224. children: <Widget>[
  225. Container(
  226. width: width*0.7,
  227. child: Text(
  228. detailObj.title??"",
  229. style: TextStyle(
  230. fontSize: ScreenUtil().setSp(16)),
  231. textAlign: TextAlign.start,
  232. maxLines: 1,
  233. overflow: TextOverflow.ellipsis,
  234. ),
  235. ),
  236. Text(
  237. detailObj.brandName??"",
  238. style: TextStyle(
  239. color: Color(0xff666666),
  240. fontSize: ScreenUtil().setSp(14)),
  241. textAlign: TextAlign.start,
  242. ),
  243. ]
  244. ),
  245. Row(
  246. children: <Widget>[
  247. Row(
  248. crossAxisAlignment: CrossAxisAlignment.start,
  249. children: <Widget>[
  250. Text(
  251. "${detailObj.likeNum??"0"}",
  252. style: TextStyle(
  253. color: Color(0xff999999),
  254. fontSize: ScreenUtil().setSp(14)),
  255. textAlign: TextAlign.start,
  256. ),
  257. SizedBox(
  258. width:3
  259. ),
  260. Icon(
  261. IconData(0xe7cd,
  262. fontFamily: "myfont"),
  263. size: 14.0,
  264. color: Color(0xff999999),
  265. ),
  266. ],
  267. ),
  268. SizedBox(
  269. width:15
  270. ),
  271. Row(
  272. crossAxisAlignment: CrossAxisAlignment.start,
  273. children: <Widget>[
  274. Text(
  275. "${detailObj.browseNum??"0"}",
  276. style: TextStyle(
  277. color: Color(0xff999999),
  278. fontSize: ScreenUtil().setSp(14)),
  279. textAlign: TextAlign.start,
  280. ),
  281. SizedBox(
  282. width:3
  283. ),
  284. Icon(
  285. IconData(0xe610,
  286. fontFamily: "myfont"),
  287. size: 14.0,
  288. color: Color(0xff999999),
  289. ),
  290. ],
  291. ),
  292. ],
  293. ),
  294. // Text(
  295. // detailObj.likeNum!=null?detailObj.likeNum.toString() + "赞":"0赞",
  296. // style: TextStyle(
  297. // color: Color(0xff999999),
  298. // fontSize: ScreenUtil().setSp(14)),
  299. // textAlign: TextAlign.start,
  300. // ),
  301. ]
  302. )
  303. ),
  304. Container(
  305. padding: EdgeInsets.only(
  306. left: ScreenUtil().setWidth(15),
  307. top: ScreenUtil().setWidth(30),
  308. bottom: ScreenUtil().setWidth(30)),
  309. decoration: BoxDecoration(
  310. border: Border(
  311. bottom: BorderSide(width: 0.5, color: Colours.line),
  312. ),
  313. ),
  314. child: Row(
  315. children: <Widget>[
  316. Container(
  317. child: ClipRRect(
  318. borderRadius: BorderRadius.circular(
  319. ScreenUtil().setWidth(18)),
  320. child: LoadNetworkImage(
  321. detailObj.avatarUrl,
  322. // fit: BoxFit.cover,
  323. width: ScreenUtil().setWidth(36),
  324. height: ScreenUtil().setWidth(36),
  325. ),
  326. )),
  327. Container(
  328. padding: EdgeInsets.only(left: 5),
  329. child: Column(
  330. crossAxisAlignment: CrossAxisAlignment.start,
  331. children: <Widget>[
  332. Text(
  333. detailObj.nickname??"",
  334. style: TextStyle(
  335. fontSize: ScreenUtil().setSp(14),
  336. color: Color(0xff666666)),
  337. ),
  338. Text(
  339. detailObj.createTime!=null?DateUtils.instance.getFormartData(
  340. timeSamp: detailObj.createTime,
  341. format: "yyyy-MM-dd"):"",
  342. style: TextStyle(
  343. fontSize: ScreenUtil().setSp(12),
  344. color: Color(0xffaaaaaa)),
  345. ),
  346. ],
  347. ),
  348. )
  349. ],
  350. ),
  351. ),
  352. Column(
  353. crossAxisAlignment: CrossAxisAlignment.start,
  354. mainAxisAlignment: MainAxisAlignment.start,
  355. children: <Widget>[
  356. Container(
  357. padding: EdgeInsets.only(
  358. left: ScreenUtil().setWidth(15),
  359. top: ScreenUtil().setWidth(10),
  360. bottom: ScreenUtil().setWidth(5)),
  361. child: Text(
  362. "视频简介",
  363. style: TextStyle(
  364. fontSize: ScreenUtil().setSp(16),
  365. ),
  366. textAlign: TextAlign.left,
  367. ),
  368. ),
  369. Container(
  370. padding: EdgeInsets.only(
  371. left: ScreenUtil().setWidth(15),
  372. right: ScreenUtil().setWidth(15),
  373. top: ScreenUtil().setWidth(10),
  374. bottom: ScreenUtil().setWidth(90)),
  375. child: Text(
  376. detailObj.descr??"",
  377. style: TextStyle(
  378. color: Color(0xff666666),
  379. fontSize: ScreenUtil().setSp(14),
  380. ),
  381. textAlign: TextAlign.left,
  382. ),
  383. ),
  384. ],
  385. ),
  386. ],
  387. ),
  388. ])),
  389. Positioned(
  390. bottom: 0,
  391. left: 0,
  392. child: Container(
  393. width: width,
  394. child: Row(children: <Widget>[
  395. Container(
  396. height: 70,
  397. width: width / 2,
  398. color: Colors.white,
  399. padding: EdgeInsets.only(
  400. left: ScreenUtil().setWidth(10),
  401. right: ScreenUtil().setWidth(10),
  402. top: ScreenUtil().setWidth(10)),
  403. child: Row(
  404. mainAxisAlignment:
  405. MainAxisAlignment.spaceAround,
  406. children: <Widget>[
  407. GestureDetector(
  408. onTap: () {
  409. if(FlutterStars.SpUtil.getString(Constant.userId) == "-1"){
  410. showAlertEvent();
  411. }else {
  412. if (detailObj.isLike == 1) {
  413. cancelLike(2);
  414. } else {
  415. changeLike(2);
  416. }
  417. }
  418. },
  419. child: Container(
  420. child: Column(children: <Widget>[
  421. Icon(
  422. IconData(
  423. detailObj.isLike == 1
  424. ? 0xe63c
  425. : 0xe608,
  426. fontFamily: "myfont"),
  427. size: 26.0,
  428. color: Color(detailObj.isLike == 1
  429. ? 0xff0388FD
  430. : 0xff333333),
  431. ),
  432. Text(
  433. detailObj.isLike == 1 ? "已点赞" : "点赞",
  434. style: TextStyle(
  435. color: Color(detailObj.isLike == 1
  436. ? 0xff0388FD
  437. : 0xff000000),
  438. fontSize: ScreenUtil().setSp(14)),
  439. textAlign: TextAlign.start,
  440. ),
  441. ])),
  442. ),
  443. GestureDetector(
  444. onTap: () {
  445. if(FlutterStars.SpUtil.getString(Constant.userId) == "-1"){
  446. showAlertEvent();
  447. }else {
  448. if (detailObj.isFavorite == 1) {
  449. cancelLike(3);
  450. } else {
  451. changeLike(3);
  452. }
  453. }
  454. },
  455. child: Container(
  456. child: Column(children: <Widget>[
  457. Icon(
  458. IconData(
  459. detailObj.isFavorite == 1
  460. ? 0xe654
  461. : 0xe604,
  462. fontFamily: "myfont"),
  463. size: 26.0,
  464. color: Color(detailObj.isFavorite == 1
  465. ? 0xff0388FD
  466. : 0xff333333),
  467. ),
  468. Text(
  469. detailObj.isFavorite == 1
  470. ? "已收藏"
  471. : "收藏",
  472. style: TextStyle(
  473. color: Color(
  474. detailObj.isFavorite == 1
  475. ? 0xff0388FD
  476. : 0xff000000),
  477. fontSize: ScreenUtil().setSp(14)),
  478. textAlign: TextAlign.start,
  479. ),
  480. ])),
  481. )
  482. ],
  483. )),
  484. Container(
  485. height: ScreenUtil().setWidth(70),
  486. width: width / 2,
  487. color: Color(0xff0388FD),
  488. child: FlatButton(
  489. // padding: EdgeInsets.all(15.0),
  490. child: Text(
  491. "打赏",
  492. style:
  493. TextStyle(fontSize: ScreenUtil().setSp(16)),
  494. ),
  495. textColor: Colors.white,
  496. onPressed: () {
  497. if(FlutterStars.SpUtil.getString(Constant.userId) == "-1"){
  498. showAlertEvent();
  499. }else {
  500. var nickname;
  501. if(detailObj.nickname!=null){
  502. nickname = Uri.encodeComponent(detailObj.nickname);
  503. }
  504. NavigatorUtils.push(
  505. context, "${BbsRouter.questionPay}?id=${detailObj.id}&type=video&name=$nickname");
  506. }
  507. },
  508. ),
  509. ),
  510. ])))
  511. ],
  512. )
  513. : Center(
  514. child: Text("正在加载..."),
  515. ),
  516. );
  517. }
  518. }