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.

229 lines
7.6KB

  1. import 'package:farm_tpf/data/repository/repository.dart';
  2. import 'package:farm_tpf/models/index.dart';
  3. import 'package:farm_tpf/presentation/screens/actions/other/bloc_get_action_type.dart';
  4. import 'package:farm_tpf/utils/const_string.dart';
  5. import 'package:farm_tpf/utils/const_style.dart';
  6. import 'package:farm_tpf/utils/pref.dart';
  7. import 'package:farm_tpf/utils/validators.dart';
  8. import 'package:flutter/material.dart';
  9. import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
  10. import 'package:fluttertoast/fluttertoast.dart';
  11. import 'package:intl/intl.dart';
  12. import 'package:keyboard_dismisser/keyboard_dismisser.dart';
  13. class EditActionOtherScreen extends StatefulWidget {
  14. @override
  15. _EditActionOtherScreenState createState() => _EditActionOtherScreenState();
  16. }
  17. class _EditActionOtherScreenState extends State<EditActionOtherScreen> {
  18. final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
  19. final _repository = Repository();
  20. GlobalKey<FormState> _formKey = GlobalKey();
  21. bool _autoValidate = false;
  22. OtherAction _otherAction = OtherAction();
  23. List<ActionType> _actionTypes = List<ActionType>();
  24. ActionType _actionType;
  25. var pref = LocalPref();
  26. FlutterToast flutterToast;
  27. TextEditingController _descriptionController = TextEditingController();
  28. TextEditingController _workerNameController = 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 (_otherAction != null) {
  37. try {
  38. executeTime = DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
  39. .parse(_otherAction.executeDate);
  40. } catch (_) {}
  41. } else {
  42. //ADD NEW
  43. var parsedExecuteDate =
  44. DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(executeTime);
  45. _otherAction.executeDate = "$parsedExecuteDate";
  46. }
  47. executeTimeView = DateFormat("dd/MM/yyyy HH:mm").format(executeTime);
  48. //Get action types
  49. getActionTypeBloc.getActionTypes((data) {
  50. _actionTypes = data;
  51. for (var item in _actionTypes) {
  52. if (item.id == _otherAction.activityId) {
  53. _actionType = item;
  54. break;
  55. }
  56. }
  57. }, (err) {});
  58. }
  59. Widget _btnExecuteTimePicker() {
  60. return FlatButton(
  61. padding: EdgeInsets.only(top: 0.0, right: 0.0, bottom: 0.0, left: 0.0),
  62. onPressed: () {
  63. DatePicker.showDateTimePicker(context,
  64. showTitleActions: true, onChanged: (date) {}, onConfirm: (date) {
  65. setState(() {
  66. var parsedDate =
  67. DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(date);
  68. _otherAction.executeDate = "$parsedDate";
  69. executeTimeView = DateFormat("dd/MM/yyyy HH:mm").format(date);
  70. });
  71. }, currentTime: executeTime, locale: LocaleType.vi);
  72. },
  73. child: Container(
  74. padding:
  75. EdgeInsets.only(top: 0.0, right: 0.0, bottom: 10.5, left: 0.0),
  76. decoration: BoxDecoration(
  77. border: kBorderTextField,
  78. ),
  79. child: Row(
  80. children: [
  81. Expanded(
  82. child: Text(
  83. //TODO: check condition
  84. executeTimeView == null ? "$executeTime" : executeTimeView,
  85. style: TextStyle(fontSize: 14.0, color: Colors.black87),
  86. )),
  87. Icon(
  88. Icons.date_range,
  89. color: Colors.blue,
  90. ),
  91. ],
  92. )));
  93. }
  94. List<DropdownMenuItem<ActionType>> _buildDropMenu(List<ActionType> actions) {
  95. return actions
  96. .map((action) => DropdownMenuItem<ActionType>(
  97. child: Text(action.description.toString()),
  98. value: action,
  99. ))
  100. .toList();
  101. }
  102. Widget _dropdownAcionTypes() {
  103. return StreamBuilder(
  104. stream: getActionTypeBloc.actions,
  105. builder: (context, AsyncSnapshot<dynamic> snapshot) {
  106. if (snapshot.hasData) {
  107. return DropdownButtonFormField<ActionType>(
  108. value: _actionType,
  109. hint: Text("Hoạt động"),
  110. onChanged: (ActionType newValue) {
  111. setState(() {
  112. _actionType = newValue;
  113. _otherAction.activityId = newValue.id;
  114. _otherAction.activityTypeName = newValue.name;
  115. });
  116. },
  117. validator: (value) => value == null ? "Hoạt động" : null,
  118. isExpanded: true,
  119. items: _buildDropMenu(_actionTypes));
  120. } else if (snapshot.hasError) {
  121. return Container();
  122. } else {
  123. return Center(
  124. child: CircularProgressIndicator(),
  125. );
  126. }
  127. },
  128. );
  129. }
  130. Widget _desciptionField() {
  131. return TextFormField(
  132. keyboardType: TextInputType.text,
  133. decoration: InputDecoration(labelText: "Ghi chú"),
  134. controller: _descriptionController,
  135. onSaved: (newValue) {
  136. _otherAction.description = newValue;
  137. },
  138. );
  139. }
  140. Widget _workerNameField() {
  141. return TextFormField(
  142. keyboardType: TextInputType.text,
  143. decoration: InputDecoration(labelText: "Người thực hiện"),
  144. controller: _workerNameController,
  145. validator: (String value) {
  146. return Validators.validateNotNullOrEmpty(value, "Người thực hiện");
  147. },
  148. onSaved: (newValue) {
  149. _otherAction.workerName = newValue;
  150. },
  151. );
  152. }
  153. _actionAppBar() {
  154. IconButton iconButton;
  155. if (1 == 1) {
  156. iconButton = IconButton(
  157. icon: Icon(
  158. Icons.done,
  159. color: Colors.black,
  160. ),
  161. onPressed: () {},
  162. );
  163. return <Widget>[iconButton];
  164. }
  165. return <Widget>[Container()];
  166. }
  167. @override
  168. Widget build(BuildContext context) => KeyboardDismisser(
  169. gestures: [
  170. GestureType.onTap,
  171. GestureType.onPanUpdateDownDirection,
  172. ],
  173. child: Scaffold(
  174. key: _scaffoldKey,
  175. appBar: AppBar(
  176. centerTitle: true,
  177. title: Text(plot_action_other),
  178. actions: _actionAppBar()),
  179. body: KeyboardDismisser(
  180. child: Form(
  181. key: _formKey,
  182. autovalidate: _autoValidate,
  183. child: SingleChildScrollView(
  184. padding: EdgeInsets.all(8.0),
  185. child: Column(
  186. children: <Widget>[
  187. Container(
  188. width: double.infinity,
  189. child: Text(
  190. "Ngày thực hiện",
  191. style: TextStyle(
  192. color: Colors.black54, fontSize: 13.0),
  193. ),
  194. ),
  195. _btnExecuteTimePicker(),
  196. SizedBox(
  197. height: 8.0,
  198. ),
  199. _dropdownAcionTypes(),
  200. SizedBox(
  201. height: 8.0,
  202. ),
  203. _desciptionField(),
  204. SizedBox(
  205. height: 8.0,
  206. ),
  207. _workerNameField()
  208. ],
  209. ),
  210. )))));
  211. @override
  212. void dispose() {
  213. _descriptionController.dispose();
  214. _workerNameController.dispose();
  215. super.dispose();
  216. }
  217. }