Browse Source

action harvest

master
daivph 5 years ago
parent
commit
cf08cf7b65
4 changed files with 421 additions and 7 deletions
  1. +8
    -0
      lib/custom_model/Harvest.dart
  2. +396
    -5
      lib/presentation/screens/actions/harvest/sc_edit_action_harvest.dart
  3. +12
    -2
      lib/presentation/screens/plot_detail/sc_plot_action.dart
  4. +5
    -0
      lib/utils/const_common.dart

+ 8
- 0
lib/custom_model/Harvest.dart View File

class Harvest { class Harvest {
int id; int id;
int cropId;
int activityId; int activityId;
String executeDate; String executeDate;
String description; String description;
String media;
num collectedQuantityLv1; num collectedQuantityLv1;
num collectedQuantityLv2; num collectedQuantityLv2;
num collectedQuantityLv3; num collectedQuantityLv3;


Harvest( Harvest(
{this.id, {this.id,
this.cropId,
this.activityId, this.activityId,
this.executeDate, this.executeDate,
this.description, this.description,
this.media,
this.collectedQuantityLv1, this.collectedQuantityLv1,
this.collectedQuantityLv2, this.collectedQuantityLv2,
this.collectedQuantityLv3, this.collectedQuantityLv3,


Harvest.fromJson(Map<String, dynamic> json) { Harvest.fromJson(Map<String, dynamic> json) {
id = json['id']; id = json['id'];
cropId = json['cropId'];
activityId = json['activityId']; activityId = json['activityId'];
executeDate = json['executeDate']; executeDate = json['executeDate'];
description = json['description']; description = json['description'];
media = json['media'];
collectedQuantityLv1 = json['collectedQuantityLv1']; collectedQuantityLv1 = json['collectedQuantityLv1'];
collectedQuantityLv2 = json['collectedQuantityLv2']; collectedQuantityLv2 = json['collectedQuantityLv2'];
collectedQuantityLv3 = json['collectedQuantityLv3']; collectedQuantityLv3 = json['collectedQuantityLv3'];
Map<String, dynamic> toJson() { Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>(); final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id; data['id'] = this.id;
data['cropId'] = this.cropId;
data['activityId'] = this.activityId; data['activityId'] = this.activityId;
data['executeDate'] = this.executeDate; data['executeDate'] = this.executeDate;
data['description'] = this.description; data['description'] = this.description;
data['media'] = this.media;
data['collectedQuantityLv1'] = this.collectedQuantityLv1; data['collectedQuantityLv1'] = this.collectedQuantityLv1;
data['collectedQuantityLv2'] = this.collectedQuantityLv2; data['collectedQuantityLv2'] = this.collectedQuantityLv2;
data['collectedQuantityLv3'] = this.collectedQuantityLv3; data['collectedQuantityLv3'] = this.collectedQuantityLv3;

+ 396
- 5
lib/presentation/screens/actions/harvest/sc_edit_action_harvest.dart View File

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_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/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_string.dart';
import 'package:farm_tpf/utils/const_style.dart';
import 'package:farm_tpf/utils/validators.dart';
import 'package:flutter/material.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 { class EditActionHarvestScreen extends StatefulWidget {
final int cropId;
final bool isEdit;
final int activityId;
EditActionHarvestScreen(
{@required this.cropId, this.isEdit = false, this.activityId});
@override @override
_EditActionHarvestScreenState createState() => _EditActionHarvestScreenState createState() =>
_EditActionHarvestScreenState(); _EditActionHarvestScreenState();
} }


class _EditActionHarvestScreenState extends State<EditActionHarvestScreen> { 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();

String executeTimeView;
DateTime executeTime = DateTime.now();
List<String> filePaths = List<String>();
var changeFileController = Get.put(ChangeFileController());

@override @override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(plot_action_harvest),
),
void initState() {
super.initState();
changeFileController.initValue();
var parsedExecuteDate =
DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(executeTime);
_harvest.executeDate = "$parsedExecuteDate";
executeTimeView = DateFormat("dd/MM/yyyy HH:mm").format(executeTime);
_harvest.cropId = widget.cropId;
}

_validateInputs() async {
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
LoadingDialog.showLoadingDialog(context);
filePaths = Get.find<ChangeFileController>().files;
try {
var activityHarvest = jsonEncode(_harvest.toJson()).toString();
//ADD NEW
if (_harvest.activityId == null) {
_repository.createAction((value) {
LoadingDialog.hideLoadingDialog(context);
Get.back(result: value);
Get.snackbar(label_add_success, "Hoạt động thu hoạch",
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.apiAddHarvest,
paramActivity: ConstCommon.paramsActionHarvest,
activityAction: activityHarvest,
filePaths: filePaths);
} else {
//UPDATE
_repository.updateAction((value) {
LoadingDialog.hideLoadingDialog(context);
Get.back(result: value);
Get.snackbar(label_update_success, "Hoạt động thu hoạch",
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.apiUpdateHarvest,
paramActivity: ConstCommon.paramsActionHarvest,
activityAction: activityHarvest,
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);
_harvest.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 _l1Field() {
return TextFormField(
keyboardType: TextInputType.numberWithOptions(decimal: true),
inputFormatters: [
ThousandsFormatter(
formatter: NumberFormat("#,###.##", "es"), allowFraction: true)
],
decoration: InputDecoration(labelText: "Số lượng/khối lượng loại 1"),
controller: _l1Controller,
validator: (String value) {
return Validators.validNumber(value, "Số lượng/khối lượng loại 1");
},
onSaved: (newValue) {
_harvest.collectedQuantityLv1 = newValue.parseDoubleThousand();
},
);
}

Widget _l2Field() {
return TextFormField(
keyboardType: TextInputType.numberWithOptions(decimal: true),
inputFormatters: [
ThousandsFormatter(
formatter: NumberFormat("#,###.##", "es"), allowFraction: true)
],
decoration: InputDecoration(labelText: "Số lượng/khối lượng loại 2"),
controller: _l2Controller,
validator: (String value) {
return Validators.validNumber(value, "Số lượng/khối lượng loại 2");
},
onSaved: (newValue) {
_harvest.collectedQuantityLv2 = newValue.parseDoubleThousand();
},
);
}

Widget _l3Field() {
return TextFormField(
keyboardType: TextInputType.numberWithOptions(decimal: true),
inputFormatters: [
ThousandsFormatter(
formatter: NumberFormat("#,###.##", "es"), allowFraction: true)
],
decoration: InputDecoration(labelText: "Số lượng/khối lượng loại 3"),
controller: _l3Controller,
validator: (String value) {
return Validators.validNumber(value, "Số lượng/khối lượng loại 3");
},
onSaved: (newValue) {
_harvest.collectedQuantityLv3 = newValue.parseDoubleThousand();
},
);
}

Widget _removedQuantityField() {
return TextFormField(
keyboardType: TextInputType.numberWithOptions(decimal: true),
inputFormatters: [
ThousandsFormatter(
formatter: NumberFormat("#,###.##", "es"), allowFraction: true)
],
decoration: InputDecoration(labelText: "Số lượng/khối lượng loại bỏ"),
controller: _removedQuantityController,
validator: (String value) {
return Validators.validNumber(value, "Số lượng/khối lượng loại bỏ");
},
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;
},
); );
} }

_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.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 ?? "";

try {
executeTime =
DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
.parse(_harvest.executeDate);
executeTimeView = DateFormat("dd/MM/yyyy HH:mm")
.format(executeTime);
} catch (_) {}
//Show media
if (_harvest.media != null) {
await UtilAction.cacheFiles(_harvest.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,
),
_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,
),
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() {
_l1Controller.dispose();
_l2Controller.dispose();
_l3Controller.dispose();
_removedQuantityController.dispose();
_descriptionController.dispose();
super.dispose();
}
} }

+ 12
- 2
lib/presentation/screens/plot_detail/sc_plot_action.dart View File

cropId: widget.cropId, cropId: widget.cropId,
))); )));
actions.add(ActionType(plot_action_other, null, EditActionOtherScreen())); actions.add(ActionType(plot_action_other, null, EditActionOtherScreen()));
actions
.add(ActionType(plot_action_harvest, null, EditActionHarvestScreen()));
actions.add(ActionType(
plot_action_harvest,
null,
EditActionHarvestScreen(
cropId: widget.cropId,
)));
actions.add(ActionType(plot_action_finish, null, EditActionEndScreen())); actions.add(ActionType(plot_action_finish, null, EditActionEndScreen()));
} }


activityId: item.id, activityId: item.id,
isEdit: true, isEdit: true,
)); ));
} else if (item.activityTypeName == "ACTIVE_TYPE_HARVEST") {
Get.to(EditActionHarvestScreen(
cropId: item.cropId,
activityId: item.id,
isEdit: true,
));
} }
}); });
} }

