interrogation_page.dart 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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:flutter_screenutil/flutter_screenutil.dart';
  15. import 'package:liftmanager/internal/bbs/bbs_router.dart';
  16. import 'dart:convert';
  17. import 'package:shared_preferences/shared_preferences.dart';
  18. import 'package:liftmanager/common/common.dart';
  19. import 'package:flustars/flustars.dart' as flustars;
  20. import 'package:liftmanager/utils/theme_utils.dart';
  21. class InterrogationPage extends StatefulWidget {
  22. InterrogationPage(this.brandName,this.brandId);
  23. final String brandName;
  24. final String brandId;
  25. @override
  26. State<StatefulWidget> createState() {
  27. return InterrogationPageState();
  28. }
  29. }
  30. class InterrogationPageState extends State<InterrogationPage> {
  31. // NewsDetailItem item = NewsDetailItem();
  32. List<String> imagesUrl = [];
  33. List<String> imagesUrlVideo = [];
  34. List<String> videoUrl = [];
  35. @override
  36. void initState() {
  37. super.initState();
  38. print(widget.brandName);
  39. print(widget.brandId);
  40. print(65656);
  41. }
  42. //图片
  43. upLoadFileOnce(path) {
  44. showLoading(context, "正在上传...");
  45. NewApiService().upload(path, onSuccess: (res) {
  46. // imagesUrl.add(res.path);
  47. dismissLoading(context);
  48. setState(() {
  49. imagesUrl.add(res.pathUrl);
  50. });
  51. }, onError: (code, msg) {
  52. dismissLoading(context);
  53. toasts(msg);
  54. });
  55. }
  56. //视频
  57. upLoadFileOnceVideo(path) {
  58. showLoading(context, "正在上传...");
  59. NewApiService().upload(path, onSuccess: (res) {
  60. // imagesUrl.add(res.path);
  61. dismissLoading(context);
  62. setState(() {
  63. videoUrl.add(res.pathUrl);
  64. imagesUrlVideo.add(res.coverUrl);
  65. });
  66. }, onError: (code, msg) {
  67. dismissLoading(context);
  68. toasts(msg);
  69. });
  70. }
  71. ///选择图片
  72. void selectPicker(type) {
  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. if(type=="image"){
  87. _getImage(value == '拍照' ? 1 : 0);
  88. }else if (type=="video") {
  89. _getVideo(value == '拍照' ? 1 : 0);
  90. }
  91. Navigator.of(context).pop();
  92. },
  93. );
  94. }).toList());
  95. });
  96. }
  97. void _getImage(int key) async {
  98. try {
  99. var _imageFile = await ImagePicker.pickImage(
  100. source: key == 1 ? ImageSource.camera : ImageSource.gallery,
  101. maxWidth: 800,
  102. imageQuality: 95);
  103. print(_imageFile);
  104. print(3333);
  105. if (_imageFile != null) {
  106. // images.add(_imageFile);
  107. upLoadFileOnce(_imageFile.path);
  108. // setState(() {});
  109. }
  110. } catch (e) {
  111. toasts("没有权限,无法打开相册!");
  112. }
  113. }
  114. void _getVideo(int key) async {
  115. print(key);
  116. print(333);
  117. try {
  118. var _imageFile = await ImagePicker.pickVideo(
  119. source: key == 1 ? ImageSource.camera : ImageSource.gallery,
  120. );
  121. if (_imageFile != null) {
  122. // print(123);
  123. // print(_imageFile);
  124. // print(jsonEncode(_imageFile));
  125. print(123);
  126. upLoadFileOnceVideo(_imageFile.path);
  127. setState(() {});
  128. }
  129. } catch (e) {
  130. toasts("没有权限,无法打开相册!");
  131. }
  132. }
  133. // 焦点控制
  134. // FocusNode _focusNode1 = new FocusNode();
  135. GlobalKey _formKey= new GlobalKey<FormState>();
  136. TextEditingController _questionController = new TextEditingController();
  137. TextEditingController _methodController = new TextEditingController();
  138. FocusNode blankNode = FocusNode();
  139. @override
  140. Widget build(BuildContext context) {
  141. double width = MediaQuery.of(context).size.width;
  142. // 监听FocusNode
  143. // _focusNode1.addListener((){
  144. // // _focusNode1.hasFocus 是否聚焦
  145. // print(_focusNode1.hasFocus);
  146. // });
  147. return Scaffold(
  148. resizeToAvoidBottomPadding: false,//不让键盘弹上去
  149. appBar: MyAppBar(
  150. centerTitle: "我要提问",
  151. ),
  152. body: GestureDetector(
  153. onTap: (){
  154. // 点击空白页面关闭键盘
  155. FocusScope.of(context).requestFocus(blankNode);
  156. },
  157. child: Stack(
  158. children:<Widget>[
  159. Container(
  160. padding: EdgeInsets.only(bottom:ScreenUtil().setWidth(80)),
  161. child: ListView(
  162. children: <Widget>[
  163. Form(
  164. key: _formKey, //设置globalKey,用于后面获取FormState
  165. // autovalidate: true, //开启自动校验
  166. child: Column(
  167. children: <Widget>[
  168. Container(
  169. padding: EdgeInsets.only(left:ScreenUtil().setWidth(15),right:ScreenUtil().setWidth(15),top:ScreenUtil().setWidth(10),bottom:ScreenUtil().setWidth(10)),
  170. decoration: BoxDecoration(
  171. border: Border(
  172. bottom: BorderSide(width: 0.5, color: Colours.line),
  173. ),
  174. ),
  175. child: Row(
  176. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  177. children:<Widget>[
  178. Text(
  179. "电梯品牌",
  180. style: TextStyle(
  181. // fontSize:ScreenUtil().setSp(14)
  182. ),
  183. textAlign:TextAlign.start,
  184. ),
  185. Text(
  186. widget.brandName,
  187. style: TextStyle(
  188. color:Color(0xff999999),
  189. // fontSize:ScreenUtil().setSp(14)
  190. ),
  191. textAlign:TextAlign.start,
  192. ),
  193. ]
  194. ),
  195. ),
  196. Row(
  197. crossAxisAlignment: CrossAxisAlignment.start,
  198. mainAxisAlignment: MainAxisAlignment.start,
  199. children: <Widget>[
  200. Container(
  201. padding:EdgeInsets.only(left:ScreenUtil().setWidth(15),top:ScreenUtil().setWidth(10),bottom:ScreenUtil().setWidth(5)),
  202. child: Text(
  203. "问题描述",
  204. style: TextStyle(
  205. // fontSize:ScreenUtil().setSp(14),
  206. ),
  207. textAlign:TextAlign.left,
  208. ),
  209. ),
  210. ],
  211. ),
  212. Container(
  213. height:120,
  214. padding: EdgeInsets.only(left:ScreenUtil().setWidth(15),right:ScreenUtil().setWidth(15),bottom:ScreenUtil().setWidth(20)),
  215. child: TextFormField(
  216. // autofocus: true,
  217. maxLength: 50,
  218. cursorColor: Color(0xffcccccc),
  219. controller: _questionController,
  220. maxLines:5,
  221. decoration: InputDecoration(
  222. contentPadding: EdgeInsets.all(0),
  223. hintText: '请详细描述事故问题和现场情况',
  224. hintStyle:TextStyle(
  225. color: Color(0xffcccccc),
  226. // fontSize: ScreenUtil().setSp(14),
  227. ),
  228. focusedBorder: InputBorder.none,
  229. border: InputBorder.none,
  230. // filled: true, // 背景色
  231. // fillColor: Colors.cyan.withAlpha(35),
  232. // icon: Icon(Icons.person)
  233. ),
  234. // 校验
  235. validator: (val) {
  236. return val.trim().length > 0 ? null : "不能为空";
  237. }
  238. ),
  239. ),
  240. SizedBox(
  241. height:6,
  242. child: Container(
  243. color:ThemeUtils.getDialogTextFieldColor(context)
  244. ),
  245. ),
  246. Row(
  247. crossAxisAlignment: CrossAxisAlignment.start,
  248. mainAxisAlignment: MainAxisAlignment.start,
  249. children: <Widget>[
  250. Container(
  251. padding:EdgeInsets.only(left:ScreenUtil().setWidth(15),top:ScreenUtil().setWidth(10),bottom:ScreenUtil().setWidth(5)),
  252. child: Text(
  253. "已尝试方法",
  254. style: TextStyle(
  255. // fontSize:ScreenUtil().setSp(14),
  256. ),
  257. textAlign:TextAlign.left,
  258. ),
  259. ),
  260. ],
  261. ),
  262. Container(
  263. height:120,
  264. padding: EdgeInsets.only(left:ScreenUtil().setWidth(15),right:ScreenUtil().setWidth(15),bottom:ScreenUtil().setWidth(20)),
  265. child: TextFormField(
  266. // autofocus: true,
  267. maxLength: 50,
  268. cursorColor: Color(0xffcccccc),
  269. controller: _methodController,
  270. maxLines:5,
  271. decoration: InputDecoration(
  272. contentPadding: EdgeInsets.all(0),
  273. hintText: '请详细描述已尝试方法以及结果',
  274. hintStyle:TextStyle(
  275. color: Color(0xffcccccc),
  276. // fontSize: ScreenUtil().setSp(14),
  277. ),
  278. focusedBorder: InputBorder.none,
  279. border: InputBorder.none,
  280. // filled: true, // 背景色
  281. // fillColor: Colors.cyan.withAlpha(35),
  282. // icon: Icon(Icons.person)
  283. ),
  284. // 校验
  285. validator: (val) {
  286. return val.trim().length > 0 ? null : "不能为空";
  287. }
  288. ),
  289. ),
  290. SizedBox(
  291. height:6,
  292. child: Container(
  293. color:ThemeUtils.getDialogTextFieldColor(context)
  294. ),
  295. ),
  296. Container(
  297. width: width,
  298. padding: EdgeInsets.only(left:15,top:15),
  299. child: Text(
  300. "上传图片:",
  301. style: TextStyle(
  302. fontSize:
  303. ScreenUtil()
  304. .setSp(14),
  305. ),
  306. textAlign: TextAlign.left,
  307. ),
  308. ),
  309. Container(
  310. color: ThemeUtils.getTabsBg(context),
  311. child: GridView.builder(
  312. shrinkWrap: true,
  313. padding: const EdgeInsets.fromLTRB(8.0, 12, 8.0, 12.0),
  314. physics: NeverScrollableScrollPhysics(),
  315. gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
  316. crossAxisCount: 3, childAspectRatio: 1.18),
  317. itemCount: imagesUrl.length >= 9 ? 9 : imagesUrl.length + 1,
  318. itemBuilder: (_, index) {
  319. return Stack(
  320. children: <Widget>[
  321. Center(
  322. child: SelectedImage(
  323. image: index < imagesUrl.length ? imagesUrl[index] : null,
  324. index:index,
  325. onTap: () {
  326. if(index >= imagesUrl.length){
  327. selectPicker("image");
  328. }
  329. FocusScope.of(context).requestFocus(FocusNode());
  330. print(index);
  331. print(imagesUrl);
  332. }),
  333. ),
  334. index < imagesUrl.length?Positioned(
  335. top:0,
  336. right:0,
  337. child:GestureDetector(
  338. onTap: (){
  339. print(index);
  340. imagesUrl.remove(imagesUrl[index]);
  341. setState(() {
  342. });
  343. },
  344. child:Icon(
  345. IconData(
  346. 0xe62a,
  347. fontFamily:"myfont"
  348. ),
  349. size: 24.0,
  350. color:Color(0xff999999),
  351. ),
  352. )
  353. ):Container(child: null,)
  354. ],
  355. );
  356. },
  357. )
  358. ),
  359. SizedBox(
  360. height:6,
  361. child: Container(
  362. color:ThemeUtils.getDialogTextFieldColor(context)
  363. ),
  364. ),
  365. Container(
  366. width: width,
  367. padding: EdgeInsets.only(left:15,top:15),
  368. child: Text(
  369. "上传视频:",
  370. style: TextStyle(
  371. fontSize:
  372. ScreenUtil()
  373. .setSp(14),
  374. ),
  375. textAlign: TextAlign.left,
  376. ),
  377. ),
  378. Container(
  379. width: width,
  380. padding: EdgeInsets.only(left:15),
  381. child: Text(
  382. "(建议时长3分钟,建议大小50M)",
  383. style: TextStyle(
  384. color: Colors.red,
  385. fontSize:
  386. ScreenUtil()
  387. .setSp(14),
  388. ),
  389. textAlign: TextAlign.left,
  390. ),
  391. ),
  392. Container(
  393. color: ThemeUtils.getTabsBg(context),
  394. child: GridView.builder(
  395. shrinkWrap: true,
  396. padding: const EdgeInsets.fromLTRB(8.0, 12, 8.0, 12.0),
  397. physics: NeverScrollableScrollPhysics(),
  398. gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
  399. crossAxisCount: 3, childAspectRatio: 1.18),
  400. itemCount: videoUrl.length >= 1 ? 1 : videoUrl.length + 1,
  401. itemBuilder: (_, index) {
  402. return Stack(
  403. children: <Widget>[
  404. Center(
  405. child: SelectedImage(
  406. image: index < videoUrl.length ? imagesUrlVideo[index] : null,
  407. index:index,
  408. onTap: () {
  409. if(index >= videoUrl.length){
  410. selectPicker("video");
  411. }
  412. FocusScope.of(context).requestFocus(FocusNode());
  413. print(index);
  414. print(videoUrl);
  415. }),
  416. ),
  417. index < videoUrl.length?Positioned(
  418. top:0,
  419. right:0,
  420. child:GestureDetector(
  421. onTap: (){
  422. print(index);
  423. videoUrl.remove(videoUrl[index]);
  424. imagesUrlVideo.remove(imagesUrlVideo[index]);
  425. setState(() {
  426. });
  427. },
  428. child:Icon(
  429. IconData(
  430. 0xe62a,
  431. fontFamily:"myfont"
  432. ),
  433. size: 24.0,
  434. color:Color(0xff999999),
  435. ),
  436. )
  437. ):Container(child: null,)
  438. ],
  439. );
  440. },
  441. )
  442. ),
  443. ],
  444. ),
  445. )
  446. ]
  447. )
  448. ),
  449. Positioned(
  450. bottom:0,
  451. left:0,
  452. child:Container(
  453. width: width,
  454. padding: EdgeInsets.only(top:ScreenUtil().setWidth(15),bottom:ScreenUtil().setWidth(15),left:ScreenUtil().setWidth(25),right:ScreenUtil().setWidth(25)),
  455. color:ThemeUtils.getDialogTextFieldColor(context),
  456. child: Container(
  457. height:ScreenUtil().setWidth(44),
  458. decoration: BoxDecoration(
  459. borderRadius: BorderRadius.circular(22.0),
  460. gradient: const LinearGradient(
  461. colors: [Color(0xFF00D9FF), Color(0xFF0287FF)]),
  462. ),
  463. child: FlatButton(
  464. // padding: EdgeInsets.all(15.0),
  465. child: Text("提交"),
  466. textColor: Colors.white,
  467. // textColor: Colors.white,
  468. onPressed: () {
  469. // if(imagesUrl.length<1){
  470. // toasts("请上传图片");
  471. // return;
  472. // }
  473. String imgs = "";
  474. if(imagesUrl.length > 0){
  475. imgs = imagesUrl.join(",");
  476. }
  477. String videoImgs = "";
  478. if(videoUrl.length > 0){
  479. videoImgs = videoUrl.join(",");
  480. }
  481. if((_formKey.currentState as FormState).validate()){
  482. dynamic obj = {
  483. "dataTable":"1",
  484. "brandId":widget.brandId,
  485. "expression":_questionController.text,
  486. "usedMethods":_methodController.text,
  487. "imgs":imgs,
  488. "videoUrl":videoImgs,
  489. "createUserId":int.parse(flustars.SpUtil.getString(Constant.userId)),
  490. "chargerId":1
  491. };
  492. showLoading(context, "正在提交...");
  493. NewApiService().createCase(obj, onSuccess: (res) {
  494. dismissLoading(context);
  495. toasts("提交成功");
  496. FocusScope.of(context).requestFocus(FocusNode());
  497. print(res);
  498. print(999999);
  499. setDataId(res);
  500. setType("1");
  501. print(widget.brandId);
  502. NavigatorUtils.push(context, "${BbsRouter.expertList}?id=${widget.brandId}");
  503. }, onError: (code, msg) {
  504. dismissLoading(context);
  505. toasts(msg);
  506. });
  507. }
  508. },
  509. ),
  510. ),
  511. )
  512. )
  513. ]
  514. ),
  515. ),
  516. );
  517. }
  518. void setDataId (int id)async{
  519. SharedPreferences prefs = await SharedPreferences.getInstance();
  520. prefs.setInt("dataId", id);
  521. }
  522. void setType (String type)async{
  523. SharedPreferences prefs = await SharedPreferences.getInstance();
  524. prefs.setString("questionType", type);
  525. }
  526. }