Browse Source

action other

master
daivph 5 years ago
parent
commit
38d0e055a4
7 changed files with 276 additions and 127 deletions
  1. +40
    -0
      lib/custom_model/Other.dart
  2. +0
    -18
      lib/models/OtherAction.dart
  3. +0
    -27
      lib/models/OtherAction.g.dart
  4. +4
    -5
      lib/models/index.dart
  5. +214
    -76
      lib/presentation/screens/actions/other/sc_edit_action_other.dart
  6. +13
    -1
      lib/presentation/screens/plot_detail/sc_plot_action.dart
  7. +5
    -0
      lib/utils/const_common.dart

+ 40
- 0
lib/custom_model/Other.dart View File

@@ -0,0 +1,40 @@
class Other {
int id;
int cropId;
int activityId;
String media;
String executeDate;
String description;
String activityTypeName;

Other(
{this.id,
this.cropId,
this.activityId,
this.media,
this.executeDate,
this.description,
this.activityTypeName});

Other.fromJson(Map<String, dynamic> json) {
id = json['id'];
cropId = json['cropId'];
activityId = json['activityId'];
media = json['media'];
executeDate = json['executeDate'];
description = json['description'];
activityTypeName = json['activityTypeName'];
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['cropId'] = this.cropId;
data['activityId'] = this.activityId;
data['media'] = this.media;
data['executeDate'] = this.executeDate;
data['description'] = this.description;
data['activityTypeName'] = this.activityTypeName;
return data;
}
}

+ 0
- 18
lib/models/OtherAction.dart View File

@@ -1,18 +0,0 @@
import 'package:json_annotation/json_annotation.dart';

part 'OtherAction.g.dart';

@JsonSerializable()
class OtherAction {
OtherAction();

num id;
num activityId;
String executeDate;
String description;
String activityTypeName;
String workerName;
factory OtherAction.fromJson(Map<String,dynamic> json) => _$OtherActionFromJson(json);
Map<String, dynamic> toJson() => _$OtherActionToJson(this);
}

+ 0
- 27
lib/models/OtherAction.g.dart View File

@@ -1,27 +0,0 @@
// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'OtherAction.dart';

// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************

OtherAction _$OtherActionFromJson(Map<String, dynamic> json) {
return OtherAction()
..id = json['id'] as num
..activityId = json['activityId'] as num
..executeDate = json['executeDate'] as String
..description = json['description'] as String
..activityTypeName = json['activityTypeName'] as String
..workerName = json['workerName'] as String;
}

Map<String, dynamic> _$OtherActionToJson(OtherAction instance) =>
<String, dynamic>{
'id': instance.id,
'activityId': instance.activityId,
'executeDate': instance.executeDate,
'description': instance.description,
'activityTypeName': instance.activityTypeName,
'workerName': instance.workerName,
};

+ 4
- 5
lib/models/index.dart View File

@@ -1,5 +1,4 @@
export 'Supply.dart' ;
export 'Crop.dart' ;
export 'ResourceHelper.dart' ;
export 'ActionType.dart' ;
export 'OtherAction.dart' ;
export 'Supply.dart';
export 'Crop.dart';
export 'ResourceHelper.dart';
export 'ActionType.dart';

+ 214
- 76
lib/presentation/screens/actions/other/sc_edit_action_other.dart View File

@@ -1,17 +1,36 @@
import 'dart:convert';

import 'package:farm_tpf/custom_model/Other.dart';
import 'package:farm_tpf/data/api/app_exception.dart';
import 'package:farm_tpf/data/repository/repository.dart';
import 'package:farm_tpf/models/index.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/other/bloc_get_action_type.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 '../util_action.dart';

