representations.dart 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_screenutil/flutter_screenutil.dart';
  3. import 'package:image_picker/image_picker.dart';
  4. import 'package:liftmanager/internal/wode/wode_router.dart';
  5. import 'package:liftmanager/net/api_service.dart';
  6. import 'package:liftmanager/res/colors.dart';
  7. import 'package:liftmanager/routers/fluro_navigator.dart';
  8. import 'package:liftmanager/utils/theme_utils.dart';
  9. import 'package:liftmanager/utils/toast.dart';
  10. import 'package:liftmanager/widgets/app_bar.dart';
  11. import 'package:liftmanager/widgets/selected_image_change.dart';
  12. class Representations extends StatefulWidget {
  13. Representations(this.id);
  14. final String id;
  15. @override
  16. State<StatefulWidget> createState() {
  17. return RepresentationsState();
  18. }
  19. }
  20. class RepresentationsState extends State<Representations> {
  21. List<String> images = [];
  22. @override
  23. void initState() {
  24. super.initState();
  25. }
  26. ///选择图片
  27. void selectPicker() {
  28. showDialog(
  29. context: context,
  30. builder: (BuildContext context) {
  31. return SimpleDialog(
  32. title: Text("选择方式"),
  33. children: ["拍照", '从手机相册选择'].map((String value) {
  34. print("$value");
  35. return SimpleDialogOption(
  36. child: Text(
  37. "${value}",
  38. style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
  39. ),
  40. onPressed: () {
  41. _getImage(value == '拍照' ? 1 : 0);
  42. Navigator.of(context).pop();
  43. },
  44. );
  45. }).toList(),
  46. );
  47. },
  48. );
  49. }
  50. void _getImage(int key) async {
  51. try {
  52. var _imageFile = await ImagePicker.pickImage(
  53. source: key == 1 ? ImageSource.camera : ImageSource.gallery,
  54. maxWidth: 800,
  55. imageQuality: 95);
  56. if (_imageFile != null) {
  57. // images.add(_imageFile);
  58. // setState(() {});
  59. upLoadFileOnce(_imageFile.path);
  60. }
  61. } catch (e) {
  62. toasts("没有权限,无法打开相册!");
  63. }
  64. }
  65. upLoadFileOnce(path) {
  66. showLoading(context, "正在上传...");
  67. NewApiService().upload(path, onSuccess: (res) {
  68. // imagesUrl.add(res.pathUrl);
  69. dismissLoading(context);
  70. setState(() {
  71. images.add(res.pathUrl);
  72. });
  73. }, onError: (code, msg) {
  74. dismissLoading(context);
  75. toasts(msg);
  76. });
  77. }
  78. submitApply() {
  79. // _contentController
  80. NewApiService().submitAppeal({
  81. "dataId": int.parse(widget.id),
  82. "content": _contentController.text,
  83. "imgs": images.join(",")
  84. }, onSuccess: (res) {
  85. dismissLoading(context);
  86. toasts("提交成功");
  87. FocusScope.of(context).requestFocus(FocusNode());
  88. NavigatorUtils.push(context, "${WodeRouter.orderPage}?checkType=0");
  89. }, onError: (code, msg) {
  90. dismissLoading(context);
  91. toasts(msg);
  92. });
  93. }
  94. FocusNode blankNode = FocusNode();
  95. // 文本编辑控制
  96. GlobalKey _formKey = new GlobalKey<FormState>();
  97. TextEditingController _contentController = new TextEditingController();
  98. @override
  99. Widget build(BuildContext context) {
  100. double width = MediaQuery.of(context).size.width;
  101. return Scaffold(
  102. resizeToAvoidBottomPadding: false, //不让键盘弹上去
  103. appBar: MyAppBar(
  104. centerTitle: "申诉",
  105. ),
  106. body: GestureDetector(
  107. onTap: () {
  108. // 点击空白页面关闭键盘
  109. FocusScope.of(context).requestFocus(blankNode);
  110. },
  111. child: Container(
  112. child: ListView(
  113. children: <Widget>[
  114. Form(
  115. key: _formKey, //设置globalKey,用于后面获取FormState
  116. // autovalidate: true, //开启自动校验
  117. child: Column(
  118. children: <Widget>[
  119. Row(
  120. children: [
  121. Container(
  122. color: Colours.blue_app_main,
  123. height: 13,
  124. width: 2,
  125. ),
  126. SizedBox(
  127. width: 13,
  128. ),
  129. Text('申诉内容'),
  130. ],
  131. ),
  132. Container(
  133. height: ScreenUtil().setWidth(120),
  134. padding: EdgeInsets.only(
  135. left: 15, right: 15, bottom: 20, top: 10),
  136. child: TextFormField(
  137. // autofocus: true,
  138. maxLength: 500,
  139. cursorColor: Color(0xffcccccc),
  140. controller: _contentController,
  141. maxLines: 5,
  142. decoration: InputDecoration(
  143. contentPadding: EdgeInsets.all(0),
  144. hintText: '请详细描述您想申诉的内容',
  145. hintStyle: TextStyle(color: Color(0xffcccccc)),
  146. focusedBorder: InputBorder.none,
  147. border: InputBorder.none,
  148. // filled: true, // 背景色
  149. // fillColor: Colors.cyan.withAlpha(35),
  150. // icon: Icon(Icons.person)
  151. ),
  152. // 校验
  153. validator: (val) {
  154. return val.trim().length > 0 ? null : "不能为空";
  155. },
  156. ),
  157. ),
  158. SizedBox(
  159. height: 6,
  160. child: Container(color: Color(0xFFF1F4FC)),
  161. ),
  162. SizedBox(
  163. height: 6,
  164. ),
  165. Row(
  166. children: [
  167. Container(
  168. color: Colours.blue_app_main,
  169. height: 13,
  170. width: 2,
  171. ),
  172. SizedBox(
  173. width: 13,
  174. ),
  175. Text('上传图片'),
  176. ],
  177. ),
  178. Container(
  179. color: ThemeUtils.getTabsBg(context),
  180. child: GridView.builder(
  181. shrinkWrap: true,
  182. padding: const EdgeInsets.fromLTRB(8.0, 12, 8.0, 12.0),
  183. physics: NeverScrollableScrollPhysics(),
  184. gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
  185. crossAxisCount: 3, childAspectRatio: 1.18),
  186. itemCount: images.length >= 6 ? 6 : images.length + 1,
  187. itemBuilder: (_, index) {
  188. return Stack(
  189. children: <Widget>[
  190. Center(
  191. child: SelectedImage(
  192. image: index < images.length
  193. ? images[index]
  194. : null,
  195. onTap: () {
  196. if (index >= images.length) {
  197. selectPicker();
  198. }
  199. FocusScope.of(context)
  200. .requestFocus(FocusNode());
  201. },
  202. ),
  203. ),
  204. index < images.length
  205. ? Positioned(
  206. top: 0,
  207. right: 0,
  208. child: GestureDetector(
  209. onTap: () {
  210. print(index);
  211. images.remove(images[index]);
  212. setState(() {});
  213. },
  214. child: Icon(
  215. const IconData(0xe651,
  216. fontFamily: "Iconfont"),
  217. size: 24.0,
  218. color: Color(0xff999999),
  219. ),
  220. ))
  221. : Container(
  222. child: null,
  223. )
  224. ],
  225. );
  226. },
  227. ),
  228. ),
  229. Container(
  230. height: 44,
  231. decoration: BoxDecoration(
  232. color: Colours.blue_app_main,
  233. borderRadius: BorderRadius.circular(22),
  234. // gradient: const LinearGradient(
  235. // colors: [Color(0xFF00D9FF), Color(0xFF0287FF)]),
  236. ),
  237. margin: EdgeInsets.all(20.0),
  238. width: double.infinity,
  239. child: FlatButton(
  240. // padding: EdgeInsets.all(15.0),
  241. child: Text("提交"),
  242. // color: Theme
  243. // .of(context)
  244. // .primaryColor,
  245. textColor: Colors.white,
  246. onPressed: () {
  247. /*
  248. * 如果:context不对。可以使用GlobalKey,
  249. * 通过_formKey.currentState 获取FormState后,
  250. * 调用validate()方法校验用户名密码是否合法,校验
  251. * 通过后再提交数据。
  252. */
  253. if ((_formKey.currentState as FormState).validate()) {
  254. submitApply();
  255. }
  256. },
  257. ),
  258. ),
  259. ],
  260. ),
  261. ),
  262. ],
  263. ),
  264. ),
  265. ),
  266. );
  267. }
  268. }