|
- import 'dart:convert';
-
- import 'package:farm_tpf/custom_model/Dung.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_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/controller/ChangeSupplyUsing.dart';
- import 'package:farm_tpf/presentation/screens/actions/dung/widget_dung_supply.dart';
- import 'package:farm_tpf/presentation/screens/actions/state_management_helper/change_file_controller.dart';
- import 'package:farm_tpf/presentation/screens/actions/state_management_helper/change_supply.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 EditActionDungScreen extends StatefulWidget {
- final int cropId;
- final bool isEdit;
- final int activityId;
- EditActionDungScreen(
- {@required this.cropId, this.isEdit = false, this.activityId});
- @override
- _EditActionDungScreenState createState() => _EditActionDungScreenState();
- }
-
- class _EditActionDungScreenState extends State<EditActionDungScreen> {
- final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
- final _repository = Repository();
- GlobalKey<FormState> _formKey = GlobalKey();
- bool _autoValidate = false;
- Dung _dung = Dung();
- var pref = LocalPref();
- final _descriptionController = TextEditingController();
- final _purposeController = TextEditingController();
- final _weatherController = TextEditingController();
- final _executeByController = TextEditingController();
- final _quarantinePeriodController = TextEditingController();
- List<SuppliesUsing> suppliesUsing = new List<SuppliesUsing>();
-
- 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();
- _dung.suppliesUsing = new List<SuppliesUsing>();
- _dung.cropId = widget.cropId;
- }
-
- _validateInputs() async {
- if (_formKey.currentState.validate()) {
- _formKey.currentState.save();
- LoadingDialog.showLoadingDialog(context);
- filePaths = Get.find<ChangeFileController>().newFiles;
- List<SuppliesUsing> newSups = [];
- suppliesUsing.forEach((sup) {
- var newSup = sup;
- newSup.suppliesInWarehouseId = sup.tbSuppliesInWarehouseId;
- newSup.equipmentOfCustomerId = sup.tbEquipmentOfCustomerId;
- newSups.add(newSup);
- });
- _dung.suppliesUsing = newSups;
- _dung.mediaDel = Get.find<ChangeFileController>().deleteFiles;
- var activityDung = jsonEncode(_dung.toJson()).toString();
- //ADD NEW
- if (_dung.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.apiAddDung,
- paramActivity: ConstCommon.paramsActionDung,
- activityAction: activityDung,
- 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.apiUpdateDung,
- paramActivity: ConstCommon.paramsActionDung,
- activityAction: activityDung,
- filePaths: filePaths);
- }
- } else {
- _autoValidate = true;
- }
- }
-
- Widget _btnExecuteTimePicker() {
- return WidgetFieldDateTimePicker(
- initDateTime: executeTime,
- onUpdateDateTime: (selectedDate) {
- _dung.executeDate =
- selectedDate.convertLocalDateTimeToStringUtcDateTime();
- });
- }
-
- Widget _purposeField() {
- return TextFormField(
- keyboardType: TextInputType.text,
- decoration: InputDecoration(labelText: "Lý do sử dụng"),
- controller: _purposeController,
- onSaved: (newValue) {
- _dung.purpose = newValue;
- },
- );
- }
-
- Widget _quarantinePeriodField() {
- return WidgetTextFormFieldNumber(
- hintValue: "Thời gian cách ly",
- textController: _quarantinePeriodController,
- onSaved: (newValue) {
- _dung.quarantinePeriod = newValue.parseDoubleThousand();
- },
- );
- }
-
- Widget _weatherField() {
- return TextFormField(
- keyboardType: TextInputType.text,
- decoration: InputDecoration(labelText: "Thời tiết"),
- controller: _weatherController,
- onSaved: (newValue) {
- _dung.weatherConditions = newValue;
- },
- );
- }
-
- Widget _desciptionField() {
- return TextFormField(
- keyboardType: TextInputType.text,
- decoration: InputDecoration(labelText: "Ghi chú"),
- controller: _descriptionController,
- onSaved: (newValue) {
- _dung.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();
- }
- if (Get.find<ChangeSupply>().selectedSupplyId > 0) {
- Utils.showDialogConfirmSupply(onConfirm: () {
- Get.back();
- _validateInputs();
- });
- } else {
- _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_dung),
- actions: _actionAppBar()),
- body: KeyboardDismisser(
- child: MultiBlocProvider(
- providers: [
- BlocProvider<ActionDetailBloc>(
- create: (context) =>
- ActionDetailBloc(repository: Repository())
- ..add(FetchData(
- isNeedFetchData: widget.isEdit,
- apiActivity: ConstCommon.apiDetailDung,
- 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);
- _dung = Dung.fromJson(state.item);
- _dung.activityId = widget.activityId;
- _purposeController.text = _dung.purpose ?? "";
- _quarantinePeriodController.text = _dung
- .quarantinePeriod
- .formatNumtoStringDecimal();
- _weatherController.text =
- _dung.weatherConditions ?? "";
- _descriptionController.text =
- _dung.description;
- _executeByController.text = _dung.executeBy;
- Get.find<ChangeDateTimePicker>().change(_dung
- .executeDate
- .convertStringServerDateTimeToLocalDateTime());
- //Show media
- if (Validators.stringNotNullOrEmpty(
- _dung.media)) {
- BlocProvider.of<MediaHelperBloc>(context)
- .add(ChangeListMedia(
- items: UtilAction
- .convertFilePathToMedia(
- _dung.media)));
- }
- //list supply
- suppliesUsing = _dung.suppliesUsing;
- Get.find<ChangeSupplyUsing>()
- .changeInitList(suppliesUsing);
- } 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,
- ),
- _purposeField(),
- SizedBox(
- height: 8.0,
- ),
- _quarantinePeriodField(),
- SizedBox(
- height: 8.0,
- ),
- _weatherField(),
- SizedBox(
- height: 8.0,
- ),
- _desciptionField(),
- SizedBox(
- height: 8.0,
- ),
- _executeByField(),
- SizedBox(
- height: 8.0,
- ),
- WidgetDungSupply(
- currentItems: [],
- onChangeSupplies: (value) {
- suppliesUsing = value;
- }),
- 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());
- }
- }),
- ],
- );
- },
- ),
- ))))));
- @override
- void dispose() {
- _quarantinePeriodController.dispose();
- _purposeController.dispose();
- _weatherController.dispose();
- _descriptionController.dispose();
- _executeByController.dispose();
- super.dispose();
- }
- }
|