2025-03-12 17:49:27 +06:30
|
|
|
// ignore_for_file: use_build_context_synchronously
|
|
|
|
|
|
2020-10-15 03:06:13 +06:30
|
|
|
import 'package:fcs/domain/entities/cargo_type.dart';
|
2020-10-07 02:33:06 +06:30
|
|
|
import 'package:fcs/helpers/theme.dart';
|
|
|
|
|
import 'package:fcs/pages/main/util.dart';
|
2020-10-07 18:49:28 +06:30
|
|
|
import 'package:fcs/pages/widgets/input_text.dart';
|
2024-01-25 17:40:35 +06:30
|
|
|
import 'package:fcs/pages/widgets/local_app_bar.dart';
|
2020-10-07 02:33:06 +06:30
|
|
|
import 'package:fcs/pages/widgets/progress.dart';
|
2024-09-25 21:49:09 +06:30
|
|
|
import 'package:flutter/cupertino.dart';
|
2020-06-25 16:19:23 +06:30
|
|
|
import 'package:flutter/material.dart';
|
2025-03-12 17:49:27 +06:30
|
|
|
import 'package:flutter_vector_icons/flutter_vector_icons.dart';
|
2020-10-15 15:49:02 +06:30
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
|
|
2024-09-25 21:49:09 +06:30
|
|
|
import '../widgets/local_text.dart';
|
2020-10-15 15:49:02 +06:30
|
|
|
import 'model/shipment_rate_model.dart';
|
2020-06-25 16:19:23 +06:30
|
|
|
|
|
|
|
|
class CargoEditor extends StatefulWidget {
|
2021-09-10 12:02:08 +06:30
|
|
|
final CargoType? cargo;
|
2025-03-12 17:49:27 +06:30
|
|
|
const CargoEditor({super.key, this.cargo});
|
2020-06-25 16:19:23 +06:30
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
_CargoEditorState createState() => _CargoEditorState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _CargoEditorState extends State<CargoEditor> {
|
2025-03-12 17:49:27 +06:30
|
|
|
TextEditingController descController = TextEditingController();
|
|
|
|
|
TextEditingController rateController = TextEditingController();
|
|
|
|
|
TextEditingController displayIndexController = TextEditingController();
|
2020-06-25 16:19:23 +06:30
|
|
|
|
|
|
|
|
bool _isLoading = false;
|
2021-09-10 12:02:08 +06:30
|
|
|
late CargoType _cargo;
|
2020-10-15 15:49:02 +06:30
|
|
|
bool _isNew = false;
|
2024-02-28 17:07:23 +06:30
|
|
|
final _cargoFormKey = GlobalKey<FormState>();
|
2024-09-25 21:49:09 +06:30
|
|
|
bool _isDefault = false;
|
2025-03-12 17:49:27 +06:30
|
|
|
bool _isMixCargo = false;
|
2020-10-15 15:49:02 +06:30
|
|
|
|
2020-06-25 16:19:23 +06:30
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
2020-10-15 03:06:13 +06:30
|
|
|
if (widget.cargo != null) {
|
2021-09-10 12:02:08 +06:30
|
|
|
_cargo = widget.cargo!;
|
2025-03-12 17:49:27 +06:30
|
|
|
descController.text = _cargo.name ?? "";
|
|
|
|
|
rateController.text = _cargo.rate.toStringAsFixed(2);
|
|
|
|
|
displayIndexController.text = _cargo.displayIndex.toString();
|
2024-09-25 21:49:09 +06:30
|
|
|
_isDefault = _cargo.isDefault;
|
2025-03-12 17:49:27 +06:30
|
|
|
_isMixCargo = _cargo.isMixCargo;
|
2020-10-15 15:49:02 +06:30
|
|
|
} else {
|
|
|
|
|
_isNew = true;
|
2020-06-25 16:19:23 +06:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void dispose() {
|
|
|
|
|
super.dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
2020-10-07 18:49:28 +06:30
|
|
|
final typeBox = InputText(
|
2024-09-25 21:49:09 +06:30
|
|
|
labelTextKey: 'cargo.type',
|
2025-03-12 17:49:27 +06:30
|
|
|
iconData: Ionicons.text,
|
|
|
|
|
controller: descController,
|
2024-09-25 21:49:09 +06:30
|
|
|
autovalidateMode: AutovalidateMode.onUserInteraction,
|
|
|
|
|
validator: (value) {
|
|
|
|
|
if (value == null || value.isEmpty) {
|
|
|
|
|
return "Please insert cargo type";
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2020-10-07 18:49:28 +06:30
|
|
|
final rateBox = InputText(
|
2024-02-28 17:07:23 +06:30
|
|
|
labelTextKey: 'cargo.rate',
|
2025-03-12 17:49:27 +06:30
|
|
|
iconData: Fontisto.dollar,
|
|
|
|
|
controller: rateController,
|
2024-09-25 21:49:09 +06:30
|
|
|
textInputType: TextInputType.number,
|
2024-02-28 17:07:23 +06:30
|
|
|
autovalidateMode: AutovalidateMode.onUserInteraction,
|
|
|
|
|
validator: (value) {
|
|
|
|
|
if (value == null || value.isEmpty) {
|
|
|
|
|
return "Please insert rate";
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
},
|
|
|
|
|
);
|
2024-09-25 21:49:09 +06:30
|
|
|
|
|
|
|
|
final displayIndexBox = InputText(
|
|
|
|
|
labelTextKey: 'cargo.display_index',
|
2025-03-12 17:49:27 +06:30
|
|
|
iconData: FontAwesome.sort_numeric_asc,
|
|
|
|
|
controller: displayIndexController,
|
2024-09-25 21:49:09 +06:30
|
|
|
textInputType: TextInputType.number,
|
|
|
|
|
autovalidateMode: AutovalidateMode.onUserInteraction,
|
2024-10-01 18:15:53 +06:30
|
|
|
// validator: (value) {
|
|
|
|
|
// if (value == null || value.isEmpty) {
|
|
|
|
|
// return "Please insert display index";
|
|
|
|
|
// }
|
|
|
|
|
// return null;
|
|
|
|
|
// },
|
2024-09-25 21:49:09 +06:30
|
|
|
);
|
|
|
|
|
|
|
|
|
|
final defaultBox = Padding(
|
2025-03-12 17:49:27 +06:30
|
|
|
padding: const EdgeInsets.symmetric(vertical: 10),
|
2024-09-25 21:49:09 +06:30
|
|
|
child: Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
|
|
|
|
|
Flexible(
|
|
|
|
|
child: Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: [
|
|
|
|
|
LocalText(
|
|
|
|
|
context,
|
|
|
|
|
'cargo.is_default',
|
|
|
|
|
color: Colors.black54,
|
|
|
|
|
fontSize: 15,
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
Transform.scale(
|
|
|
|
|
scale: 0.7,
|
|
|
|
|
alignment: Alignment.centerRight,
|
|
|
|
|
child: CupertinoSwitch(
|
2025-03-12 17:49:27 +06:30
|
|
|
activeTrackColor: primaryColor,
|
2024-09-25 21:49:09 +06:30
|
|
|
value: _isDefault,
|
|
|
|
|
onChanged: (v) {
|
|
|
|
|
setState(() {
|
|
|
|
|
_isDefault = v;
|
|
|
|
|
});
|
|
|
|
|
}))
|
|
|
|
|
]),
|
|
|
|
|
);
|
|
|
|
|
|
2025-03-12 17:49:27 +06:30
|
|
|
final mixCargoBox =
|
|
|
|
|
Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
|
|
|
|
|
Flexible(
|
|
|
|
|
child: Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: [
|
|
|
|
|
LocalText(
|
|
|
|
|
context,
|
|
|
|
|
'cargo.is_mix',
|
|
|
|
|
color: Colors.black54,
|
|
|
|
|
fontSize: 15,
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
Transform.scale(
|
|
|
|
|
scale: 0.7,
|
|
|
|
|
alignment: Alignment.centerRight,
|
|
|
|
|
child: CupertinoSwitch(
|
|
|
|
|
activeTrackColor: primaryColor,
|
|
|
|
|
value: _isMixCargo,
|
|
|
|
|
onChanged: (v) {
|
|
|
|
|
setState(() {
|
|
|
|
|
_isMixCargo = v;
|
|
|
|
|
});
|
|
|
|
|
}))
|
|
|
|
|
]);
|
|
|
|
|
|
2020-06-25 16:19:23 +06:30
|
|
|
return LocalProgress(
|
|
|
|
|
inAsyncCall: _isLoading,
|
|
|
|
|
child: Scaffold(
|
2024-01-25 17:40:35 +06:30
|
|
|
appBar: LocalAppBar(
|
|
|
|
|
labelKey: "cargo.form.title",
|
|
|
|
|
backgroundColor: Colors.white,
|
|
|
|
|
labelColor: primaryColor,
|
|
|
|
|
arrowColor: primaryColor,
|
|
|
|
|
onBack: () {
|
|
|
|
|
if (isDataChanged()) {
|
|
|
|
|
showConfirmDialog(context, "back.button_confirm", () {
|
2020-12-04 17:28:21 +06:30
|
|
|
Navigator.of(context).pop();
|
2024-01-25 17:40:35 +06:30
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
|
}
|
|
|
|
|
},
|
2020-10-15 15:49:02 +06:30
|
|
|
actions: [
|
2021-10-09 17:08:28 +06:30
|
|
|
_isNew
|
|
|
|
|
? Container()
|
|
|
|
|
: IconButton(
|
2024-01-25 17:40:35 +06:30
|
|
|
icon: Icon(Icons.delete, color: primaryColor),
|
2021-10-09 17:08:28 +06:30
|
|
|
onPressed: _delete,
|
|
|
|
|
)
|
2020-10-15 15:49:02 +06:30
|
|
|
],
|
2020-06-25 16:19:23 +06:30
|
|
|
),
|
2024-02-28 17:07:23 +06:30
|
|
|
body: Form(
|
|
|
|
|
key: _cargoFormKey,
|
|
|
|
|
child: Container(
|
|
|
|
|
padding: EdgeInsets.all(18),
|
|
|
|
|
child: Column(
|
|
|
|
|
children: <Widget>[
|
|
|
|
|
Expanded(
|
|
|
|
|
child: ListView(
|
|
|
|
|
children: <Widget>[
|
|
|
|
|
typeBox,
|
|
|
|
|
rateBox,
|
2024-09-25 21:49:09 +06:30
|
|
|
displayIndexBox,
|
2025-03-12 17:49:27 +06:30
|
|
|
SizedBox(height: 10),
|
2024-09-25 21:49:09 +06:30
|
|
|
defaultBox,
|
2025-03-12 17:49:27 +06:30
|
|
|
mixCargoBox,
|
2024-02-28 17:07:23 +06:30
|
|
|
SizedBox(height: 30),
|
|
|
|
|
],
|
|
|
|
|
),
|
2020-06-25 16:19:23 +06:30
|
|
|
),
|
2024-02-28 17:07:23 +06:30
|
|
|
fcsButton(context, getLocalString(context, "btn.save"),
|
|
|
|
|
callack: _save),
|
2025-03-12 17:49:27 +06:30
|
|
|
SizedBox(height: 30)
|
2024-02-28 17:07:23 +06:30
|
|
|
],
|
|
|
|
|
),
|
2020-06-25 16:19:23 +06:30
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-10-15 15:49:02 +06:30
|
|
|
|
|
|
|
|
_save() async {
|
2024-02-28 17:07:23 +06:30
|
|
|
if (!_cargoFormKey.currentState!.validate()) {
|
2021-10-11 17:09:47 +06:30
|
|
|
return;
|
|
|
|
|
}
|
2020-10-15 15:49:02 +06:30
|
|
|
setState(() {
|
|
|
|
|
_isLoading = true;
|
|
|
|
|
});
|
|
|
|
|
try {
|
|
|
|
|
var shipmentRateModel =
|
|
|
|
|
Provider.of<ShipmentRateModel>(context, listen: false);
|
2025-03-12 17:49:27 +06:30
|
|
|
CargoType cargo = CargoType(
|
|
|
|
|
name: descController.text,
|
|
|
|
|
rate: double.parse(rateController.text),
|
|
|
|
|
displayIndex: displayIndexController.text == ''
|
2024-10-01 18:15:53 +06:30
|
|
|
? 0
|
2025-03-12 17:49:27 +06:30
|
|
|
: int.parse(displayIndexController.text),
|
|
|
|
|
isDefault: _isDefault,
|
|
|
|
|
isMixCargo: _isMixCargo);
|
2020-10-15 15:49:02 +06:30
|
|
|
if (_isNew) {
|
2025-03-12 17:49:27 +06:30
|
|
|
await shipmentRateModel.addCargoType(cargo);
|
2020-10-15 15:49:02 +06:30
|
|
|
} else {
|
2025-03-12 17:49:27 +06:30
|
|
|
cargo.id = _cargo.id;
|
|
|
|
|
await shipmentRateModel.updateCargoType(cargo);
|
2020-10-15 15:49:02 +06:30
|
|
|
}
|
|
|
|
|
Navigator.pop(context);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
showMsgDialog(context, "Error", e.toString());
|
|
|
|
|
} finally {
|
|
|
|
|
setState(() {
|
|
|
|
|
_isLoading = false;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_delete() {
|
2020-10-17 01:40:24 +06:30
|
|
|
showConfirmDialog(context, "cargo.edit.delete.confirm", _deleteCargoType);
|
2020-10-15 15:49:02 +06:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_deleteCargoType() async {
|
|
|
|
|
setState(() {
|
|
|
|
|
_isLoading = true;
|
|
|
|
|
});
|
|
|
|
|
try {
|
|
|
|
|
var shipmentRateModel =
|
|
|
|
|
Provider.of<ShipmentRateModel>(context, listen: false);
|
2025-03-12 17:49:27 +06:30
|
|
|
await shipmentRateModel.deleteCargoType(_cargo.id!);
|
2020-10-15 15:49:02 +06:30
|
|
|
Navigator.pop(context);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
showMsgDialog(context, "Error", e.toString());
|
|
|
|
|
} finally {
|
|
|
|
|
setState(() {
|
|
|
|
|
_isLoading = false;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-08 20:24:15 +06:30
|
|
|
|
|
|
|
|
isDataChanged() {
|
|
|
|
|
if (_isNew) {
|
2025-03-12 17:49:27 +06:30
|
|
|
return descController.text != "" ||
|
|
|
|
|
rateController.text != "" ||
|
|
|
|
|
displayIndexController.text != "" ||
|
|
|
|
|
_isDefault != false ||
|
|
|
|
|
_isMixCargo != false;
|
2020-12-08 20:24:15 +06:30
|
|
|
} else {
|
2025-03-12 17:49:27 +06:30
|
|
|
CargoType cargo = CargoType(
|
|
|
|
|
name: descController.text,
|
|
|
|
|
rate: double.parse(rateController.text),
|
|
|
|
|
displayIndex: int.parse(displayIndexController.text),
|
|
|
|
|
isDefault: _isDefault,
|
|
|
|
|
isMixCargo: _isMixCargo);
|
|
|
|
|
return _cargo.isChangedForEdit(cargo);
|
2020-12-08 20:24:15 +06:30
|
|
|
}
|
|
|
|
|
}
|
2020-06-25 16:19:23 +06:30
|
|
|
}
|