|
- import 'package:farm_tpf/custom_model/SuppliesUsing.dart';
- import 'package:farm_tpf/custom_model/Supply.dart';
- import 'package:farm_tpf/presentation/custom_widgets/widget_text_form_field.dart';
- import 'package:farm_tpf/presentation/screens/actions/controller/ChangeFieldInForm.dart';
- import 'package:farm_tpf/presentation/screens/actions/controller/ChangeFormButton.dart';
- import 'package:farm_tpf/presentation/screens/actions/controller/ChangeSupplyUsing.dart';
- import 'package:farm_tpf/presentation/screens/actions/controller/ChangeUnit.dart';
- import 'package:farm_tpf/presentation/screens/actions/state_management_helper/change_supply.dart';
- import 'package:farm_tpf/presentation/screens/actions/util_action.dart';
- import 'package:farm_tpf/presentation/screens/resources/sc_resource_helper.dart';
- import 'package:farm_tpf/utils/const_color.dart';
- import 'package:farm_tpf/utils/const_common.dart';
- import 'package:farm_tpf/utils/const_style.dart';
- import 'package:farm_tpf/utils/validators.dart';
- import 'package:flutter/material.dart';
- import 'package:get/get.dart';
- import 'package:farm_tpf/utils/formatter.dart';
-
- class WidgetPlantSupply extends StatefulWidget {
- final List<SuppliesUsing> currentItems;
- final Function(List<SuppliesUsing> supplyChanges) onChangeSupplies;
- WidgetPlantSupply({this.currentItems, @required this.onChangeSupplies});
- @override
- _WidgetPlantSupplyState createState() => _WidgetPlantSupplyState();
- }
-
- class _WidgetPlantSupplyState extends State<WidgetPlantSupply> {
- final _dosageController = TextEditingController();
- final _quantityController = TextEditingController();
- final changeSelectedSupply = Get.put(ChangeSupply());
- final changeSupplyUsing = Get.put(ChangeSupplyUsing());
- final changeUnit = Get.put(ChangeUnit());
- final changeButton = Get.put(ChangeButtonInForm());
- final changeFormField = Get.put(ChangeFieldFormSupply());
-
- @override
- void initState() {
- super.initState();
- changeSelectedSupply.initValue();
- changeSupplyUsing.init(widget.currentItems);
- changeUnit.initValue();
- changeButton.resetValue();
- changeFormField.init();
- }
-
- Widget _buildListSupply() {
- return GetBuilder<ChangeSupplyUsing>(builder: (value) {
- widget.onChangeSupplies(value.currentItems);
- if (value.currentItems.isEmpty) {
- return Container();
- } else {
- return Container(
- height: 80,
- child: ListView.builder(
- physics: ClampingScrollPhysics(),
- scrollDirection: Axis.horizontal,
- shrinkWrap: true,
- itemCount: value.currentItems.length,
- itemBuilder: (context, index) {
- return GestureDetector(
- onTap: () {
- print("edit");
- changeSupplyUsing.changeIndexEdit(index);
- changeButton.updateToEdit(true);
- var editedSupplyUsing = value.currentItems[index];
- var editedSupply = Supply()
- ..id = editedSupplyUsing.tbSuppliesInWarehouseId
- ..tbSuppliesName = editedSupplyUsing.supplyName
- ..unit = editedSupplyUsing.supplyUnit;
- changeSelectedSupply.change(editedSupply);
- changeUnit
- .updateListByUnitName(editedSupplyUsing.supplyUnit);
- changeUnit.updateSelected(editedSupplyUsing.supplyUnit);
- _dosageController.text = editedSupplyUsing.dosage;
- _quantityController.text = editedSupplyUsing.quantity
- .formatNumtoStringDecimal();
- },
- child: Card(
- child: Stack(
- alignment: Alignment.bottomCenter,
- overflow: Overflow.visible,
- children: <Widget>[
- Positioned(
- child: ClipRRect(
- borderRadius: BorderRadius.circular(8),
- child: Container(
- padding: EdgeInsets.all(4),
- width: 150,
- child: Column(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- SizedBox(
- height: 12.0,
- ),
- Flexible(
- child: Text(
- value.currentItems[index].supplyName ??
- "",
- overflow: TextOverflow.ellipsis,
- maxLines: 1),
- ),
- Validators.stringNotNullOrEmpty(
- value.currentItems[index].dosage)
- ? Flexible(
- child: Text(
- "${value.currentItems[index].dosage ?? ""}"))
- : SizedBox(),
- Validators.stringNotNullOrEmpty(value
- .currentItems[index].quantity
- .formatNumtoStringDecimal())
- ? Flexible(
- child: Text(
- "${value.currentItems[index].quantity.formatNumtoStringDecimal() ?? ""} ${value.currentItems[index].supplyUnit ?? ""}"))
- : SizedBox(),
- ],
- ),
- ),
- )),
- Positioned(
- top: -10,
- right: -10,
- child: IconButton(
- icon: Icon(
- Icons.cancel,
- color: Colors.redAccent,
- ),
- onPressed: () {
- changeSupplyUsing.deleteSupply(index);
- }),
- )
- ],
- )));
- }));
- }
- });
- }
-
- Widget _btnSelectSubstrates() {
- return GetBuilder<ChangeSupply>(builder: (data) {
- return FlatButton(
- padding:
- EdgeInsets.only(top: 0.0, right: 0.0, bottom: 0.0, left: 0.0),
- onPressed: () {
- Navigator.of(context)
- .push(MaterialPageRoute(
- builder: (_) => ResourceHelperScreen(
- titleName: "Hoá chất xử lý",
- type: ConstCommon.supplyTypeAll,
- selectedId: changeSelectedSupply.selectedSupplyId),
- fullscreenDialog: false))
- .then((value) {
- if (value != null) {
- var result = value as Supply;
- changeSelectedSupply.change(result);
- changeUnit.updateListByUnitName(result.unit);
- changeFormField.change(true);
- }
- });
- },
- child: Container(
- padding: EdgeInsets.only(
- top: 0.0, right: 0.0, bottom: 10.5, left: 0.0),
- decoration: BoxDecoration(
- border: kBorderTextField,
- ),
- child: Row(
- children: [
- GetBuilder<ChangeSupply>(
- builder: (_) => Expanded(
- child: Text(
- changeSelectedSupply.selectedSupplyName ??
- "Hoá chất xử lý",
- style: TextStyle(
- fontSize: 14.0, color: Colors.black87)))),
- Icon(
- Icons.arrow_drop_down,
- color: Colors.grey,
- ),
- ],
- )));
- });
- }
-
- Widget _dropdownUnitTypes() {
- return GetBuilder<ChangeUnit>(builder: (data) {
- return DropdownButtonFormField<String>(
- itemHeight: 100,
- value: data.selectedUnit.isEmpty ? null : data.selectedUnit,
- items: data.currentUnits
- .map((label) => DropdownMenuItem(
- child: Text(label),
- value: label,
- ))
- .toList(),
- onChanged: (value) {
- var currentQuantity = _quantityController.text;
- num assignValue = currentQuantity.parseDoubleThousand();
- if (assignValue != null) {
- var oldSelected = data.selectedUnit;
- if (oldSelected == value) {
- } else {
- assignValue = UtilAction.convertUnit(
- inputValue: assignValue,
- oldUnit: oldSelected,
- newUnit: value);
- }
- _quantityController.text = assignValue.formatNumtoStringDecimal();
- }
- changeUnit.updateSelected(value);
- },
- );
- });
- }
-
- _quantityField() {
- return WidgetTextFormFieldNumber(
- hintValue: "Tổng lượng sử dụng",
- textController: _quantityController,
- onChanged: (value) {
- if (!Validators.stringNotNullOrEmpty(value) &&
- !Validators.stringNotNullOrEmpty(_dosageController.text) &&
- Get.find<ChangeSupply>().selectedSupplyId <= 0) {
- changeFormField.change(false);
- } else {
- changeFormField.change(true);
- }
- },
- );
- }
-
- _buttonInForm() {
- return GetBuilder<ChangeButtonInForm>(builder: (_) {
- return Align(
- alignment: Alignment.centerRight,
- child: Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: [
- _.isEdit
- ? OutlineButton(
- shape: RoundedRectangleBorder(
- borderRadius: new BorderRadius.circular(8.0)),
- child: Text("Huỷ"),
- onPressed: () {
- changeButton.resetValue();
- _resetForm();
- _hidenKeyboard(context);
- })
- : SizedBox(),
- _.isEdit
- ? FlatButton(
- color: COLOR_CONST.DEFAULT,
- shape: RoundedRectangleBorder(
- borderRadius: new BorderRadius.circular(8.0)),
- onPressed: () {
- var currentSupply = changeSelectedSupply.currentSupply;
- if (currentSupply.id != null) {
- var quantityWithCurrentSupplyUnit =
- UtilAction.convertUnit(
- inputValue: _quantityController.text
- .parseDoubleThousand(),
- oldUnit: changeUnit.selectedUnit,
- newUnit:
- changeSelectedSupply.currentSupply.unit);
- SuppliesUsing newSup = SuppliesUsing()
- ..dosage = _dosageController.text
- ..quantity = quantityWithCurrentSupplyUnit
- ..tbSuppliesInWarehouseId = currentSupply.id
- ..suppliesInWarehouseId = currentSupply.id
- ..supplyName = currentSupply.tbSuppliesName
- ..supplyUnit = currentSupply.unit
- ..unit = currentSupply.unit;
- changeSupplyUsing.editSupply(
- changeSupplyUsing.currentIndex, newSup);
- _resetForm();
- _hidenKeyboard(context);
- }
- },
- child: Text(
- "Sửa",
- style: TextStyle(color: Colors.white),
- ))
- : FlatButton(
- color: COLOR_CONST.DEFAULT,
- shape: RoundedRectangleBorder(
- borderRadius: new BorderRadius.circular(8.0)),
- onPressed: () {
- var currentSupply = changeSelectedSupply.currentSupply;
- if (currentSupply.id != null) {
- var quantityWithCurrentSupplyUnit =
- UtilAction.convertUnit(
- inputValue: _quantityController.text
- .parseDoubleThousand(),
- oldUnit: changeUnit.selectedUnit,
- newUnit:
- changeSelectedSupply.currentSupply.unit);
- SuppliesUsing newSup = SuppliesUsing()
- ..dosage = _dosageController.text
- ..quantity = quantityWithCurrentSupplyUnit
- ..tbSuppliesInWarehouseId = currentSupply.id
- ..suppliesInWarehouseId = currentSupply.id
- ..supplyName = currentSupply.tbSuppliesName
- ..supplyUnit = currentSupply.unit
- ..unit = currentSupply.unit;
- changeSupplyUsing.addSupply(newSup);
- _resetForm();
- _hidenKeyboard(context);
- }
- },
- child: Text(
- "Thêm",
- style: TextStyle(color: Colors.white),
- ))
- ],
- ),
- );
- });
- }
-
- Widget _formEdit() {
- return Container(
- padding: EdgeInsets.all(8.0),
- decoration: BoxDecoration(
- shape: BoxShape.rectangle,
- borderRadius: BorderRadius.circular(10),
- color: Colors.white,
- border: Border.all(color: COLOR_CONST.DEFAULT)),
- child: Column(
- children: [
- Container(
- width: double.infinity,
- child: Text(
- "Hoá chất xử lý",
- style: TextStyle(color: Colors.black54, fontSize: 13.0),
- ),
- ),
- _btnSelectSubstrates(),
- TextFormField(
- keyboardType: TextInputType.text,
- controller: _dosageController,
- decoration: InputDecoration(labelText: "Liều lượng"),
- onSaved: (newValue) {},
- onChanged: (value) {
- if (!Validators.stringNotNullOrEmpty(
- _quantityController.text) &&
- !Validators.stringNotNullOrEmpty(value) &&
- Get.find<ChangeSupply>().selectedSupplyId <= 0) {
- changeFormField.change(false);
- } else {
- changeFormField.change(true);
- }
- },
- ),
- Row(
- mainAxisSize: MainAxisSize.min,
- mainAxisAlignment: MainAxisAlignment.spaceEvenly,
- crossAxisAlignment: CrossAxisAlignment.center,
- children: [
- Expanded(
- flex: 2,
- child: Container(
- height: 70,
- child: _quantityField(),
- ),
- ),
- SizedBox(
- width: 16.0,
- ),
- Expanded(
- flex: 1,
- child: Align(
- alignment: Alignment.bottomCenter,
- child: _dropdownUnitTypes(),
- )),
- ]),
- _buttonInForm()
- ],
- ));
- }
-
- _resetForm() {
- changeSupplyUsing.changeIndexEdit(-1);
- changeButton.resetValue();
- _dosageController.text = "";
- _quantityController.text = "";
- changeUnit.initValue();
- changeSelectedSupply.initValue();
- changeFormField.change(false);
- }
-
- _hidenKeyboard(BuildContext context) {
- FocusScopeNode currentFocus = FocusScope.of(context);
- if (!currentFocus.hasPrimaryFocus) {
- currentFocus.unfocus();
- }
- }
-
- @override
- Widget build(BuildContext context) {
- return Column(
- children: [
- _buildListSupply(),
- SizedBox(
- height: 8.0,
- ),
- _formEdit()
- ],
- );
- }
- }
|