|
|
|
@@ -1,18 +1,36 @@ |
|
|
|
import 'dart:convert'; |
|
|
|
|
|
|
|
import 'package:farm_tpf/custom_model/Plant.dart'; |
|
|
|
import 'package:farm_tpf/custom_model/SuppliesUsing.dart'; |
|
|
|
import 'package:farm_tpf/data/api/app_exception.dart'; |
|
|
|
import 'package:farm_tpf/data/repository/repository.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/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 'package:pattern_formatter/pattern_formatter.dart'; |
|
|
|
import 'package:farm_tpf/utils/formatter.dart'; |
|
|
|
|
|
|
|
import '../util_action.dart'; |
|
|
|
|
|
|
|
class EditActionPlantScreen extends StatefulWidget { |
|
|
|
final int cropId; |
|
|
|
final bool isEdit; |
|
|
|
final int activityId; |
|
|
|
EditActionPlantScreen( |
|
|
|
{@required this.cropId, this.isEdit = false, this.activityId}); |
|
|
|
@override |
|
|
|
_EditActionPlantScreenState createState() => _EditActionPlantScreenState(); |
|
|
|
} |
|
|
|
@@ -24,30 +42,95 @@ class _EditActionPlantScreenState extends State<EditActionPlantScreen> { |
|
|
|
bool _autoValidate = false; |
|
|
|
Plant _plant = Plant(); |
|
|
|
var pref = LocalPref(); |
|
|
|
FlutterToast flutterToast; |
|
|
|
TextEditingController _descriptionController = TextEditingController(); |
|
|
|
TextEditingController _quantityController = TextEditingController(); |
|
|
|
TextEditingController _densityController = TextEditingController(); |
|
|
|
final _descriptionController = TextEditingController(); |
|
|
|
final _quantityController = TextEditingController(); |
|
|
|
final _densityController = TextEditingController(); |
|
|
|
final _executeByController = TextEditingController(); |
|
|
|
List<SuppliesUsing> suppliesUsing; |
|
|
|
|
|
|
|
String executeTimeView; |
|
|
|
DateTime executeTime = DateTime.now(); |
|
|
|
List<String> filePaths = List<String>(); |
|
|
|
var changeFileController = Get.put(ChangeFileController()); |
|
|
|
|
|
|
|
Future<Null> getSharedPrefs() async { |
|
|
|
var currentFullName = await pref.getString(DATA_CONST.CURRENT_FULL_NAME); |
|
|
|
_executeByController.text = currentFullName ?? ""; |
|
|
|
} |
|
|
|
|
|
|
|
@override |
|
|
|
void initState() { |
|
|
|
super.initState(); |
|
|
|
flutterToast = FlutterToast(context); |
|
|
|
//UPDATE |
|
|
|
if (_plant != null) { |
|
|
|
try { |
|
|
|
executeTime = |
|
|
|
DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").parse(_plant.executeDate); |
|
|
|
} catch (_) {} |
|
|
|
} else { |
|
|
|
getSharedPrefs(); |
|
|
|
changeFileController.initValue(); |
|
|
|
_plant.suppliesUsing = new List<SuppliesUsing>(); |
|
|
|
var parsedExecuteDate = |
|
|
|
DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(executeTime); |
|
|
|
_plant.executeDate = "$parsedExecuteDate"; |
|
|
|
executeTimeView = DateFormat("dd/MM/yyyy HH:mm").format(executeTime); |
|
|
|
_plant.cropId = widget.cropId; |
|
|
|
} |
|
|
|
|
|
|
|
_validateInputs() async { |
|
|
|
if (_formKey.currentState.validate()) { |
|
|
|
_formKey.currentState.save(); |
|
|
|
LoadingDialog.showLoadingDialog(context); |
|
|
|
filePaths = Get.find<ChangeFileController>().files; |
|
|
|
var activityPlant = jsonEncode(_plant.toJson()).toString(); |
|
|
|
//ADD NEW |
|
|
|
var parsedExecuteDate = |
|
|
|
DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(executeTime); |
|
|
|
_plant.executeDate = "$parsedExecuteDate"; |
|
|
|
if (_plant.activityId == null) { |
|
|
|
_repository.createAction((value) { |
|
|
|
LoadingDialog.hideLoadingDialog(context); |
|
|
|
Get.back(result: value); |
|
|
|
Get.snackbar(label_add_success, "Hoạt động trồng", |
|
|
|
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.apiAddPlant, |
|
|
|
paramActivity: ConstCommon.paramsActionPlant, |
|
|
|
activityAction: activityPlant, |
|
|
|
filePaths: filePaths); |
|
|
|
} else { |
|
|
|
//UPDATE |
|
|
|
_repository.updateAction((value) { |
|
|
|
LoadingDialog.hideLoadingDialog(context); |
|
|
|
Get.back(result: value); |
|
|
|
Get.snackbar(label_update_success, "Hoạt động trồng", |
|
|
|
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.apiUpdatePlant, |
|
|
|
paramActivity: ConstCommon.paramsActionPlant, |
|
|
|
activityAction: activityPlant, |
|
|
|
filePaths: filePaths); |
|
|
|
} |
|
|
|
} else { |
|
|
|
_autoValidate = true; |
|
|
|
} |
|
|
|
executeTimeView = DateFormat("dd/MM/yyyy HH:mm").format(executeTime); |
|
|
|
} |
|
|
|
|
|
|
|
Widget _btnExecuteTimePicker() { |
|
|
|
@@ -86,31 +169,6 @@ class _EditActionPlantScreenState extends State<EditActionPlantScreen> { |
|
|
|
))); |
|
|
|
} |
|
|
|
|
|
|
|
Widget _btnSelectSeed() { |
|
|
|
return FlatButton( |
|
|
|
padding: EdgeInsets.only(top: 0.0, right: 0.0, bottom: 0.0, left: 0.0), |
|
|
|
onPressed: () {}, |
|
|
|
child: Container( |
|
|
|
padding: |
|
|
|
EdgeInsets.only(top: 0.0, right: 0.0, bottom: 10.5, left: 0.0), |
|
|
|
decoration: BoxDecoration( |
|
|
|
border: kBorderTextField, |
|
|
|
), |
|
|
|
child: Row( |
|
|
|
children: [ |
|
|
|
Expanded( |
|
|
|
child: Text( |
|
|
|
"Tên giống", |
|
|
|
style: TextStyle(fontSize: 14.0, color: Colors.black87), |
|
|
|
)), |
|
|
|
Icon( |
|
|
|
Icons.arrow_drop_down, |
|
|
|
color: Colors.grey, |
|
|
|
), |
|
|
|
], |
|
|
|
))); |
|
|
|
} |
|
|
|
|
|
|
|
Widget _quantityField() { |
|
|
|
return TextFormField( |
|
|
|
keyboardType: TextInputType.numberWithOptions(decimal: true), |
|
|
|
@@ -151,6 +209,16 @@ class _EditActionPlantScreenState extends State<EditActionPlantScreen> { |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
Widget _executeByField() { |
|
|
|
return TextFormField( |
|
|
|
keyboardType: TextInputType.text, |
|
|
|
decoration: InputDecoration(labelText: "Người thực hiện"), |
|
|
|
enabled: false, |
|
|
|
controller: _executeByController, |
|
|
|
onSaved: (newValue) {}, |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
_actionAppBar() { |
|
|
|
IconButton iconButton; |
|
|
|
if (1 == 1) { |
|
|
|
@@ -159,7 +227,13 @@ class _EditActionPlantScreenState extends State<EditActionPlantScreen> { |
|
|
|
Icons.done, |
|
|
|
color: Colors.black, |
|
|
|
), |
|
|
|
onPressed: () {}, |
|
|
|
onPressed: () { |
|
|
|
FocusScopeNode currentFocus = FocusScope.of(context); |
|
|
|
if (!currentFocus.hasPrimaryFocus) { |
|
|
|
currentFocus.unfocus(); |
|
|
|
} |
|
|
|
_validateInputs(); |
|
|
|
}, |
|
|
|
); |
|
|
|
return <Widget>[iconButton]; |
|
|
|
} |
|
|
|
@@ -179,54 +253,123 @@ class _EditActionPlantScreenState extends State<EditActionPlantScreen> { |
|
|
|
title: Text(plot_action_plant), |
|
|
|
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), |
|
|
|
), |
|
|
|
), |
|
|
|
_btnExecuteTimePicker(), |
|
|
|
SizedBox( |
|
|
|
height: 8.0, |
|
|
|
), |
|
|
|
Container( |
|
|
|
width: double.infinity, |
|
|
|
child: Text( |
|
|
|
"Tên giống", |
|
|
|
style: TextStyle( |
|
|
|
color: Colors.black54, fontSize: 13.0), |
|
|
|
), |
|
|
|
), |
|
|
|
_btnSelectSeed(), |
|
|
|
SizedBox( |
|
|
|
height: 8.0, |
|
|
|
), |
|
|
|
_quantityField(), |
|
|
|
SizedBox( |
|
|
|
height: 8.0, |
|
|
|
), |
|
|
|
_densityField(), |
|
|
|
SizedBox( |
|
|
|
height: 8.0, |
|
|
|
child: MultiBlocProvider( |
|
|
|
providers: [ |
|
|
|
BlocProvider<ActionDetailBloc>( |
|
|
|
create: (context) => |
|
|
|
ActionDetailBloc(repository: Repository()) |
|
|
|
..add(FetchData( |
|
|
|
isNeedFetchData: widget.isEdit, |
|
|
|
apiActivity: ConstCommon.apiDetailPlant, |
|
|
|
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); |
|
|
|
_plant = Plant.fromJson(state.item); |
|
|
|
_plant.activityId = widget.activityId; |
|
|
|
_quantityController.text = _plant.quantity |
|
|
|
.formatNumtoStringDecimal(); |
|
|
|
_densityController.text = _plant.density; |
|
|
|
_descriptionController.text = |
|
|
|
_plant.description; |
|
|
|
_executeByController.text = _plant.executeBy; |
|
|
|
try { |
|
|
|
executeTime = |
|
|
|
DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") |
|
|
|
.parse(_plant.executeDate); |
|
|
|
} catch (_) {} |
|
|
|
executeTimeView = |
|
|
|
DateFormat("dd/MM/yyyy HH:mm") |
|
|
|
.format(executeTime); |
|
|
|
//Show media |
|
|
|
if (_plant.media != null) { |
|
|
|
await UtilAction.cacheFiles(_plant.media) |
|
|
|
.then((value) { |
|
|
|
BlocProvider.of<MediaHelperBloc>(context) |
|
|
|
.add(ChangeListMedia(items: value)); |
|
|
|
}).whenComplete(() { |
|
|
|
print("completed"); |
|
|
|
}); |
|
|
|
} |
|
|
|
} else if (state is ActionDetailInitial) { |
|
|
|
print("init"); |
|
|
|
} else if (state is ActionDetailLoading) { |
|
|
|
print("loading"); |
|
|
|
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, |
|
|
|
), |
|
|
|
_quantityField(), |
|
|
|
SizedBox( |
|
|
|
height: 8.0, |
|
|
|
), |
|
|
|
_densityField(), |
|
|
|
SizedBox( |
|
|
|
height: 8.0, |
|
|
|
), |
|
|
|
_desciptionField(), |
|
|
|
SizedBox( |
|
|
|
height: 8.0, |
|
|
|
), |
|
|
|
_executeByField(), |
|
|
|
SizedBox( |
|
|
|
height: 8.0, |
|
|
|
), |
|
|
|
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()); |
|
|
|
} |
|
|
|
}), |
|
|
|
], |
|
|
|
); |
|
|
|
}, |
|
|
|
), |
|
|
|
_desciptionField() |
|
|
|
], |
|
|
|
), |
|
|
|
))))); |
|
|
|
)))))); |
|
|
|
@override |
|
|
|
void dispose() { |
|
|
|
_quantityController.dispose(); |
|
|
|
_densityController.dispose(); |
|
|
|
_descriptionController.dispose(); |
|
|
|
_executeByController.dispose(); |
|
|
|
super.dispose(); |
|
|
|
} |
|
|
|
} |