|
- import 'dart:convert';
-
- import 'package:farm_tpf/custom_model/Harvest.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_field_time_picker.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/custom_widgets/widget_text_form_field.dart';
- import 'package:farm_tpf/presentation/custom_widgets/widget_utils.dart';
- import 'package:farm_tpf/presentation/screens/actions/bloc/action_detail_bloc.dart';
- import 'package:farm_tpf/presentation/screens/actions/harvest_process/sc_edit_action_harvest_process.dart';
- import 'package:farm_tpf/presentation/screens/actions/packing/sc_edit_action_packing.dart';
- import 'package:farm_tpf/presentation/screens/actions/sell/sc_edit_action_sell.dart';
- import 'package:farm_tpf/presentation/screens/actions/state_management_helper/change_file_controller.dart';
- import 'package:farm_tpf/presentation/screens/actions/util_action.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';
-
- class EditActionHarvestScreen extends StatefulWidget {
- final int cropId;
- final bool isEdit;
- final int activityId;
- EditActionHarvestScreen(
- {@required this.cropId, this.isEdit = false, this.activityId});
- @override
- _EditActionHarvestScreenState createState() =>
- _EditActionHarvestScreenState();
- }
-
- class _EditActionHarvestScreenState extends State<EditActionHarvestScreen> {
- final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
- final _repository = Repository();
- GlobalKey<FormState> _formKey = GlobalKey();
- bool _autoValidate = false;
- Harvest _harvest = Harvest();
- TextEditingController _l1Controller = TextEditingController();
- TextEditingController _l2Controller = TextEditingController();
- TextEditingController _l3Controller = TextEditingController();
- TextEditingController _removedQuantityController = TextEditingController();
- TextEditingController _descriptionController = TextEditingController();
- final _executeByController = TextEditingController();
-
- var pref = LocalPref();
- 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();
- getSharedPrefs();
- changeFileController.initValue();
- _harvest.cropId = widget.cropId;
- }
-
- _validateInputs() async {
- if (_formKey.currentState.validate()) {
- _formKey.currentState.save();
- LoadingDialog.showLoadingDialog(context);
- filePaths = Get.find<ChangeFileController>().newFiles;
- try {
- _harvest.mediaDel = Get.find<ChangeFileController>().deleteFiles;
- var activityHarvest = jsonEncode(_harvest.toJson()).toString();
- //ADD NEW
- if (_harvest.activityId == null) {
- _repository.createAction((value) {
- LoadingDialog.hideLoadingDialog(context);
- Get.back(result: value);
- Utils.showSnackBarSuccess(message: label_add_success);
- }, (error) {
- LoadingDialog.hideLoadingDialog(context);
- Utils.showSnackBarError(message: AppException.handleError(error));
- },
- apiAddAction: ConstCommon.apiAddHarvest,
- paramActivity: ConstCommon.paramsActionHarvest,
- activityAction: activityHarvest,
- filePaths: filePaths);
- } else {
- //UPDATE
- _repository.updateAction((value) {
- LoadingDialog.hideLoadingDialog(context);
- Get.back(result: value);
- Utils.showSnackBarSuccess(message: label_update_success);
- }, (error) {
- LoadingDialog.hideLoadingDialog(context);
- Utils.showSnackBarError(message: AppException.handleError(error));
- },
- apiUpdateAction: ConstCommon.apiUpdateHarvest,
- paramActivity: ConstCommon.paramsActionHarvest,
- activityAction: activityHarvest,
- filePaths: filePaths);
- }
- } catch (e) {
- LoadingDialog.hideLoadingDialog(context);
- print(e.toString());
- }
- } else {
- _autoValidate = true;
- }
- }
-
- Widget _btnExecuteTimePicker() {
- return WidgetFieldDateTimePicker(
- initDateTime: executeTime,
- onUpdateDateTime: (selectedDate) {
- _harvest.executeDate =
- selectedDate.convertLocalDateTimeToStringUtcDateTime();
- });
- }
-
- Widget _l1Field() {
- return WidgetTextFormFieldNumber(
- hintValue: "Số lượng/khối lượng loại 1",
- textController: _l1Controller,
- onSaved: (newValue) {
- _harvest.collectedQuantityLv1 = newValue.parseDoubleThousand();
- },
- );
- }
-
- Widget _l2Field() {
- return WidgetTextFormFieldNumber(
- hintValue: "Số lượng/khối lượng loại 2",
- textController: _l2Controller,
- onSaved: (newValue) {
- _harvest.collectedQuantityLv2 = newValue.parseDoubleThousand();
- },
- );
- }
-
- Widget _l3Field() {
- return WidgetTextFormFieldNumber(
- hintValue: "Số lượng/khối lượng loại 3",
- textController: _l3Controller,
- onSaved: (newValue) {
- _harvest.collectedQuantityLv3 = newValue.parseDoubleThousand();
- },
- );
- }
-
- Widget _removedQuantityField() {
- return WidgetTextFormFieldNumber(
- hintValue: "Số lượng/khối lượng loại bỏ",
- textController: _removedQuantityController,
- onSaved: (newValue) {
- _harvest.removedQuantity = newValue.parseDoubleThousand();
- },
- );
- }
-
- Widget _descriptionField() {
- return TextFormField(
- keyboardType: TextInputType.text,
- decoration: InputDecoration(labelText: "Ghi chú"),
- controller: _descriptionController,
- onSaved: (newValue) {
- _harvest.description = newValue;
- },
- );
- }
-
- 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) {
- 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_harvest),
- actions: _actionAppBar()),
- body: KeyboardDismisser(
- child: MultiBlocProvider(
- providers: [
- BlocProvider<ActionDetailBloc>(
- create: (context) =>
- ActionDetailBloc(repository: Repository())
- ..add(FetchData(
- isNeedFetchData: widget.isEdit,
- apiActivity: ConstCommon.apiDetailHarvest,
- 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);
- _harvest = Harvest.fromJson(state.item);
- _harvest.activityId = widget.activityId;
- _l1Controller.text = _harvest.collectedQuantityLv1
- .formatNumtoStringDecimal();
- _l2Controller.text = _harvest.collectedQuantityLv2
- .formatNumtoStringDecimal();
- _l3Controller.text = _harvest.collectedQuantityLv3
- .formatNumtoStringDecimal();
- _removedQuantityController.text = _harvest
- .removedQuantity
- .formatNumtoStringDecimal();
- _descriptionController.text =
- _harvest.description ?? "";
- _executeByController.text = _harvest.executeBy;
-
- Get.find<ChangeDateTimePicker>().change(_harvest
- .executeDate
- .convertStringServerDateTimeToLocalDateTime());
- //Show media
- if (Validators.stringNotNullOrEmpty(
- _harvest.media)) {
- BlocProvider.of<MediaHelperBloc>(context).add(
- ChangeListMedia(
- items: UtilAction.convertFilePathToMedia(
- _harvest.media)));
- }
- } 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,
- ),
- _l1Field(),
- SizedBox(
- height: 8.0,
- ),
- _l2Field(),
- SizedBox(
- height: 8.0,
- ),
- _l3Field(),
- SizedBox(
- height: 8.0,
- ),
- _removedQuantityField(),
- SizedBox(
- height: 8.0,
- ),
- _descriptionField(),
- 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: (newPathFiles,
- deletePathFiles) async {
- Get.find<ChangeFileController>().change(
- newPathFiles, deletePathFiles);
- });
- } else {
- return Center(
- child: CircularProgressIndicator());
- }
- }),
- Row(
- mainAxisAlignment:
- MainAxisAlignment.spaceBetween,
- children: [
- SizedBox(
- width: Get.width / 2 - 16,
- child: FlatButton(
- padding: EdgeInsets.all(14),
- shape: RoundedRectangleBorder(
- borderRadius:
- new BorderRadius.circular(8.0)),
- color: Colors.lightGreen,
- onPressed: () {
- if (_harvest.id != null) {
- Get.to(EditActionHarvestProcessScreen(
- cropId: widget.cropId,
- harvestId: _harvest.id,
- ));
- } else {
- Get.to(EditActionHarvestProcessScreen(
- cropId: widget.cropId));
- }
- },
- child: Text(
- plot_action_harvest_process,
- style: TextStyle(
- color: Colors.white, fontSize: 14),
- ),
- ),
- ),
- SizedBox(
- width: Get.width / 2 - 16,
- child: FlatButton(
- padding: EdgeInsets.all(14),
- shape: RoundedRectangleBorder(
- borderRadius:
- new BorderRadius.circular(8.0)),
- color: Colors.lightGreen,
- onPressed: () {
- if (_harvest.id != null) {
- Get.to(EditActionPackingScreen(
- cropId: widget.cropId,
- harvestId: _harvest.id,
- ));
- } else {
- Get.to(EditActionPackingScreen(
- cropId: widget.cropId));
- }
- },
- child: Text(
- plot_action_packing,
- style: TextStyle(
- color: Colors.white, fontSize: 14),
- ),
- ),
- )
- ],
- ),
- SizedBox(
- height: 8,
- ),
- Row(
- mainAxisAlignment: MainAxisAlignment.start,
- children: [
- SizedBox(
- width: Get.width / 2 - 16,
- child: FlatButton(
- padding: EdgeInsets.all(14),
- shape: RoundedRectangleBorder(
- borderRadius:
- new BorderRadius.circular(8.0)),
- color: Colors.lightGreen,
- onPressed: () {
- if (_harvest.id != null) {
- Get.to(EditActionSellScreen(
- cropId: widget.cropId,
- harvestId: _harvest.id,
- ));
- } else {
- Get.to(EditActionSellScreen(
- cropId: widget.cropId));
- }
- },
- child: Text(
- plot_action_sell,
- style: TextStyle(
- color: Colors.white, fontSize: 14),
- ),
- ),
- )
- ],
- ),
- ],
- );
- },
- ),
- )),
- ))));
- @override
- void dispose() {
- _l1Controller.dispose();
- _l2Controller.dispose();
- _l3Controller.dispose();
- _removedQuantityController.dispose();
- _descriptionController.dispose();
- _executeByController.dispose();
- super.dispose();
- }
- }
|