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.

233 lines
7.8KB

  1. import 'package:farm_tpf/custom_model/Plant.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 EditActionPlantScreen extends StatefulWidget {
  15. @override
  16. _EditActionPlantScreenState createState() => _EditActionPlantScreenState();
  17. }
  18. class _EditActionPlantScreenState extends State<EditActionPlantScreen> {
  19. final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
  20. final _repository = Repository();
  21. GlobalKey<FormState> _formKey = GlobalKey();
  22. bool _autoValidate = false;
  23. Plant _plant = Plant();
  24. var pref = LocalPref();
  25. FlutterToast flutterToast;
  26. TextEditingController _descriptionController = TextEditingController();
  27. TextEditingController _quantityController = TextEditingController();
  28. TextEditingController _densityController = TextEditingController();
  29. String executeTimeView;
  30. DateTime executeTime = DateTime.now();
  31. @override
  32. void initState() {
  33. super.initState();
  34. flutterToast = FlutterToast(context);
  35. //UPDATE
  36. if (_plant != null) {
  37. try {
  38. executeTime =
  39. DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").parse(_plant.executeDate);
  40. } catch (_) {}
  41. } else {
  42. //ADD NEW
  43. var parsedExecuteDate =
  44. DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(executeTime);
  45. _plant.executeDate = "$parsedExecuteDate";
  46. }
  47. executeTimeView = DateFormat("dd/MM/yyyy HH:mm").format(executeTime);
  48. }
  49. Widget _btnExecuteTimePicker() {
  50. return FlatButton(
  51. padding: EdgeInsets.only(top: 0.0, right: 0.0, bottom: 0.0, left: 0.0),
  52. onPressed: () {
  53. DatePicker.showDateTimePicker(context,
  54. showTitleActions: true, onChanged: (date) {}, onConfirm: (date) {
  55. setState(() {
  56. var parsedDate =
  57. DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(date);
  58. _plant.executeDate = "$parsedDate";
  59. executeTimeView = DateFormat("dd/MM/yyyy HH:mm").format(date);
  60. });
  61. }, currentTime: executeTime, locale: LocaleType.vi);
  62. },
  63. child: Container(
  64. padding:
  65. EdgeInsets.only(top: 0.0, right: 0.0, bottom: 10.5, left: 0.0),
  66. decoration: BoxDecoration(
  67. border: kBorderTextField,
  68. ),
  69. child: Row(
  70. children: [
  71. Expanded(
  72. child: Text(
  73. //TODO: check condition
  74. executeTimeView == null ? "$executeTime" : executeTimeView,
  75. style: TextStyle(fontSize: 14.0, color: Colors.black87),
  76. )),
  77. Icon(
  78. Icons.date_range,
  79. color: Colors.blue,
  80. ),
  81. ],
  82. )));
  83. }
  84. Widget _btnSelectSeed() {
  85. return FlatButton(
  86. padding: EdgeInsets.only(top: 0.0, right: 0.0, bottom: 0.0, left: 0.0),
  87. onPressed: () {},
  88. child: Container(
  89. padding:
  90. EdgeInsets.only(top: 0.0, right: 0.0, bottom: 10.5, left: 0.0),
  91. decoration: BoxDecoration(
  92. border: kBorderTextField,
  93. ),
  94. child: Row(
  95. children: [
  96. Expanded(
  97. child: Text(
  98. "Tên giống",
  99. style: TextStyle(fontSize: 14.0, color: Colors.black87),
  100. )),
  101. Icon(
  102. Icons.arrow_drop_down,
  103. color: Colors.grey,
  104. ),
  105. ],
  106. )));
  107. }
  108. Widget _quantityField() {
  109. return TextFormField(
  110. keyboardType: TextInputType.numberWithOptions(decimal: true),
  111. inputFormatters: [
  112. ThousandsFormatter(
  113. formatter: NumberFormat("#,###.##", "es"), allowFraction: true)
  114. ],
  115. decoration: InputDecoration(labelText: "Số lượng cây trồng"),
  116. controller: _quantityController,
  117. validator: (String value) {
  118. return Validators.validNumber(value, "Số lượng cây trồng");
  119. },
  120. onSaved: (newValue) {
  121. _plant.quantity = newValue.parseDoubleThousand();
  122. },
  123. );
  124. }
  125. Widget _densityField() {
  126. return TextFormField(
  127. keyboardType: TextInputType.text,
  128. decoration: InputDecoration(labelText: "Mật độ"),
  129. controller: _densityController,
  130. onSaved: (newValue) {
  131. _plant.density = newValue;
  132. },
  133. );
  134. }
  135. Widget _desciptionField() {
  136. return TextFormField(
  137. keyboardType: TextInputType.text,
  138. decoration: InputDecoration(labelText: "Ghi chú"),
  139. controller: _descriptionController,
  140. onSaved: (newValue) {
  141. _plant.description = newValue;
  142. },
  143. );
  144. }
  145. _actionAppBar() {
  146. IconButton iconButton;
  147. if (1 == 1) {
  148. iconButton = IconButton(
  149. icon: Icon(
  150. Icons.done,
  151. color: Colors.black,
  152. ),
  153. onPressed: () {},
  154. );
  155. return <Widget>[iconButton];
  156. }
  157. return <Widget>[Container()];
  158. }
  159. @override
  160. Widget build(BuildContext context) => KeyboardDismisser(
  161. gestures: [
  162. GestureType.onTap,
  163. GestureType.onPanUpdateDownDirection,
  164. ],
  165. child: Scaffold(
  166. key: _scaffoldKey,
  167. appBar: AppBar(
  168. centerTitle: true,
  169. title: Text(plot_action_plant),
  170. actions: _actionAppBar()),
  171. body: KeyboardDismisser(
  172. child: Form(
  173. key: _formKey,
  174. autovalidate: _autoValidate,
  175. child: SingleChildScrollView(
  176. padding: EdgeInsets.all(8.0),
  177. child: Column(
  178. children: <Widget>[
  179. Container(
  180. width: double.infinity,
  181. child: Text(
  182. "Ngày thực hiện",
  183. style: TextStyle(
  184. color: Colors.black54, fontSize: 13.0),
  185. ),
  186. ),
  187. _btnExecuteTimePicker(),
  188. SizedBox(
  189. height: 8.0,
  190. ),
  191. Container(
  192. width: double.infinity,
  193. child: Text(
  194. "Tên giống",
  195. style: TextStyle(
  196. color: Colors.black54, fontSize: 13.0),
  197. ),
  198. ),
  199. _btnSelectSeed(),
  200. SizedBox(
  201. height: 8.0,
  202. ),
  203. _quantityField(),
  204. SizedBox(
  205. height: 8.0,
  206. ),
  207. _densityField(),
  208. SizedBox(
  209. height: 8.0,
  210. ),
  211. _desciptionField()
  212. ],
  213. ),
  214. )))));
  215. @override
  216. void dispose() {
  217. _quantityController.dispose();
  218. _densityController.dispose();
  219. _descriptionController.dispose();
  220. super.dispose();
  221. }
  222. }