Files
fcs/lib/pages/discount/discount_editor.dart

223 lines
6.6 KiB
Dart
Raw Normal View History

2020-10-07 02:33:06 +06:30
import 'package:fcs/domain/entities/discount.dart';
import 'package:fcs/helpers/theme.dart';
2020-10-15 15:49:02 +06:30
import 'package:fcs/pages/discount/model/discount_model.dart';
2020-10-07 02:33:06 +06:30
import 'package:fcs/pages/main/util.dart';
2024-02-07 17:26:29 +06:30
import 'package:fcs/pages/user_search/user_search.dart';
2020-10-07 18:49:28 +06:30
import 'package:fcs/pages/widgets/display_text.dart';
import 'package:fcs/pages/widgets/input_text.dart';
import 'package:fcs/pages/widgets/local_app_bar.dart';
2020-10-07 02:33:06 +06:30
import 'package:fcs/pages/widgets/progress.dart';
2020-06-26 16:04:40 +06:30
import 'package:flutter/material.dart';
2021-09-10 14:29:55 +06:30
import 'package:flutter_vector_icons/flutter_vector_icons.dart';
2020-06-26 16:04:40 +06:30
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
2020-10-15 15:49:02 +06:30
import 'package:provider/provider.dart';
2020-06-26 16:04:40 +06:30
class DiscountEditor extends StatefulWidget {
2021-09-10 12:02:08 +06:30
final Discount? discount;
2020-06-26 16:04:40 +06:30
2021-09-10 12:02:08 +06:30
const DiscountEditor({Key? key, this.discount}) : super(key: key);
2020-06-26 16:04:40 +06:30
@override
_DiscountEditorState createState() => _DiscountEditorState();
}
class _DiscountEditorState extends State<DiscountEditor> {
bool _isLoading = false;
Discount _discount = new Discount();
TextEditingController _codeController = new TextEditingController();
TextEditingController _amountController = new TextEditingController();
TextEditingController _statusController = new TextEditingController();
2020-10-15 15:49:02 +06:30
bool _isNew = false;
2020-10-15 18:48:32 +06:30
String customerName = '';
String customerId = '';
final _discountFormKey = GlobalKey<FormState>();
2020-06-26 16:04:40 +06:30
@override
void initState() {
super.initState();
if (widget.discount != null) {
2021-09-10 12:02:08 +06:30
_discount = widget.discount!;
2021-09-10 15:23:13 +06:30
_codeController.text = _discount.code ?? "";
_amountController.text = _discount.amount.toStringAsFixed(2);
2021-09-10 15:23:13 +06:30
_statusController.text = _discount.status ?? '';
customerName = _discount.customerName ?? "";
customerId = _discount.customerId ?? "";
2020-06-26 16:04:40 +06:30
} else {
2020-10-15 15:49:02 +06:30
_isNew = true;
2020-06-26 16:04:40 +06:30
}
}
@override
Widget build(BuildContext context) {
2020-10-07 18:49:28 +06:30
final codeBox = InputText(
labelTextKey: 'discount.code',
iconData: FontAwesomeIcons.algolia,
controller: _codeController,
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: (value) {
if (value == null || value.isEmpty) {
return "Please insert code";
}
return null;
},
);
2020-10-15 15:49:02 +06:30
2020-10-07 18:49:28 +06:30
final amountBox = InputText(
labelTextKey: 'discount.amount',
iconData: FontAwesomeIcons.moneyBill,
controller: _amountController,
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: (value) {
if (value == null || value.isEmpty) {
return "Please insert amount";
}
return null;
},
);
2020-10-15 15:49:02 +06:30
2020-10-07 18:49:28 +06:30
final statusBox = DisplayText(
text: _statusController.text,
2020-10-15 15:49:02 +06:30
labelTextKey: "discount.status",
2020-10-07 18:49:28 +06:30
iconData: Icons.av_timer,
);
2020-10-15 18:48:32 +06:30
final customerBox = Row(
children: <Widget>[
Expanded(
child: DisplayText(
2021-09-10 12:02:08 +06:30
text: customerName,
2020-10-15 18:48:32 +06:30
labelTextKey: "discount.name",
iconData: Feather.user,
)),
IconButton(
icon: Icon(Icons.search, color: primaryColor),
2021-01-10 15:56:27 +06:30
onPressed: () => searchUser(context, onUserSelect: (u) {
2020-10-15 18:48:32 +06:30
setState(() {
2021-09-10 15:23:13 +06:30
customerId = u.id ?? "";
customerName = u.name ?? "";
2020-10-15 18:48:32 +06:30
});
2021-09-13 11:10:17 +06:30
}, popPage: true)),
2020-10-15 18:48:32 +06:30
],
);
2020-06-26 16:04:40 +06:30
return LocalProgress(
inAsyncCall: _isLoading,
child: Scaffold(
backgroundColor: Colors.white,
appBar: LocalAppBar(
labelKey: "discount.form",
backgroundColor: Colors.white,
arrowColor: primaryColor,
labelColor: primaryColor,
2020-10-15 15:49:02 +06:30
actions: [
_isNew
? const SizedBox()
: IconButton(
icon: Icon(Icons.delete, color: primaryColor),
onPressed: _delete)
2020-10-15 15:49:02 +06:30
],
2020-06-26 16:04:40 +06:30
),
body: Form(
key: _discountFormKey,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: <Widget>[
Expanded(
child: ListView(
children: <Widget>[
codeBox,
amountBox,
SizedBox(height: 7),
customerBox,
SizedBox(height: 7),
widget.discount == null
? Container()
: Container(
padding: EdgeInsets.only(top: 5),
child: statusBox,
),
],
),
2020-06-26 16:04:40 +06:30
),
fcsButton(context, getLocalString(context, "btn.save"),
callack: _save),
SizedBox(
height: 30,
)
],
),
2020-06-26 16:04:40 +06:30
),
)),
);
}
2020-10-15 15:49:02 +06:30
_save() async {
if (!_discountFormKey.currentState!.validate()) {
return null;
}
2020-10-15 15:49:02 +06:30
setState(() {
_isLoading = true;
});
try {
DiscountModel discountModel =
Provider.of<DiscountModel>(context, listen: false);
Discount _discount = Discount(
code: _codeController.text,
2020-10-15 18:48:32 +06:30
customerName: customerName,
customerId: customerId,
2020-10-15 15:49:02 +06:30
amount: double.parse(_amountController.text));
if (_isNew) {
await discountModel.addDiscount(_discount);
} else {
2021-09-10 12:02:08 +06:30
_discount.id = this._discount.id;
2020-10-15 15:49:02 +06:30
await discountModel.updateDiscount(_discount);
}
Navigator.pop(context);
} catch (e) {
showMsgDialog(context, "Error", e.toString());
} finally {
setState(() {
_isLoading = false;
});
}
}
_delete() {
showConfirmDialog(
2020-10-15 18:48:32 +06:30
context, "discount.edit.delete.confirm", _deleteCargoType);
2020-10-15 15:49:02 +06:30
}
_deleteCargoType() async {
setState(() {
_isLoading = true;
});
try {
var discountModel = Provider.of<DiscountModel>(context, listen: false);
2021-09-10 12:02:08 +06:30
await discountModel.deleteDiscount(_discount);
2020-10-15 15:49:02 +06:30
Navigator.pop(context);
} catch (e) {
showMsgDialog(context, "Error", e.toString());
} finally {
setState(() {
_isLoading = false;
});
}
}
isDataChanged() {
if (_isNew) {
return _codeController.text != "" ||
_amountController.text != "" ||
customerName != "";
} else {
Discount _discount = Discount(
code: _codeController.text,
customerName: customerName,
customerId: customerId,
amount: double.parse(_amountController.text));
2021-09-10 12:02:08 +06:30
return widget.discount!.isChangedForEdit(_discount);
}
}
2020-06-26 16:04:40 +06:30
}