video_upload.dart 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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_change.dart';
  12. import 'package:image_picker/image_picker.dart';
  13. import 'dart:io';
  14. import 'package:liftmanager/widgets/bbs_content.dart';
  15. import 'package:flutter_screenutil/flutter_screenutil.dart';
  16. import 'package:flutter_spinkit/flutter_spinkit.dart';
  17. import 'package:video_player/video_player.dart';
  18. import 'package:liftmanager/utils/image_utils.dart';
  19. import 'package:liftmanager/internal/bbs/bbs_router.dart';
  20. import 'package:flustars/flustars.dart' as FlutterStars;
  21. import 'package:liftmanager/common/common.dart';
  22. import 'package:liftmanager/utils/theme_utils.dart';
  23. class VideoUpload extends StatefulWidget {
  24. // BrandPage(this.id);
  25. // final String id;
  26. @override
  27. State<StatefulWidget> createState() {
  28. return VideoUploadState();
  29. }
  30. }
  31. class VideoUploadState extends State<VideoUpload> {
  32. // NewsDetailItem item = NewsDetailItem();
  33. ScrollController _scrollController = new ScrollController();
  34. String brandChiose = '请选择';
  35. String brandName = "品牌";
  36. int brandIdss;
  37. bool sortBool = true;
  38. List<String> imagesUrl = [];
  39. List<String> videoUrl = [];
  40. @override
  41. void initState() {
  42. super.initState();
  43. }
  44. upLoadFileOnce(path) {
  45. showLoading(context, "正在上传...");
  46. NewApiService().upload(path, onSuccess: (res) {
  47. // imagesUrl.add(res.path);
  48. dismissLoading(context);
  49. setState(() {
  50. videoUrl = [];
  51. imagesUrl = [];
  52. videoUrl.add(res.pathUrl);
  53. imagesUrl.add(res.coverUrl);
  54. });
  55. }, onError: (code, msg) {
  56. dismissLoading(context);
  57. toasts(msg);
  58. });
  59. }
  60. List<dynamic> brandList;
  61. Future getBrandList() async {
  62. await NewApiService().getBrandListNoPage(onSuccess: (res) {
  63. if (res != null) {
  64. brandList = res;
  65. setState(() {});
  66. }
  67. }, onError: (code, msg) {
  68. toasts(msg);
  69. });
  70. }
  71. ///选择视频
  72. void selectPicker() {
  73. showDialog(
  74. context: context,
  75. builder: (BuildContext context) {
  76. return SimpleDialog(
  77. title: Text("选择方式"),
  78. children: ["拍照", '从手机相册选择'].map((String value) {
  79. print("$value");
  80. return SimpleDialogOption(
  81. child: Text(
  82. "${value}",
  83. style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
  84. ),
  85. onPressed: () {
  86. _getImage(value == '拍照' ? 1 : 0);
  87. Navigator.of(context).pop();
  88. },
  89. );
  90. }).toList());
  91. });
  92. }
  93. void _getImage(int key) async {
  94. print(key);
  95. print(333);
  96. try {
  97. var _imageFile = await ImagePicker.pickVideo(
  98. source: key == 1 ? ImageSource.camera : ImageSource.gallery,
  99. );
  100. if (_imageFile != null) {
  101. upLoadFileOnce(_imageFile.path);
  102. setState(() {});
  103. }
  104. } catch (e) {
  105. toasts("没有权限,无法打开相册!");
  106. }
  107. }
  108. // 焦点控制
  109. FocusNode focusNode1 = new FocusNode();
  110. GlobalKey _formKey = new GlobalKey<FormState>();
  111. TextEditingController _titleController = new TextEditingController();
  112. TextEditingController _descController = new TextEditingController();
  113. @override
  114. Widget build(BuildContext context) {
  115. double width = MediaQuery.of(context).size.width;
  116. double height = MediaQuery.of(context).size.height;
  117. return Scaffold(
  118. resizeToAvoidBottomPadding: false, //不让键盘弹上去
  119. appBar: MyAppBar(
  120. centerTitle: "上传视频",
  121. ),
  122. body: GestureDetector(
  123. onTap: () {
  124. // 点击空白页面关闭键盘
  125. FocusScope.of(context).requestFocus(focusNode1);
  126. },
  127. child: Stack(
  128. children: <Widget>[
  129. Container(
  130. child: ListView(children: <Widget>[
  131. Form(
  132. key: _formKey, //设置globalKey,用于后面获取FormState
  133. // autovalidate: true, //开启自动校验
  134. child: Column(
  135. children: <Widget>[
  136. ChioseThisRight(
  137. label: "电梯品牌",
  138. value: brandChiose,
  139. fun: () {
  140. setState(() {
  141. // brandChiose = brandListChiose[index];
  142. sortBool = false;
  143. print(5656333);
  144. });
  145. getBrandList();
  146. // Navigator.maybePop(context);
  147. }),
  148. Row(
  149. crossAxisAlignment: CrossAxisAlignment.start,
  150. mainAxisAlignment: MainAxisAlignment.start,
  151. children: <Widget>[
  152. Container(
  153. padding: EdgeInsets.only(
  154. left: ScreenUtil().setWidth(15),
  155. top: ScreenUtil().setWidth(10),
  156. bottom: ScreenUtil().setWidth(5)),
  157. child: Text(
  158. "视频标题",
  159. style: TextStyle(
  160. // fontSize:ScreenUtil().setSp(14),
  161. ),
  162. textAlign: TextAlign.left,
  163. ),
  164. ),
  165. ],
  166. ),
  167. Container(
  168. height: 80,
  169. padding: EdgeInsets.only(
  170. left: ScreenUtil().setWidth(15),
  171. right: ScreenUtil().setWidth(15),
  172. bottom: ScreenUtil().setWidth(20)),
  173. child: TextFormField(
  174. // autofocus: true,
  175. maxLength: 50,
  176. cursorColor: Color(0xffcccccc),
  177. controller: _titleController,
  178. maxLines: 5,
  179. decoration: InputDecoration(
  180. contentPadding: EdgeInsets.all(0),
  181. hintText: '请输入你上传视频的标题',
  182. hintStyle: TextStyle(color: Color(0xffcccccc)),
  183. focusedBorder: InputBorder.none,
  184. border: InputBorder.none,
  185. // filled: true, // 背景色
  186. // fillColor: Colors.cyan.withAlpha(35),
  187. // icon: Icon(Icons.person)
  188. ),
  189. // 校验
  190. validator: (val) {
  191. return val.trim().length > 0 ? null : "不能为空";
  192. }),
  193. ),
  194. SizedBox(
  195. height: 6,
  196. child: Container(color: ThemeUtils.getDialogTextFieldColor(context)),
  197. ),
  198. Row(
  199. crossAxisAlignment: CrossAxisAlignment.start,
  200. mainAxisAlignment: MainAxisAlignment.start,
  201. children: <Widget>[
  202. Container(
  203. padding: EdgeInsets.only(
  204. left: ScreenUtil().setWidth(15),
  205. top: ScreenUtil().setWidth(10),
  206. bottom: ScreenUtil().setWidth(5)),
  207. child: Text(
  208. "视频简介",
  209. style: TextStyle(
  210. // fontSize:ScreenUtil().setSp(14),
  211. ),
  212. textAlign: TextAlign.left,
  213. ),
  214. ),
  215. ],
  216. ),
  217. Container(
  218. height: 120,
  219. padding: EdgeInsets.only(
  220. left: ScreenUtil().setWidth(15),
  221. right: ScreenUtil().setWidth(15),
  222. bottom: ScreenUtil().setWidth(20)),
  223. child: TextFormField(
  224. // autofocus: true,
  225. maxLength: 500,
  226. cursorColor: Color(0xffcccccc),
  227. controller: _descController,
  228. maxLines: 5,
  229. decoration: InputDecoration(
  230. contentPadding: EdgeInsets.all(0),
  231. hintText: '请输入您上传视频的简介',
  232. hintStyle: TextStyle(color: Color(0xffcccccc)),
  233. focusedBorder: InputBorder.none,
  234. border: InputBorder.none,
  235. // filled: true, // 背景色
  236. // fillColor: Colors.cyan.withAlpha(35),
  237. // icon: Icon(Icons.person)
  238. ),
  239. // 校验
  240. validator: (val) {
  241. return val.trim().length > 0 ? null : "不能为空";
  242. }),
  243. ),
  244. SizedBox(
  245. height: 6,
  246. child: Container(color: ThemeUtils.getDialogTextFieldColor(context)),
  247. ),
  248. Container(
  249. width: width,
  250. padding: EdgeInsets.only(left:15,top:15),
  251. child: Text(
  252. "(建议时长3分钟,建议大小50M)",
  253. style: TextStyle(
  254. color: Colors.red,
  255. fontSize:
  256. ScreenUtil()
  257. .setSp(14),
  258. ),
  259. textAlign: TextAlign.left,
  260. ),
  261. ),
  262. Container(
  263. color: ThemeUtils.getTabsBg(context),
  264. child: GridView.builder(
  265. shrinkWrap: true,
  266. padding:
  267. const EdgeInsets.fromLTRB(8.0, 12, 8.0, 12.0),
  268. physics: NeverScrollableScrollPhysics(),
  269. gridDelegate:
  270. SliverGridDelegateWithFixedCrossAxisCount(
  271. crossAxisCount: 3, childAspectRatio: 1.18),
  272. itemCount:
  273. videoUrl.length >= 1 ? 1 : videoUrl.length + 1,
  274. itemBuilder: (_, index) {
  275. return Stack(
  276. children: <Widget>[
  277. Center(
  278. child: SelectedImage(
  279. image: index < videoUrl.length
  280. ? imagesUrl[index]
  281. : null,
  282. // image: index < videoUrl.length ? videoUrl[index] : null,
  283. index: index,
  284. onTap: () {
  285. if (index >= videoUrl.length) {
  286. selectPicker();
  287. }
  288. FocusScope.of(context)
  289. .requestFocus(FocusNode());
  290. print(index);
  291. print(videoUrl);
  292. }),
  293. ),
  294. index < videoUrl.length
  295. ? Positioned(
  296. top: 0,
  297. right: 0,
  298. child: GestureDetector(
  299. onTap: () {
  300. print(index);
  301. videoUrl.remove(videoUrl[index]);
  302. imagesUrl.remove(imagesUrl[index]);
  303. setState(() {});
  304. },
  305. child: Icon(
  306. IconData(0xe62a,
  307. fontFamily: "myfont"),
  308. size: 24.0,
  309. color: Color(0xff999999),
  310. ),
  311. ))
  312. : Container(
  313. child: null,
  314. )
  315. ],
  316. );
  317. },
  318. )),
  319. SizedBox(
  320. height: ScreenUtil().setWidth(80),
  321. ),
  322. ],
  323. ),
  324. )
  325. ])),
  326. Positioned(
  327. bottom: 0,
  328. left: 0,
  329. child: Container(
  330. width: width,
  331. padding: EdgeInsets.only(
  332. top: ScreenUtil().setWidth(15),
  333. bottom: ScreenUtil().setWidth(15),
  334. left: ScreenUtil().setWidth(25),
  335. right: ScreenUtil().setWidth(25)),
  336. color: ThemeUtils.getDialogTextFieldColor(context),
  337. child: Container(
  338. height: ScreenUtil().setWidth(44),
  339. decoration: BoxDecoration(
  340. borderRadius:
  341. BorderRadius.circular(ScreenUtil().setWidth(22)),
  342. gradient: const LinearGradient(
  343. colors: [Color(0xFF00D9FF), Color(0xFF0287FF)]),
  344. ),
  345. child: FlatButton(
  346. // padding: EdgeInsets.all(15.0),
  347. child: Text("提交"),
  348. textColor: Colors.white,
  349. onPressed: () {
  350. if (brandIdss == null) {
  351. toasts("请选择品牌");
  352. return;
  353. }
  354. if (videoUrl.length < 1) {
  355. toasts("请上传视频");
  356. return;
  357. }
  358. String videos = videoUrl.join(",");
  359. String images = imagesUrl.join(",");
  360. if ((_formKey.currentState as FormState).validate()) {
  361. dynamic obj = {
  362. "brandId": brandIdss,
  363. "title": _titleController.text,
  364. "descr": _descController.text,
  365. "url": videos,
  366. "cover": images,
  367. "checkFlag": 2,
  368. "statuz": 1,
  369. "platformFlag": 1,
  370. "userId":FlutterStars.SpUtil.getString(Constant.userId),
  371. };
  372. showLoading(context, "正在提交...");
  373. NewApiService().addVideo(obj, onSuccess: (res) {
  374. dismissLoading(context);
  375. toasts("提交成功");
  376. Navigator.pop(context);
  377. }, onError: (code, msg) {
  378. dismissLoading(context);
  379. toasts(msg);
  380. });
  381. }
  382. },
  383. ),
  384. ),
  385. )),
  386. !sortBool
  387. ? Positioned(
  388. top: 0,
  389. left: 0,
  390. child: Container(
  391. width: width,
  392. height: height,
  393. color: Color.fromRGBO(0, 0, 0, 0.5)))
  394. : Container(child: null),
  395. !sortBool
  396. ? Positioned(
  397. top: 0,
  398. right: 0,
  399. child: Container(
  400. width: width / 4 * 3,
  401. height: height,
  402. color: Colors.white,
  403. padding: EdgeInsets.all(10),
  404. child: ListView(
  405. children: <Widget>[
  406. Container(
  407. padding:
  408. EdgeInsets.only(left: 5, top: 5, bottom: 10),
  409. child: Text(
  410. "品牌",
  411. style: TextStyle(
  412. color: Color(0xff666666),
  413. fontSize: ScreenUtil().setSp(16)),
  414. textAlign: TextAlign.start,
  415. ),
  416. ),
  417. brandList != null && brandList != []
  418. ? Container(
  419. child: Wrap(
  420. alignment: WrapAlignment.start,
  421. crossAxisAlignment:
  422. WrapCrossAlignment.center,
  423. children:
  424. brandList.asMap().keys.map((index) {
  425. return InkWell(
  426. onTap: () {
  427. print("666");
  428. setState(() {
  429. sortBool = true;
  430. brandChiose =
  431. brandList[index].name ?? "";
  432. brandIdss = brandList[index].id;
  433. });
  434. },
  435. child: Container(
  436. width: width / 4 - 17,
  437. padding: EdgeInsets.only(
  438. bottom: ScreenUtil().setWidth(10),
  439. top: ScreenUtil().setWidth(10)),
  440. margin: EdgeInsets.only(
  441. left: 5,
  442. right: 5,
  443. bottom: 5,
  444. top: 5),
  445. decoration: BoxDecoration(
  446. // border: Border(
  447. // bottom: BorderSide(width: 0.5, color: Colours.line),
  448. // ),
  449. color: Color(0xfff5f5f5)),
  450. child: Text(
  451. brandList[index].name ?? "",
  452. style: TextStyle(
  453. color: Color(0xff666666),
  454. fontSize:
  455. ScreenUtil().setSp(15)),
  456. textAlign: TextAlign.center,
  457. ),
  458. ),
  459. );
  460. }).toList(),
  461. ))
  462. : loadCircle()
  463. ],
  464. )))
  465. : Container(child: null),
  466. ],
  467. ),
  468. ),
  469. );
  470. }
  471. Widget loadCircle() {
  472. return Container(
  473. padding: EdgeInsets.only(top: 10, bottom: 10),
  474. color: ThemeUtils.getTabsBg(context),
  475. child: Center(
  476. child: SpinKitFadingCircle(
  477. color: Colors.blueAccent,
  478. size: 30.0,
  479. ),
  480. ),
  481. );
  482. }
  483. }
  484. class ChioseThisRight extends StatelessWidget {
  485. ChioseThisRight(
  486. {Key key, this.value, this.label, this.fun, this.labelText = '请选择'})
  487. : super(key: key);
  488. String value;
  489. String label;
  490. Function fun;
  491. String labelText;
  492. @override
  493. Widget build(BuildContext context) {
  494. double width = MediaQuery.of(context).size.width;
  495. return InkWell(
  496. onTap: () {
  497. fun();
  498. },
  499. child: Container(
  500. padding: EdgeInsets.only(
  501. top: ScreenUtil().setWidth(15), bottom: ScreenUtil().setWidth(15)),
  502. margin: EdgeInsets.only(left: ScreenUtil().setWidth(15)),
  503. decoration: BoxDecoration(
  504. border: Border(
  505. bottom: BorderSide(width: 0.5, color: Colours.line),
  506. ),
  507. ),
  508. child: Row(
  509. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  510. children: <Widget>[
  511. Text(
  512. label,
  513. style: TextStyle(
  514. // fontSize: ScreenUtil().setSp(14)
  515. ),
  516. textAlign: TextAlign.start,
  517. ),
  518. value.isEmpty
  519. ? Container(
  520. child: Row(children: <Widget>[
  521. Text(
  522. labelText,
  523. style: TextStyle(
  524. color: Color(0xffcccccc),
  525. // fontSize: ScreenUtil().setSp(14)
  526. ),
  527. textAlign: TextAlign.start,
  528. ),
  529. Container(
  530. padding: EdgeInsets.only(top: 3),
  531. child: Icon(
  532. Icons.keyboard_arrow_right,
  533. size: 20.0,
  534. color: Color(0xffcccccc),
  535. ),
  536. ),
  537. SizedBox(width: 10)
  538. ]),
  539. )
  540. : Container(
  541. padding:
  542. EdgeInsets.only(right: ScreenUtil().setWidth(15)),
  543. child: Row(children: <Widget>[
  544. Text(
  545. value,
  546. style: TextStyle(
  547. color: Color(0xff222222),
  548. fontSize: ScreenUtil().setSp(14)),
  549. textAlign: TextAlign.start,
  550. ),
  551. ]),
  552. ),
  553. ]),
  554. ),
  555. );
  556. }
  557. }