representations.dart 9.3 KB

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