class EditActionOtherScreen extends StatefulWidget {
final int cropId;
final bool isEdit;
final int activityId;
EditActionOtherScreen(
{@required this.cropId, this.isEdit = false, this.activityId});
@override
_EditActionOtherScreenState createState() => _EditActionOtherScreenState();
}
@@ -21,44 +40,98 @@ class _EditActionOtherScreenState extends State<EditActionOtherScreen> {
final _repository = Repository();
GlobalKey<FormState> _formKey = GlobalKey();
bool _autoValidate = false;
OtherAction _otherAction = OtherAction();
Other _other = Other();
final _descriptionController = TextEditingController();

List<ActionType> _actionTypes = List<ActionType>();
ActionType _actionType;
var pref = LocalPref();
FlutterToast flutterToast;
TextEditingController _descriptionController = TextEditingController();
TextEditingController _workerNameController = TextEditingController();
String executeTimeView;
DateTime executeTime = DateTime.now();
List<String> filePaths = List<String>();
var changeFileController = Get.put(ChangeFileController());

@override
void initState() {
super.initState();
flutterToast = FlutterToast(context);
//UPDATE
if (_otherAction != null) {
try {
executeTime = DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
.parse(_otherAction.executeDate);
} catch (_) {}
} else {
//ADD NEW
var parsedExecuteDate =
DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(executeTime);
_otherAction.executeDate = "$parsedExecuteDate";
}
changeFileController.initValue();
var parsedExecuteDate =
DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(executeTime);
_other.executeDate = "$parsedExecuteDate";
executeTimeView = DateFormat("dd/MM/yyyy HH:mm").format(executeTime);
_other.cropId = widget.cropId;

if (!widget.isEdit) {
getActionTypeBloc.getActionTypes((data) {
_actionTypes = data;
}, (err) {});
}
}

//Get action types
getActionTypeBloc.getActionTypes((data) {
_actionTypes = data;
for (var item in _actionTypes) {
if (item.id == _otherAction.activityId) {
_actionType = item;
break;
_validateInputs() async {
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
LoadingDialog.showLoadingDialog(context);
filePaths = Get.find<ChangeFileController>().files;
try {
var activityOther = jsonEncode(_other.toJson()).toString();
//ADD NEW
if (_other.activityId == null) {
_repository.createAction((value) {
LoadingDialog.hideLoadingDialog(context);
Get.back(result: value);
Get.snackbar(label_add_success, "Hoạt động khá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.apiAddOther,
paramActivity: ConstCommon.paramsActionOther,
activityAction: activityOther,
filePaths: filePaths);
} else {
//UPDATE
_repository.updateAction((value) {
LoadingDialog.hideLoadingDialog(context);
Get.back(result: value);
Get.snackbar(label_update_success, "Hoạt động khá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.apiUpdateOther,
paramActivity: ConstCommon.paramsActionOther,
activityAction: activityOther,
filePaths: filePaths);
}
} catch (e) {
LoadingDialog.hideLoadingDialog(context);
print(e.toString());
}
}, (err) {});
} else {
_autoValidate = true;
}
}

Widget _btnExecuteTimePicker() {
@@ -70,7 +143,7 @@ class _EditActionOtherScreenState extends State<EditActionOtherScreen> {
setState(() {
var parsedDate =
DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(date);
_otherAction.executeDate = "$parsedDate";
_other.executeDate = "$parsedDate";
executeTimeView = DateFormat("dd/MM/yyyy HH:mm").format(date);
});
}, currentTime: executeTime, locale: LocaleType.vi);
@@ -117,8 +190,7 @@ class _EditActionOtherScreenState extends State<EditActionOtherScreen> {
onChanged: (ActionType newValue) {
setState(() {
_actionType = newValue;
_otherAction.activityId = newValue.id;
_otherAction.activityTypeName = newValue.name;
_other.activityTypeName = newValue.name;
});
},
validator: (value) => value == null ? "Hoạt động" : null,
@@ -141,21 +213,7 @@ class _EditActionOtherScreenState extends State<EditActionOtherScreen> {
decoration: InputDecoration(labelText: "Ghi chú"),
controller: _descriptionController,
onSaved: (newValue) {
_otherAction.description = newValue;
},
);
}

Widget _workerNameField() {
return TextFormField(
keyboardType: TextInputType.text,
decoration: InputDecoration(labelText: "Người thực hiện"),
controller: _workerNameController,
validator: (String value) {
return Validators.validateNotNullOrEmpty(value, "Người thực hiện");
},
onSaved: (newValue) {
_otherAction.workerName = newValue;
_other.description = newValue;
},
);
}
@@ -168,7 +226,13 @@ class _EditActionOtherScreenState extends State<EditActionOtherScreen> {
Icons.done,
color: Colors.black,
),
onPressed: () {},
onPressed: () {
FocusScopeNode currentFocus = FocusScope.of(context);
if (!currentFocus.hasPrimaryFocus) {
currentFocus.unfocus();
}
_validateInputs();
},
);
return <Widget>[iconButton];
}
@@ -188,41 +252,115 @@ class _EditActionOtherScreenState extends State<EditActionOtherScreen> {
title: Text(plot_action_other),
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),
child: MultiBlocProvider(
providers: [
BlocProvider<ActionDetailBloc>(
create: (context) =>
ActionDetailBloc(repository: Repository())
..add(FetchData(
isNeedFetchData: widget.isEdit,
apiActivity: ConstCommon.apiDetailOther,
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);
_other = Other.fromJson(state.item);
_other.activityId = widget.activityId;
_descriptionController.text =
_other.description ?? "";

//select harvest
getActionTypeBloc.getActionTypes((data) {
_actionTypes = data;
for (var item in _actionTypes) {
if (item.name == _other.activityTypeName) {
_actionType = item;
break;
}
}
}, (err) {});

try {
executeTime =
DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
.parse(_other.executeDate);
executeTimeView = DateFormat("dd/MM/yyyy HH:mm")
.format(executeTime);
} catch (_) {}
//Show media
if (_other.media != null) {
await UtilAction.cacheFiles(_other.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,
),
_dropdownAcionTypes(),
SizedBox(
height: 8.0,
),
_desciptionField(),
SizedBox(
height: 8.0,
),
),
_btnExecuteTimePicker(),
SizedBox(
height: 8.0,
),
_dropdownAcionTypes(),
SizedBox(
height: 8.0,
),
_desciptionField(),
SizedBox(
height: 8.0,
),
_workerNameField()
],
),
)))));
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() {
_descriptionController.dispose();
_workerNameController.dispose();
super.dispose();
}
}