+ 5
- 0
lib/utils/const_common.dart View File

static const String apiDetailEnvUpdate = "api/activity-environment-update"; static const String apiDetailEnvUpdate = "api/activity-environment-update";
static const String apiDetailDisease = "api/activity-pests-investigation"; static const String apiDetailDisease = "api/activity-pests-investigation";
static const String apiDetailUseWater = "api/activity-water-usage"; static const String apiDetailUseWater = "api/activity-water-usage";
static const String apiDetailHarvest = "api/activity-harvest";


//nursery //nursery
static const String paramsActionNursery = "activityNursery"; static const String paramsActionNursery = "activityNursery";
static const String paramsActionUseWater = "activityWaters"; static const String paramsActionUseWater = "activityWaters";
static const String apiUpdateUseWater = "api/updateWaters"; static const String apiUpdateUseWater = "api/updateWaters";
static const String apiAddUserWater = "api/createWaters"; static const String apiAddUserWater = "api/createWaters";
//Harvest
static const String paramsActionHarvest = "activityHarvest";
static const String apiUpdateHarvest = "api/updateActivityHarvest";
static const String apiAddHarvest = "api/createActivityHarvest";


static const String supplyTypeSeed = "GIONG"; static const String supplyTypeSeed = "GIONG";
static const String supplyTypeDung = "PHANBON"; static const String supplyTypeDung = "PHANBON";

Loading…
Cancel
Save