|
- import 'package:farm_tpf/custom_model/CropPlot.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/loading_list_page.dart';
- import 'package:farm_tpf/presentation/custom_widgets/widget_loading.dart';
- import 'package:farm_tpf/presentation/custom_widgets/widget_toast.dart';
- import 'package:farm_tpf/presentation/screens/plot_detail/bloc_plot_information.dart';
- import 'package:farm_tpf/utils/const_color.dart';
- import 'package:farm_tpf/utils/const_string.dart';
- import 'package:flutter/material.dart';
- import 'package:fluttertoast/fluttertoast.dart';
- import 'package:get/get.dart';
- import 'package:keyboard_dismisser/keyboard_dismisser.dart';
- import 'package:farm_tpf/utils/formatter.dart';
-
- class PlotInformationScreen extends StatefulWidget {
- final int cropId;
- PlotInformationScreen({@required this.cropId});
- @override
- _PlotInformationScreenState createState() => _PlotInformationScreenState();
- }
-
- class _PlotInformationScreenState extends State<PlotInformationScreen> {
- final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
- GlobalKey<FormState> _formKey = GlobalKey();
- TextEditingController _statusController = TextEditingController();
- TextEditingController _houseNameController = TextEditingController();
- TextEditingController _codeController = TextEditingController();
- TextEditingController _supplyNameController = TextEditingController();
- TextEditingController _seedingDateController = TextEditingController();
- TextEditingController _timeSoakSeedController = TextEditingController();
- TextEditingController _timeNurserySeedController = TextEditingController();
- TextEditingController _quantityPlantController = TextEditingController();
- TextEditingController _currentPlantController = TextEditingController();
- TextEditingController _timeEndGrowController = TextEditingController();
- TextEditingController _mainTechnicianController = TextEditingController();
- TextEditingController _areaController = TextEditingController();
- TextEditingController _descriptionController = TextEditingController();
-
- FlutterToast flutterToast;
- bool _autoValidate = false;
- TbCropDTO _crop = TbCropDTO();
- final controller = Get.put(DescriptionChangeControler());
- Repository _repository = Repository();
-
- @override
- void initState() {
- super.initState();
- flutterToast = FlutterToast(context);
- getPlotInfoBloc.getPlotInfo(widget.cropId, (data) {
- var result = data as CropPlot;
- _crop = result.tbCropDTO;
- var statusCrop;
- switch (_crop.status) {
- case "STATUS_ARE_ACTIVE":
- statusCrop = plot_status_active;
- break;
- case "STATUS_FINISHED":
- statusCrop = plot_status_end;
- break;
- default:
- statusCrop = plot_status_unknown;
- }
-
- _statusController.text = statusCrop;
- _houseNameController.text = _crop.netHouseName?.toString();
- _codeController.text = _crop.code?.toString();
- _supplyNameController.text = _crop.suppliesName?.toString();
- _areaController.text = _crop.areaM2.formatNumtoStringDecimal();
- _seedingDateController.text = result.sowingDate.format_DDMMYY_HHmm();
- _timeSoakSeedController.text =
- result.soakSeedsTime.formatNumtoStringDecimal();
- _timeNurserySeedController.text =
- result.soakSeedsTime.formatNumtoStringDecimal();
- _quantityPlantController.text =
- result.numberPlants.formatNumtoStringDecimal();
- _currentPlantController.text =
- result.numberCurrentPlants.formatNumtoStringDecimal();
- _timeEndGrowController.text = _crop.endDate.format_DDMMYY_HHmm();
- _descriptionController.text =
- _crop.description == null ? "" : _crop.description.toString();
- var technicians = _crop.tbDetailUsers.map((e) => e.fullName).join(",");
- _mainTechnicianController.text = technicians;
- }, (err) {});
- }
-
- _validateInputs() async {
- if (_formKey.currentState.validate()) {
- _formKey.currentState.save();
- LoadingDialog.showLoadingDialog(context);
- _repository.updatePlot(_crop).then((value) {
- LoadingDialog.hideLoadingDialog(context);
- flutterToast.showToast(
- child: WidgetToast(message: label_update_success));
- controller.initValue();
- }).catchError((error) {
- _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),
- ));
- LoadingDialog.hideLoadingDialog(context);
- });
- } else {
- _autoValidate = true;
- }
- }
-
- Widget _statusField() {
- return TextFormField(
- enabled: false,
- controller: _statusController,
- decoration: InputDecoration(labelText: "Trạng thái"),
- );
- }
-
- Widget _houseNameField() {
- return TextFormField(
- enabled: false,
- controller: _houseNameController,
- decoration: InputDecoration(labelText: "Nhà màng"),
- );
- }
-
- Widget _codeField() {
- return TextFormField(
- enabled: false,
- controller: _codeController,
- decoration: InputDecoration(labelText: "Mã lô"),
- );
- }
-
- Widget _supplyNameField() {
- return TextFormField(
- enabled: false,
- controller: _supplyNameController,
- decoration: InputDecoration(labelText: "Giống"),
- );
- }
-
- Widget _seedingDateField() {
- return TextFormField(
- enabled: false,
- controller: _seedingDateController,
- decoration: InputDecoration(labelText: "Ngày gieo trồng"),
- );
- }
-
- Widget _timeSoakSeedField() {
- return TextFormField(
- enabled: false,
- controller: _timeSoakSeedController,
- decoration: InputDecoration(labelText: "Thời gian ngâm hạt"),
- );
- }
-
- Widget _timeNurserySeedField() {
- return TextFormField(
- enabled: false,
- controller: _timeNurserySeedController,
- decoration: InputDecoration(labelText: "Thời gian vô khây ươm"),
- );
- }
-
- Widget _quantityPlantField() {
- return TextFormField(
- enabled: false,
- controller: _quantityPlantController,
- decoration: InputDecoration(labelText: "Số lượng cây trồng"),
- );
- }
-
- Widget _currentPlantField() {
- return TextFormField(
- enabled: false,
- controller: _currentPlantController,
- decoration: InputDecoration(labelText: "Số lượng cây hiện tại"),
- );
- }
-
- Widget _timeEndGrowField() {
- return TextFormField(
- enabled: false,
- controller: _timeEndGrowController,
- decoration: InputDecoration(labelText: "Ngày kết thúc canh tác"),
- );
- }
-
- Widget _mainTechnicianField() {
- return TextFormField(
- enabled: false,
- controller: _mainTechnicianController,
- decoration: InputDecoration(labelText: "Kỹ sư trực tiếp"),
- );
- }
-
- Widget _areaField() {
- return TextFormField(
- enabled: false,
- controller: _areaController,
- decoration: InputDecoration(labelText: "Diện tích (m\u00B2)"),
- );
- }
-
- Widget _descriptionField() {
- return TextFormField(
- keyboardType: TextInputType.text,
- controller: _descriptionController,
- decoration: InputDecoration(labelText: "Ghi chú"),
- onSaved: (newValue) {
- _crop.description = newValue;
- },
- onChanged: (newValue) {
- controller.changeValue(_crop.description, newValue);
- },
- );
- }
-
- _actionAppBar() {
- return <Widget>[
- GetBuilder<DescriptionChangeControler>(
- builder: (_) {
- return IconButton(
- icon: Icon(
- Icons.done,
- ),
- disabledColor: Colors.grey,
- onPressed: controller.isChanged == false
- ? null
- : () {
- FocusScopeNode currentFocus = FocusScope.of(context);
- if (!currentFocus.hasPrimaryFocus) {
- currentFocus.unfocus();
- }
- _validateInputs();
- },
- );
- },
- )
- ];
- }
-
- @override
- Widget build(BuildContext context) => KeyboardDismisser(
- gestures: [
- GestureType.onTap,
- GestureType.onPanUpdateDownDirection,
- ],
- child: Container(
- color: COLOR_CONST.ITEM_BG,
- child: SafeArea(
- top: false,
- bottom: true,
- child: Scaffold(
- key: _scaffoldKey,
- appBar: AppBar(
- centerTitle: true,
- title: Text("Thông tin lô"),
- actions: _actionAppBar()),
- body: KeyboardDismisser(
- child: StreamBuilder(
- stream: getPlotInfoBloc.actions,
- builder: (BuildContext context,
- AsyncSnapshot<dynamic> snapshot) {
- if (snapshot.hasData) {
- return Form(
- key: _formKey,
- autovalidate: _autoValidate,
- child: SingleChildScrollView(
- padding: EdgeInsets.all(8.0),
- child: Column(
- children: <Widget>[
- _statusField(),
- SizedBox(
- height: 4.0,
- ),
- _houseNameField(),
- SizedBox(
- height: 4.0,
- ),
- _codeField(),
- SizedBox(
- height: 4.0,
- ),
- _supplyNameField(),
- SizedBox(
- height: 4.0,
- ),
- _seedingDateField(),
- SizedBox(
- height: 4.0,
- ),
- _timeSoakSeedField(),
- SizedBox(
- height: 4.0,
- ),
- _timeNurserySeedField(),
- SizedBox(
- height: 4.0,
- ),
- _quantityPlantField(),
- SizedBox(
- height: 4.0,
- ),
- _currentPlantField(),
- SizedBox(
- height: 4.0,
- ),
- _timeEndGrowField(),
- SizedBox(
- height: 4.0,
- ),
- _mainTechnicianField(),
- SizedBox(
- height: 4.0,
- ),
- _areaField(),
- SizedBox(
- height: 4.0,
- ),
- _descriptionField()
- ],
- ),
- ));
- } else if (snapshot.hasError) {
- return Center(
- child: Text(snapshot.error.toString()),
- );
- } else {
- return LoadingListPage();
- }
- }))))));
-
- @override
- void dispose() {
- _statusController.dispose();
- _houseNameController.dispose();
- _codeController.dispose();
- _supplyNameController.dispose();
- _seedingDateController.dispose();
- _timeSoakSeedController.dispose();
- _timeNurserySeedController.dispose();
- _quantityPlantController.dispose();
- _currentPlantController.dispose();
- _timeEndGrowController.dispose();
- _mainTechnicianController.dispose();
- _areaController.dispose();
- _descriptionController.dispose();
- super.dispose();
- }
- }
-
- class DescriptionChangeControler extends GetxController {
- bool isChanged = false;
-
- void initValue() {
- isChanged = false;
- update();
- }
-
- void changeValue(String oldValue, String newValue) {
- if (oldValue != newValue) {
- isChanged = true;
- } else {
- isChanged = false;
- }
- if (oldValue.isNullOrBlank && newValue.isEmpty) {
- isChanged = false;
- }
- update();
- }
- }
|