+ 13
- 1
lib/presentation/screens/plot_detail/sc_plot_action.dart View File

@@ -80,7 +80,12 @@ class _PlotActionScreenState extends State<PlotActionScreen> {
EditActionUseWaterScreen(
cropId: widget.cropId,
)));
actions.add(ActionType(plot_action_other, null, EditActionOtherScreen()));
actions.add(ActionType(
plot_action_other,
null,
EditActionOtherScreen(
cropId: widget.cropId,
)));
actions.add(ActionType(
plot_action_harvest,
null,
@@ -438,6 +443,13 @@ class ItemInfinityWidget extends StatelessWidget {
activityId: item.id,
isEdit: true,
));
} else {
//TODO: Check other types
Get.to(EditActionOtherScreen(
cropId: item.cropId,
activityId: item.id,
isEdit: true,
));
}
});
}

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

@@ -13,6 +13,7 @@ class ConstCommon {
static const String apiDetailPacking = "api/activity-packing";
static const String apiDetailSell = "api/activity-sell";
static const String apiDetailEnd = "api/activity-end";
static const String apiDetailOther = "api/activity-other";

//nursery
static const String paramsActionNursery = "activityNursery";
@@ -50,6 +51,10 @@ class ConstCommon {
static const String paramsActionEnd = "activityEnd";
static const String apiUpdateEnd = "api/updateActivityEnd";
static const String apiAddEnd = "api/createActivityEnd";
//End
static const String paramsActionOther = "activityOther";
static const String apiUpdateOther = "api/updateActivityOther";
static const String apiAddOther = "api/createActivityOther";

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

Loading…
Cancel
Save