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.

206 lines
8.4KB

  1. import 'package:farm_tpf/data/repository/repository.dart';
  2. import 'package:farm_tpf/presentation/custom_widgets/app_bar_widget.dart';
  3. import 'package:farm_tpf/presentation/custom_widgets/bloc/media_helper_bloc.dart';
  4. import 'package:farm_tpf/presentation/custom_widgets/button_widget.dart';
  5. import 'package:farm_tpf/presentation/custom_widgets/loading_list_page.dart';
  6. import 'package:farm_tpf/presentation/custom_widgets/widget_field_time_picker.dart';
  7. import 'package:farm_tpf/presentation/custom_widgets/widget_loading.dart';
  8. import 'package:farm_tpf/presentation/custom_widgets/widget_media_picker.dart';
  9. import 'package:farm_tpf/presentation/screens/actions/cubit/action_ui_cubit.dart';
  10. import 'package:farm_tpf/utils/pref.dart';
  11. import 'package:farm_tpf/utils/validators.dart';
  12. import 'package:flutter/material.dart';
  13. import 'package:flutter_bloc/flutter_bloc.dart';
  14. import 'package:get/get.dart';
  15. import 'package:keyboard_dismisser/keyboard_dismisser.dart';
  16. import 'state_management_helper/change_file_controller.dart';
  17. class ActionScreen extends StatefulWidget {
  18. final int idAction;
  19. final String title;
  20. ActionScreen({@required this.idAction, @required this.title});
  21. @override
  22. _ActionScreenState createState() => _ActionScreenState();
  23. }
  24. class _ActionScreenState extends State<ActionScreen> {
  25. final _scaffoldKey = GlobalKey<ScaffoldState>();
  26. var _formKey = GlobalKey<FormState>();
  27. var pref = LocalPref();
  28. final _executeByController = TextEditingController();
  29. DateTime executeTime = DateTime.now();
  30. List<String> filePaths = List<String>();
  31. var changeFileController = Get.put(ChangeFileController());
  32. Future<Null> getSharedPrefs() async {
  33. var currentFullName = await pref.getString(DATA_CONST.CURRENT_FULL_NAME);
  34. _executeByController.text = currentFullName ?? "";
  35. }
  36. @override
  37. void initState() {
  38. super.initState();
  39. getSharedPrefs();
  40. }
  41. _validateInputs() async {
  42. if (_formKey.currentState.validate()) {
  43. _formKey.currentState.save();
  44. LoadingDialog.showLoadingDialog(context);
  45. filePaths = Get.find<ChangeFileController>().newFiles;
  46. //Create request general model
  47. try {
  48. //ADD NEW
  49. //Update
  50. } catch (e) {
  51. LoadingDialog.hideLoadingDialog(context);
  52. print(e.toString());
  53. }
  54. } else {
  55. //
  56. }
  57. }
  58. Widget _btnExecuteTimePicker() {
  59. return WidgetFieldDateTimePicker(
  60. initDateTime: executeTime,
  61. onUpdateDateTime: (selectedDate) {
  62. //
  63. });
  64. }
  65. Widget _executeByField() {
  66. return TextFormField(
  67. keyboardType: TextInputType.text,
  68. decoration: InputDecoration(labelText: "Người thực hiện"),
  69. enabled: false,
  70. controller: _executeByController,
  71. onSaved: (newValue) {},
  72. );
  73. }
  74. @override
  75. Widget build(BuildContext context) => KeyboardDismisser(
  76. gestures: [
  77. GestureType.onTap,
  78. GestureType.onPanUpdateDownDirection,
  79. ],
  80. child: Scaffold(
  81. backgroundColor: Colors.white,
  82. key: _scaffoldKey,
  83. appBar: AppBarWidget(
  84. isBack: true,
  85. action: InkWell(
  86. child: Text(
  87. 'Lưu',
  88. style: TextStyle(
  89. color: Colors.red, fontWeight: FontWeight.normal),
  90. ),
  91. onTap: () {},
  92. ),
  93. ),
  94. body: KeyboardDismisser(
  95. child: MultiBlocProvider(
  96. providers: [
  97. BlocProvider<ActionUiCubit>(
  98. create: (context) =>
  99. ActionUiCubit(repository: Repository())
  100. ..getActionUIForm(widget.idAction)),
  101. BlocProvider<MediaHelperBloc>(
  102. create: (context) =>
  103. MediaHelperBloc()..add(ChangeListMedia(items: [])),
  104. )
  105. ],
  106. child: Form(
  107. key: _formKey,
  108. child: SafeArea(
  109. child: SingleChildScrollView(
  110. child: BlocBuilder<ActionUiCubit, ActionUiState>(
  111. builder: (context, state) {
  112. if (state is ActionUiLoading) {
  113. print('loading...');
  114. return Center(child: CircularProgressIndicator());
  115. } else if (state is ActionUiSuccess) {
  116. print('success');
  117. return Column(
  118. children: [
  119. Padding(
  120. padding: const EdgeInsets.all(8.0),
  121. child: Column(
  122. children: <Widget>[
  123. Container(
  124. width: double.infinity,
  125. child: Text(
  126. "Ngày thực hiện *",
  127. style: TextStyle(
  128. color: Colors.black54,
  129. fontSize: 13.0),
  130. ),
  131. ),
  132. _btnExecuteTimePicker(),
  133. SizedBox(
  134. height: 8.0,
  135. ),
  136. _executeByField(),
  137. SizedBox(
  138. height: 8.0,
  139. ),
  140. ],
  141. ),
  142. ),
  143. Container(
  144. width: double.infinity,
  145. height: 16,
  146. color: Colors.grey[200],
  147. ),
  148. BlocBuilder<MediaHelperBloc,
  149. MediaHelperState>(
  150. builder: (context, state) {
  151. if (state is MediaHelperSuccess) {
  152. return WidgetMediaPicker(
  153. currentItems: state.items,
  154. onChangeFiles: (newPathFiles,
  155. deletePathFiles) async {
  156. Get.find<ChangeFileController>()
  157. .change(newPathFiles,
  158. deletePathFiles);
  159. });
  160. } else {
  161. return Center(
  162. child: CircularProgressIndicator());
  163. }
  164. }),
  165. Padding(
  166. padding: const EdgeInsets.all(8.0),
  167. child: ButtonWidget(
  168. title: 'CẬP NHẬT',
  169. onPressed: () {
  170. FocusScopeNode currentFocus =
  171. FocusScope.of(context);
  172. if (!currentFocus.hasPrimaryFocus) {
  173. currentFocus.unfocus();
  174. }
  175. _validateInputs();
  176. }),
  177. ),
  178. ],
  179. );
  180. } else if (state is ActionUiFailure) {
  181. print('error');
  182. return Text(state.errorString);
  183. // LoadingDialog.hideLoadingDialog(context);
  184. }
  185. },
  186. ),
  187. ),
  188. )),
  189. ))));
  190. @override
  191. void dispose() {
  192. _executeByController.dispose();
  193. super.dispose();
  194. }
  195. }