|
|
|
@@ -1,19 +1,372 @@ |
|
|
|
import 'dart:convert'; |
|
|
|
|
|
|
|
import 'package:farm_tpf/custom_model/UseWater.dart'; |
|
|
|
import 'package:farm_tpf/custom_model/WaterType.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: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 EditActionUseWaterScreen extends StatefulWidget { |
|
|
|
final int cropId; |
|
|
|
final bool isEdit; |
|
|
|
final int activityId; |
|
|
|
EditActionUseWaterScreen( |
|
|
|
{@required this.cropId, this.isEdit = false, this.activityId}); |
|
|
|
@override |
|
|
|
_EditActionUseWaterScreenState createState() => |
|
|
|
_EditActionUseWaterScreenState(); |
|
|
|
} |
|
|
|
|
|
|
|
class _EditActionUseWaterScreenState extends State<EditActionUseWaterScreen> { |
|
|
|
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>(); |
|
|
|
final _repository = Repository(); |
|
|
|
GlobalKey<FormState> _formKey = GlobalKey(); |
|
|
|
bool _autoValidate = false; |
|
|
|
UseWater _useWater = UseWater(); |
|
|
|
var pref = LocalPref(); |
|
|
|
TextEditingController _waterTypeController = TextEditingController(); |
|
|
|
TextEditingController _amountController = TextEditingController(); |
|
|
|
TextEditingController _descriptionController = TextEditingController(); |
|
|
|
|
|
|
|
String executeTimeView; |
|
|
|
DateTime executeTime = DateTime.now(); |
|
|
|
List<String> filePaths = List<String>(); |
|
|
|
var changeFileController = Get.put(ChangeFileController()); |
|
|
|
|
|
|
|
@override |
|
|
|
Widget build(BuildContext context) { |
|
|
|
return Scaffold( |
|
|
|
appBar: AppBar( |
|
|
|
title: Text(plot_action_use_water), |
|
|
|
void initState() { |
|
|
|
super.initState(); |
|
|
|
changeFileController.initValue(); |
|
|
|
var parsedExecuteDate = |
|
|
|
DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(executeTime); |
|
|
|
_useWater.executeDate = "$parsedExecuteDate"; |
|
|
|
executeTimeView = DateFormat("dd/MM/yyyy HH:mm").format(executeTime); |
|
|
|
_useWater.cropId = widget.cropId; |
|
|
|
} |
|
|
|
|
|
|
|
_validateInputs() async { |
|
|
|
if (_formKey.currentState.validate()) { |
|
|
|
_formKey.currentState.save(); |
|
|
|
LoadingDialog.showLoadingDialog(context); |
|
|
|
filePaths = Get.find<ChangeFileController>().files; |
|
|
|
//Create request general model |
|
|
|
try { |
|
|
|
var activityUseWater = jsonEncode(_useWater.toJson()).toString(); |
|
|
|
//ADD NEW |
|
|
|
if (_useWater.activityId == null) { |
|
|
|
_repository.createAction((value) { |
|
|
|
LoadingDialog.hideLoadingDialog(context); |
|
|
|
Get.back(result: value); |
|
|
|
Get.snackbar(label_add_success, "Hoạt động sử dụng nướ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.apiAddUserWater, |
|
|
|
paramActivity: ConstCommon.paramsActionUseWater, |
|
|
|
activityAction: activityUseWater, |
|
|
|
filePaths: filePaths); |
|
|
|
} else { |
|
|
|
//UPDATE |
|
|
|
_repository.updateAction((value) { |
|
|
|
LoadingDialog.hideLoadingDialog(context); |
|
|
|
Get.back(result: value); |
|
|
|
Get.snackbar(label_update_success, "Hoạt động sử dụng nướ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.apiUpdateUseWater, |
|
|
|
paramActivity: ConstCommon.paramsActionUseWater, |
|
|
|
activityAction: activityUseWater, |
|
|
|
filePaths: filePaths); |
|
|
|
} |
|
|
|
} catch (e) { |
|
|
|
LoadingDialog.hideLoadingDialog(context); |
|
|
|
print(e.toString()); |
|
|
|
} |
|
|
|
} else { |
|
|
|
_autoValidate = true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
Widget _btnExecuteTimePicker() { |
|
|
|
return FlatButton( |
|
|
|
padding: EdgeInsets.only(top: 0.0, right: 0.0, bottom: 0.0, left: 0.0), |
|
|
|
onPressed: () { |
|
|
|
DatePicker.showDateTimePicker(context, |
|
|
|
showTitleActions: true, onChanged: (date) {}, onConfirm: (date) { |
|
|
|
setState(() { |
|
|
|
var parsedDate = |
|
|
|
DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(date); |
|
|
|
_useWater.executeDate = "$parsedDate"; |
|
|
|
executeTimeView = DateFormat("dd/MM/yyyy HH:mm").format(date); |
|
|
|
}); |
|
|
|
}, currentTime: executeTime, locale: LocaleType.vi); |
|
|
|
}, |
|
|
|
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( |
|
|
|
//TODO: check condition |
|
|
|
executeTimeView == null ? "$executeTime" : executeTimeView, |
|
|
|
style: TextStyle(fontSize: 14.0, color: Colors.black87), |
|
|
|
)), |
|
|
|
Icon( |
|
|
|
Icons.date_range, |
|
|
|
color: Colors.blue, |
|
|
|
), |
|
|
|
], |
|
|
|
))); |
|
|
|
} |
|
|
|
|
|
|
|
Widget _amountField() { |
|
|
|
return TextFormField( |
|
|
|
keyboardType: TextInputType.numberWithOptions(decimal: true), |
|
|
|
inputFormatters: [ |
|
|
|
ThousandsFormatter( |
|
|
|
formatter: NumberFormat("#,###.##", "es"), allowFraction: true) |
|
|
|
], |
|
|
|
decoration: InputDecoration(labelText: "Thể tích nước sử dụng"), |
|
|
|
controller: _amountController, |
|
|
|
validator: (String value) { |
|
|
|
return Validators.validNumber(value, "Thể tích nước sử dụng"); |
|
|
|
}, |
|
|
|
onSaved: (newValue) { |
|
|
|
_useWater.amount = newValue.parseDoubleThousand(); |
|
|
|
}, |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
Widget _descriptionField() { |
|
|
|
return TextFormField( |
|
|
|
keyboardType: TextInputType.text, |
|
|
|
decoration: InputDecoration(labelText: "Ghi chú"), |
|
|
|
controller: _descriptionController, |
|
|
|
onSaved: (newValue) { |
|
|
|
_useWater.description = newValue; |
|
|
|
}, |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
Widget _typeWater() { |
|
|
|
var waterTypes = List<WaterType>(); |
|
|
|
waterTypes.add(WaterType() |
|
|
|
..waterTypeName = "name" |
|
|
|
..waterTypeDescription = "description"); |
|
|
|
waterTypes.add(WaterType() |
|
|
|
..waterTypeName = "name1" |
|
|
|
..waterTypeDescription = "description1"); |
|
|
|
waterTypes.add(WaterType() |
|
|
|
..waterTypeName = "name2" |
|
|
|
..waterTypeDescription = "description2"); |
|
|
|
return Container( |
|
|
|
height: 150, |
|
|
|
child: ListView.builder( |
|
|
|
physics: NeverScrollableScrollPhysics(), |
|
|
|
shrinkWrap: true, |
|
|
|
itemBuilder: (context, index) { |
|
|
|
return GestureDetector( |
|
|
|
child: RadioListTile( |
|
|
|
title: Text(waterTypes[index].waterTypeDescription ?? ""), |
|
|
|
value: waterTypes[index], |
|
|
|
groupValue: 1 == 1 ? null : waterTypes[0], |
|
|
|
onChanged: (WaterType value) { |
|
|
|
print("selected value: ${value.waterTypeName}"); |
|
|
|
}), |
|
|
|
onTap: () {}); |
|
|
|
}, |
|
|
|
itemCount: waterTypes.length, |
|
|
|
), |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
_actionAppBar() { |
|
|
|
IconButton iconButton; |
|
|
|
if (1 == 1) { |
|
|
|
iconButton = IconButton( |
|
|
|
icon: Icon( |
|
|
|
Icons.done, |
|
|
|
color: Colors.black, |
|
|
|
), |
|
|
|
onPressed: () { |
|
|
|
FocusScopeNode currentFocus = FocusScope.of(context); |
|
|
|
if (!currentFocus.hasPrimaryFocus) { |
|
|
|
currentFocus.unfocus(); |
|
|
|
} |
|
|
|
_validateInputs(); |
|
|
|
}, |
|
|
|
); |
|
|
|
return <Widget>[iconButton]; |
|
|
|
} |
|
|
|
return <Widget>[Container()]; |
|
|
|
} |
|
|
|
|
|
|
|
@override |
|
|
|
Widget build(BuildContext context) => KeyboardDismisser( |
|
|
|
gestures: [ |
|
|
|
GestureType.onTap, |
|
|
|
GestureType.onPanUpdateDownDirection, |
|
|
|
], |
|
|
|
child: Scaffold( |
|
|
|
key: _scaffoldKey, |
|
|
|
appBar: AppBar( |
|
|
|
centerTitle: true, |
|
|
|
title: Text(plot_action_use_water), |
|
|
|
actions: _actionAppBar()), |
|
|
|
body: KeyboardDismisser( |
|
|
|
child: MultiBlocProvider( |
|
|
|
providers: [ |
|
|
|
BlocProvider<ActionDetailBloc>( |
|
|
|
create: (context) => |
|
|
|
ActionDetailBloc(repository: Repository()) |
|
|
|
..add(FetchData( |
|
|
|
isNeedFetchData: widget.isEdit, |
|
|
|
apiActivity: ConstCommon.apiDetailUseWater, |
|
|
|
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); |
|
|
|
print(state.item); |
|
|
|
_useWater = UseWater.fromJson(state.item); |
|
|
|
_amountController.text = |
|
|
|
_useWater.amount.formatNumtoStringDecimal(); |
|
|
|
_descriptionController.text = |
|
|
|
_useWater.description ?? ""; |
|
|
|
|
|
|
|
try { |
|
|
|
executeTime = |
|
|
|
DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") |
|
|
|
.parse(_useWater.executeDate); |
|
|
|
} catch (_) {} |
|
|
|
executeTimeView = DateFormat("dd/MM/yyyy HH:mm") |
|
|
|
.format(executeTime); |
|
|
|
//Show media |
|
|
|
if (_useWater.media != null) { |
|
|
|
await UtilAction.cacheFiles(_useWater.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, |
|
|
|
), |
|
|
|
_typeWater(), |
|
|
|
SizedBox( |
|
|
|
height: 8.0, |
|
|
|
), |
|
|
|
_amountField(), |
|
|
|
SizedBox( |
|
|
|
height: 8.0, |
|
|
|
), |
|
|
|
_descriptionField(), |
|
|
|
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()); |
|
|
|
} |
|
|
|
}), |
|
|
|
], |
|
|
|
); |
|
|
|
}, |
|
|
|
), |
|
|
|
)), |
|
|
|
)))); |
|
|
|
@override |
|
|
|
void dispose() { |
|
|
|
_amountController.dispose(); |
|
|
|
_descriptionController.dispose(); |
|
|
|
super.dispose(); |
|
|
|
} |
|
|
|
} |