|
|
|
@@ -1,17 +1,36 @@ |
|
|
|
import 'dart:convert'; |
|
|
|
|
|
|
|
import 'package:farm_tpf/custom_model/Other.dart'; |
|
|
|
import 'package:farm_tpf/data/api/app_exception.dart'; |
|
|
|
import 'package:farm_tpf/data/repository/repository.dart'; |
|
|
|
import 'package:farm_tpf/models/index.dart'; |
|
|
|
import 'package:farm_tpf/presentation/custom_widgets/bloc/media_helper_bloc.dart'; |
|
|
|
import 'package:farm_tpf/presentation/custom_widgets/widget_loading.dart'; |
|
|
|
import 'package:farm_tpf/presentation/custom_widgets/widget_media_picker.dart'; |
|
|
|
import 'package:farm_tpf/presentation/screens/actions/bloc/action_detail_bloc.dart'; |
|
|
|
import 'package:farm_tpf/presentation/screens/actions/other/bloc_get_action_type.dart'; |
|
|
|
import 'package:farm_tpf/presentation/screens/actions/state_management_helper/change_file_controller.dart'; |
|
|
|
import 'package:farm_tpf/utils/const_common.dart'; |
|
|
|
import 'package:farm_tpf/utils/const_string.dart'; |
|
|
|
import 'package:farm_tpf/utils/const_style.dart'; |
|
|
|
import 'package:farm_tpf/utils/pref.dart'; |
|
|
|
import 'package:farm_tpf/utils/validators.dart'; |
|
|
|
import 'package:flutter/material.dart'; |
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart'; |
|
|
|
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart'; |
|
|
|
import 'package:fluttertoast/fluttertoast.dart'; |
|
|
|
import 'package:get/get.dart'; |
|
|
|
import 'package:intl/intl.dart'; |
|
|
|
import 'package:keyboard_dismisser/keyboard_dismisser.dart'; |
|
|
|
|
|
|
|
import '../util_action.dart'; |
|
|
|
|
|
|
|
class EditActionOtherScreen extends StatefulWidget { |
|
|
|
final int cropId; |
|
|
|
final bool isEdit; |
|
|
|
final int activityId; |
|
|
|
EditActionOtherScreen( |
|
|
|
{@required this.cropId, this.isEdit = false, this.activityId}); |
|
|
|
@override |
|
|
|
_EditActionOtherScreenState createState() => _EditActionOtherScreenState(); |
|
|
|
} |
|
|
|
@@ -21,44 +40,98 @@ class _EditActionOtherScreenState extends State<EditActionOtherScreen> { |
|
|
|
final _repository = Repository(); |
|
|
|
GlobalKey<FormState> _formKey = GlobalKey(); |
|
|
|
bool _autoValidate = false; |
|
|
|
OtherAction _otherAction = OtherAction(); |
|
|
|
Other _other = Other(); |
|
|
|
final _descriptionController = TextEditingController(); |
|
|
|
|
|
|
|
List<ActionType> _actionTypes = List<ActionType>(); |
|
|
|
ActionType _actionType; |
|
|
|
var pref = LocalPref(); |
|
|
|
FlutterToast flutterToast; |
|
|
|
TextEditingController _descriptionController = TextEditingController(); |
|
|
|
TextEditingController _workerNameController = TextEditingController(); |
|
|
|
String executeTimeView; |
|
|
|
DateTime executeTime = DateTime.now(); |
|
|
|
List<String> filePaths = List<String>(); |
|
|
|
var changeFileController = Get.put(ChangeFileController()); |
|
|
|
|
|
|
|
@override |
|
|
|
void initState() { |
|
|
|
super.initState(); |
|
|
|
flutterToast = FlutterToast(context); |
|
|
|
//UPDATE |
|
|
|
if (_otherAction != null) { |
|
|
|
try { |
|
|
|
executeTime = DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") |
|
|
|
.parse(_otherAction.executeDate); |
|
|
|
} catch (_) {} |
|
|
|
} else { |
|
|
|
//ADD NEW |
|
|
|
var parsedExecuteDate = |
|
|
|
DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(executeTime); |
|
|
|
_otherAction.executeDate = "$parsedExecuteDate"; |
|
|
|
} |
|
|
|
changeFileController.initValue(); |
|
|
|
var parsedExecuteDate = |
|
|
|
DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(executeTime); |
|
|
|
_other.executeDate = "$parsedExecuteDate"; |
|
|
|
executeTimeView = DateFormat("dd/MM/yyyy HH:mm").format(executeTime); |
|
|
|
_other.cropId = widget.cropId; |
|
|
|
|
|
|
|
if (!widget.isEdit) { |
|
|
|
getActionTypeBloc.getActionTypes((data) { |
|
|
|
_actionTypes = data; |
|
|
|
}, (err) {}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//Get action types |
|
|
|
getActionTypeBloc.getActionTypes((data) { |
|
|
|
_actionTypes = data; |
|
|
|
for (var item in _actionTypes) { |
|
|
|
if (item.id == _otherAction.activityId) { |
|
|
|
_actionType = item; |
|
|
|
break; |
|
|
|
_validateInputs() async { |
|
|
|
if (_formKey.currentState.validate()) { |
|
|
|
_formKey.currentState.save(); |
|
|
|
LoadingDialog.showLoadingDialog(context); |
|
|
|
filePaths = Get.find<ChangeFileController>().files; |
|
|
|
try { |
|
|
|
var activityOther = jsonEncode(_other.toJson()).toString(); |
|
|
|
//ADD NEW |
|
|
|
if (_other.activityId == null) { |
|
|
|
_repository.createAction((value) { |
|
|
|
LoadingDialog.hideLoadingDialog(context); |
|
|
|
Get.back(result: value); |
|
|
|
Get.snackbar(label_add_success, "Hoạt động khác", |
|
|
|
snackPosition: SnackPosition.BOTTOM); |
|
|
|
}, (error) { |
|
|
|
LoadingDialog.hideLoadingDialog(context); |
|
|
|
_scaffoldKey.currentState.showSnackBar(SnackBar( |
|
|
|
content: Row( |
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween, |
|
|
|
children: <Widget>[ |
|
|
|
Flexible(child: Text(AppException.handleError(error))), |
|
|
|
Icon(Icons.error), |
|
|
|
], |
|
|
|
), |
|
|
|
backgroundColor: Colors.red, |
|
|
|
duration: Duration(seconds: 3), |
|
|
|
)); |
|
|
|
}, |
|
|
|
apiAddAction: ConstCommon.apiAddOther, |
|
|
|
paramActivity: ConstCommon.paramsActionOther, |
|
|
|
activityAction: activityOther, |
|
|
|
filePaths: filePaths); |
|
|
|
} else { |
|
|
|
//UPDATE |
|
|
|
_repository.updateAction((value) { |
|
|
|
LoadingDialog.hideLoadingDialog(context); |
|
|
|
Get.back(result: value); |
|
|
|
Get.snackbar(label_update_success, "Hoạt động khác", |
|
|
|
snackPosition: SnackPosition.BOTTOM); |
|
|
|
}, (error) { |
|
|
|
LoadingDialog.hideLoadingDialog(context); |
|
|
|
_scaffoldKey.currentState.showSnackBar(SnackBar( |
|
|
|
content: Row( |
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween, |
|
|
|
children: <Widget>[ |
|
|
|
Flexible(child: Text(AppException.handleError(error))), |
|
|
|
Icon(Icons.error), |
|
|
|
], |
|
|
|
), |
|
|
|
backgroundColor: Colors.red, |
|
|
|
duration: Duration(seconds: 3), |
|
|
|
)); |
|
|
|
}, |
|
|
|
apiUpdateAction: ConstCommon.apiUpdateOther, |
|
|
|
paramActivity: ConstCommon.paramsActionOther, |
|
|
|
activityAction: activityOther, |
|
|
|
filePaths: filePaths); |
|
|
|
} |
|
|
|
} catch (e) { |
|
|
|
LoadingDialog.hideLoadingDialog(context); |
|
|
|
print(e.toString()); |
|
|
|
} |
|
|
|
}, (err) {}); |
|
|
|
} else { |
|
|
|
_autoValidate = true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
Widget _btnExecuteTimePicker() { |
|
|
|
@@ -70,7 +143,7 @@ class _EditActionOtherScreenState extends State<EditActionOtherScreen> { |
|
|
|
setState(() { |
|
|
|
var parsedDate = |
|
|
|
DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(date); |
|
|
|
_otherAction.executeDate = "$parsedDate"; |
|
|
|
_other.executeDate = "$parsedDate"; |
|
|
|
executeTimeView = DateFormat("dd/MM/yyyy HH:mm").format(date); |
|
|
|
}); |
|
|
|
}, currentTime: executeTime, locale: LocaleType.vi); |
|
|
|
@@ -117,8 +190,7 @@ class _EditActionOtherScreenState extends State<EditActionOtherScreen> { |
|
|
|
onChanged: (ActionType newValue) { |
|
|
|
setState(() { |
|
|
|
_actionType = newValue; |
|
|
|
_otherAction.activityId = newValue.id; |
|
|
|
_otherAction.activityTypeName = newValue.name; |
|
|
|
_other.activityTypeName = newValue.name; |
|
|
|
}); |
|
|
|
}, |
|
|
|
validator: (value) => value == null ? "Hoạt động" : null, |
|
|
|
@@ -141,21 +213,7 @@ class _EditActionOtherScreenState extends State<EditActionOtherScreen> { |
|
|
|
decoration: InputDecoration(labelText: "Ghi chú"), |
|
|
|
controller: _descriptionController, |
|
|
|
onSaved: (newValue) { |
|
|
|
_otherAction.description = newValue; |
|
|
|
}, |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
Widget _workerNameField() { |
|
|
|
return TextFormField( |
|
|
|
keyboardType: TextInputType.text, |
|
|
|
decoration: InputDecoration(labelText: "Người thực hiện"), |
|
|
|
controller: _workerNameController, |
|
|
|
validator: (String value) { |
|
|
|
return Validators.validateNotNullOrEmpty(value, "Người thực hiện"); |
|
|
|
}, |
|
|
|
onSaved: (newValue) { |
|
|
|
_otherAction.workerName = newValue; |
|
|
|
_other.description = newValue; |
|
|
|
}, |
|
|
|
); |
|
|
|
} |
|
|
|
@@ -168,7 +226,13 @@ class _EditActionOtherScreenState extends State<EditActionOtherScreen> { |
|
|
|
Icons.done, |
|
|
|
color: Colors.black, |
|
|
|
), |
|
|
|
onPressed: () {}, |
|
|
|
onPressed: () { |
|
|
|
FocusScopeNode currentFocus = FocusScope.of(context); |
|
|
|
if (!currentFocus.hasPrimaryFocus) { |
|
|
|
currentFocus.unfocus(); |
|
|
|
} |
|
|
|
_validateInputs(); |
|
|
|
}, |
|
|
|
); |
|
|
|
return <Widget>[iconButton]; |
|
|
|
} |
|
|
|
@@ -188,41 +252,115 @@ class _EditActionOtherScreenState extends State<EditActionOtherScreen> { |
|
|
|
title: Text(plot_action_other), |
|
|
|
actions: _actionAppBar()), |
|
|
|
body: KeyboardDismisser( |
|
|
|
child: Form( |
|
|
|
key: _formKey, |
|
|
|
autovalidate: _autoValidate, |
|
|
|
child: SingleChildScrollView( |
|
|
|
padding: EdgeInsets.all(8.0), |
|
|
|
child: Column( |
|
|
|
children: <Widget>[ |
|
|
|
Container( |
|
|
|
width: double.infinity, |
|
|
|
child: Text( |
|
|
|
"Ngày thực hiện", |
|
|
|
style: TextStyle( |
|
|
|
color: Colors.black54, fontSize: 13.0), |
|
|
|
child: MultiBlocProvider( |
|
|
|
providers: [ |
|
|
|
BlocProvider<ActionDetailBloc>( |
|
|
|
create: (context) => |
|
|
|
ActionDetailBloc(repository: Repository()) |
|
|
|
..add(FetchData( |
|
|
|
isNeedFetchData: widget.isEdit, |
|
|
|
apiActivity: ConstCommon.apiDetailOther, |
|
|
|
activityId: widget.activityId))), |
|
|
|
BlocProvider<MediaHelperBloc>( |
|
|
|
create: (context) => |
|
|
|
MediaHelperBloc()..add(ChangeListMedia(items: [])), |
|
|
|
) |
|
|
|
], |
|
|
|
child: Form( |
|
|
|
key: _formKey, |
|
|
|
autovalidate: _autoValidate, |
|
|
|
child: SingleChildScrollView( |
|
|
|
padding: EdgeInsets.all(8.0), |
|
|
|
child: BlocConsumer<ActionDetailBloc, ActionDetailState>( |
|
|
|
listener: (context, state) async { |
|
|
|
if (state is ActionDetailFailure) { |
|
|
|
LoadingDialog.hideLoadingDialog(context); |
|
|
|
} else if (state is ActionDetailSuccess) { |
|
|
|
LoadingDialog.hideLoadingDialog(context); |
|
|
|
_other = Other.fromJson(state.item); |
|
|
|
_other.activityId = widget.activityId; |
|
|
|
_descriptionController.text = |
|
|
|
_other.description ?? ""; |
|
|
|
|
|
|
|
//select harvest |
|
|
|
getActionTypeBloc.getActionTypes((data) { |
|
|
|
_actionTypes = data; |
|
|
|
for (var item in _actionTypes) { |
|
|
|
if (item.name == _other.activityTypeName) { |
|
|
|
_actionType = item; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
}, (err) {}); |
|
|
|
|
|
|
|
try { |
|
|
|
executeTime = |
|
|
|
DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") |
|
|
|
.parse(_other.executeDate); |
|
|
|
executeTimeView = DateFormat("dd/MM/yyyy HH:mm") |
|
|
|
.format(executeTime); |
|
|
|
} catch (_) {} |
|
|
|
//Show media |
|
|
|
if (_other.media != null) { |
|
|
|
await UtilAction.cacheFiles(_other.media) |
|
|
|
.then((value) { |
|
|
|
BlocProvider.of<MediaHelperBloc>(context) |
|
|
|
.add(ChangeListMedia(items: value)); |
|
|
|
}).whenComplete(() { |
|
|
|
print("completed"); |
|
|
|
}); |
|
|
|
} |
|
|
|
} else if (state is ActionDetailInitial) { |
|
|
|
} else if (state is ActionDetailLoading) { |
|
|
|
LoadingDialog.showLoadingDialog(context); |
|
|
|
} |
|
|
|
}, |
|
|
|
builder: (context, state) { |
|
|
|
return Column( |
|
|
|
children: <Widget>[ |
|
|
|
Container( |
|
|
|
width: double.infinity, |
|
|
|
child: Text( |
|
|
|
"Ngày thực hiện", |
|
|
|
style: TextStyle( |
|
|
|
color: Colors.black54, fontSize: 13.0), |
|
|
|
), |
|
|
|
), |
|
|
|
_btnExecuteTimePicker(), |
|
|
|
SizedBox( |
|
|
|
height: 8.0, |
|
|
|
), |
|
|
|
_dropdownAcionTypes(), |
|
|
|
SizedBox( |
|
|
|
height: 8.0, |
|
|
|
), |
|
|
|
_desciptionField(), |
|
|
|
SizedBox( |
|
|
|
height: 8.0, |
|
|
|
), |
|
|
|
), |
|
|
|
_btnExecuteTimePicker(), |
|
|
|
SizedBox( |
|
|
|
height: 8.0, |
|
|
|
), |
|
|
|
_dropdownAcionTypes(), |
|
|
|
SizedBox( |
|
|
|
height: 8.0, |
|
|
|
), |
|
|
|
_desciptionField(), |
|
|
|
SizedBox( |
|
|
|
height: 8.0, |
|
|
|
), |
|
|
|
_workerNameField() |
|
|
|
], |
|
|
|
), |
|
|
|
))))); |
|
|
|
BlocBuilder<MediaHelperBloc, MediaHelperState>( |
|
|
|
builder: (context, state) { |
|
|
|
if (state is MediaHelperSuccess) { |
|
|
|
return WidgetMediaPicker( |
|
|
|
currentItems: state.items, |
|
|
|
onChangeFiles: (filePaths) async { |
|
|
|
Get.find<ChangeFileController>() |
|
|
|
.addAllFile(filePaths); |
|
|
|
}); |
|
|
|
} else { |
|
|
|
return Center( |
|
|
|
child: CircularProgressIndicator()); |
|
|
|
} |
|
|
|
}), |
|
|
|
], |
|
|
|
); |
|
|
|
}, |
|
|
|
), |
|
|
|
)), |
|
|
|
)))); |
|
|
|
@override |
|
|
|
void dispose() { |
|
|
|
_descriptionController.dispose(); |
|
|
|
_workerNameController.dispose(); |
|
|
|
super.dispose(); |
|
|
|
} |
|
|
|
} |