You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

284 lines
9.7KB

  1. import 'package:farm_tpf/custom_model/Nursery.dart';
  2. import 'package:farm_tpf/data/repository/repository.dart';
  3. import 'package:farm_tpf/utils/const_string.dart';
  4. import 'package:farm_tpf/utils/const_style.dart';
  5. import 'package:farm_tpf/utils/pref.dart';
  6. import 'package:farm_tpf/utils/validators.dart';
  7. import 'package:flutter/material.dart';
  8. import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
  9. import 'package:fluttertoast/fluttertoast.dart';
  10. import 'package:intl/intl.dart';
  11. import 'package:keyboard_dismisser/keyboard_dismisser.dart';
  12. import 'package:pattern_formatter/pattern_formatter.dart';
  13. import 'package:farm_tpf/utils/formatter.dart';
  14. class EditActionNursery extends StatefulWidget {
  15. @override
  16. _EditActionNurseryState createState() => _EditActionNurseryState();
  17. }
  18. class _EditActionNurseryState extends State<EditActionNursery> {
  19. final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
  20. final _repository = Repository();
  21. GlobalKey<FormState> _formKey = GlobalKey();
  22. bool _autoValidate = false;
  23. Nursery _nursery = Nursery();
  24. var pref = LocalPref();
  25. FlutterToast flutterToast;
  26. TextEditingController _substrateController = TextEditingController();
  27. TextEditingController _seedLengthController = TextEditingController();
  28. TextEditingController _quantityController = TextEditingController();
  29. TextEditingController _seedIncubationTimeController = TextEditingController();
  30. TextEditingController _descriptionController = TextEditingController();
  31. String executeTimeView;
  32. DateTime executeTime = DateTime.now();
  33. @override
  34. void initState() {
  35. super.initState();
  36. flutterToast = FlutterToast(context);
  37. //UPDATE
  38. if (_nursery != null) {
  39. try {
  40. executeTime =
  41. DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").parse(_nursery.executeDate);
  42. } catch (_) {}
  43. } else {
  44. //ADD NEW
  45. var parsedExecuteDate =
  46. DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(executeTime);
  47. _nursery.executeDate = "$parsedExecuteDate";
  48. }
  49. executeTimeView = DateFormat("dd/MM/yyyy HH:mm").format(executeTime);
  50. }
  51. Widget _btnExecuteTimePicker() {
  52. return FlatButton(
  53. padding: EdgeInsets.only(top: 0.0, right: 0.0, bottom: 0.0, left: 0.0),
  54. onPressed: () {
  55. DatePicker.showDateTimePicker(context,
  56. showTitleActions: true, onChanged: (date) {}, onConfirm: (date) {
  57. setState(() {
  58. var parsedDate =
  59. DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(date);
  60. _nursery.executeDate = "$parsedDate";
  61. executeTimeView = DateFormat("dd/MM/yyyy HH:mm").format(date);
  62. });
  63. }, currentTime: executeTime, locale: LocaleType.vi);
  64. },
  65. child: Container(
  66. padding:
  67. EdgeInsets.only(top: 0.0, right: 0.0, bottom: 10.5, left: 0.0),
  68. decoration: BoxDecoration(
  69. border: kBorderTextField,
  70. ),
  71. child: Row(
  72. children: [
  73. Expanded(
  74. child: Text(
  75. //TODO: check condition
  76. executeTimeView == null ? "$executeTime" : executeTimeView,
  77. style: TextStyle(fontSize: 14.0, color: Colors.black87),
  78. )),
  79. Icon(
  80. Icons.date_range,
  81. color: Colors.blue,
  82. ),
  83. ],
  84. )));
  85. }
  86. Widget _btnSelectSeed() {
  87. return FlatButton(
  88. padding: EdgeInsets.only(top: 0.0, right: 0.0, bottom: 0.0, left: 0.0),
  89. onPressed: () {},
  90. child: Container(
  91. padding:
  92. EdgeInsets.only(top: 0.0, right: 0.0, bottom: 10.5, left: 0.0),
  93. decoration: BoxDecoration(
  94. border: kBorderTextField,
  95. ),
  96. child: Row(
  97. children: [
  98. Expanded(
  99. child: Text(
  100. "Tên giống",
  101. style: TextStyle(fontSize: 14.0, color: Colors.black87),
  102. )),
  103. Icon(
  104. Icons.arrow_drop_down,
  105. color: Colors.grey,
  106. ),
  107. ],
  108. )));
  109. }
  110. Widget _subStratesField() {
  111. return TextFormField(
  112. keyboardType: TextInputType.text,
  113. decoration: InputDecoration(labelText: "Loại giá thể"),
  114. controller: _substrateController,
  115. validator: (String value) {
  116. return Validators.validateNotNullOrEmpty(value, "Loại giá thể");
  117. },
  118. onSaved: (newValue) {
  119. _nursery.substrates = newValue;
  120. },
  121. );
  122. }
  123. Widget _seedLengthField() {
  124. return TextFormField(
  125. keyboardType: TextInputType.numberWithOptions(decimal: true),
  126. inputFormatters: [
  127. ThousandsFormatter(
  128. formatter: NumberFormat("#,###.##", "es"), allowFraction: true)
  129. ],
  130. decoration: InputDecoration(labelText: "Chiều dài mần"),
  131. controller: _seedLengthController,
  132. validator: (String value) {
  133. return Validators.validNumber(value, "Chiều dài mần");
  134. },
  135. onSaved: (newValue) {
  136. _nursery.seedLength = newValue.parseDoubleThousand();
  137. },
  138. );
  139. }
  140. Widget _quantityField() {
  141. return TextFormField(
  142. keyboardType: TextInputType.numberWithOptions(decimal: true),
  143. inputFormatters: [
  144. ThousandsFormatter(
  145. formatter: NumberFormat("#,###.##", "es"), allowFraction: true)
  146. ],
  147. decoration: InputDecoration(labelText: "Số lượng hạt gieo"),
  148. controller: _quantityController,
  149. validator: (String value) {
  150. return Validators.validNumber(value, "Số lượng hạt gieo");
  151. },
  152. onSaved: (newValue) {
  153. _nursery.quantity = newValue.parseDoubleThousand();
  154. },
  155. );
  156. }
  157. Widget _seedIncubationTimeField() {
  158. return TextFormField(
  159. keyboardType: TextInputType.numberWithOptions(decimal: true),
  160. inputFormatters: [
  161. ThousandsFormatter(
  162. formatter: NumberFormat("#,###.##", "es"), allowFraction: true)
  163. ],
  164. decoration: InputDecoration(labelText: "Thời gian ngâm hạt"),
  165. controller: _seedIncubationTimeController,
  166. validator: (String value) {
  167. return Validators.validNumber(value, "Thời gian ngâm hạt");
  168. },
  169. onSaved: (newValue) {
  170. _nursery.seedIncubationTime = newValue.parseDoubleThousand();
  171. },
  172. );
  173. }
  174. Widget _desciptionField() {
  175. return TextFormField(
  176. keyboardType: TextInputType.text,
  177. decoration: InputDecoration(labelText: "Ghi chú"),
  178. controller: _descriptionController,
  179. onSaved: (newValue) {
  180. _nursery.description = newValue;
  181. },
  182. );
  183. }
  184. _actionAppBar() {
  185. IconButton iconButton;
  186. if (1 == 1) {
  187. iconButton = IconButton(
  188. icon: Icon(
  189. Icons.done,
  190. color: Colors.black,
  191. ),
  192. onPressed: () {},
  193. );
  194. return <Widget>[iconButton];
  195. }
  196. return <Widget>[Container()];
  197. }
  198. @override
  199. Widget build(BuildContext context) => KeyboardDismisser(
  200. gestures: [
  201. GestureType.onTap,
  202. GestureType.onPanUpdateDownDirection,
  203. ],
  204. child: Scaffold(
  205. key: _scaffoldKey,
  206. appBar: AppBar(
  207. centerTitle: true,
  208. title: Text(plot_action_nursery),
  209. actions: _actionAppBar()),
  210. body: KeyboardDismisser(
  211. child: Form(
  212. key: _formKey,
  213. autovalidate: _autoValidate,
  214. child: SingleChildScrollView(
  215. padding: EdgeInsets.all(8.0),
  216. child: Column(
  217. children: <Widget>[
  218. Container(
  219. width: double.infinity,
  220. child: Text(
  221. "Ngày thực hiện",
  222. style: TextStyle(
  223. color: Colors.black54, fontSize: 13.0),
  224. ),
  225. ),
  226. _btnExecuteTimePicker(),
  227. SizedBox(
  228. height: 8.0,
  229. ),
  230. Container(
  231. width: double.infinity,
  232. child: Text(
  233. "Tên giống",
  234. style: TextStyle(
  235. color: Colors.black54, fontSize: 13.0),
  236. ),
  237. ),
  238. _btnSelectSeed(),
  239. SizedBox(
  240. height: 8.0,
  241. ),
  242. _subStratesField(),
  243. SizedBox(
  244. height: 8.0,
  245. ),
  246. _seedLengthField(),
  247. SizedBox(
  248. height: 8.0,
  249. ),
  250. _quantityField(),
  251. SizedBox(
  252. height: 8.0,
  253. ),
  254. _seedIncubationTimeField(),
  255. SizedBox(
  256. height: 8.0,
  257. ),
  258. _desciptionField()
  259. ],
  260. ),
  261. )))));
  262. @override
  263. void dispose() {
  264. _substrateController.dispose();
  265. _seedLengthController.dispose();
  266. _quantityController.dispose();
  267. _seedIncubationTimeController.dispose();
  268. _descriptionController.dispose();
  269. super.dispose();
  270. }
  271. }