222 lines
6.6 KiB
Dart
222 lines
6.6 KiB
Dart
import 'package:fcs/domain/entities/discount.dart';
|
|
import 'package:fcs/helpers/theme.dart';
|
|
import 'package:fcs/pages/discount/model/discount_model.dart';
|
|
import 'package:fcs/pages/main/util.dart';
|
|
import 'package:fcs/pages/user_search/user_search.dart';
|
|
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';
|
|
import 'package:fcs/pages/widgets/progress.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_vector_icons/flutter_vector_icons.dart';
|
|
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
|
import 'package:provider/provider.dart';
|
|
|
|
class DiscountEditor extends StatefulWidget {
|
|
final Discount? discount;
|
|
|
|
const DiscountEditor({super.key, this.discount});
|
|
@override
|
|
_DiscountEditorState createState() => _DiscountEditorState();
|
|
}
|
|
|
|
class _DiscountEditorState extends State<DiscountEditor> {
|
|
bool _isLoading = false;
|
|
Discount _discount = Discount();
|
|
final TextEditingController _codeController = TextEditingController();
|
|
final TextEditingController _amountController = TextEditingController();
|
|
final TextEditingController _statusController = TextEditingController();
|
|
|
|
bool _isNew = false;
|
|
String customerName = '';
|
|
String customerId = '';
|
|
final _discountFormKey = GlobalKey<FormState>();
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
if (widget.discount != null) {
|
|
_discount = widget.discount!;
|
|
_codeController.text = _discount.code ?? "";
|
|
_amountController.text = _discount.amount.toStringAsFixed(2);
|
|
_statusController.text = _discount.status ?? '';
|
|
customerName = _discount.customerName ?? "";
|
|
customerId = _discount.customerId ?? "";
|
|
} else {
|
|
_isNew = true;
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final codeBox = InputText(
|
|
labelTextKey: 'discount.code',
|
|
iconData: Ionicons.code_slash,
|
|
controller: _codeController,
|
|
autovalidateMode: AutovalidateMode.onUserInteraction,
|
|
validator: (value) {
|
|
if (value == null || value.isEmpty) {
|
|
return "Please insert code";
|
|
}
|
|
return null;
|
|
},
|
|
);
|
|
|
|
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;
|
|
},
|
|
);
|
|
|
|
final statusBox = DisplayText(
|
|
text: _statusController.text,
|
|
labelTextKey: "discount.status",
|
|
iconData: Icons.av_timer,
|
|
);
|
|
|
|
final customerBox = Row(
|
|
children: <Widget>[
|
|
Expanded(
|
|
child: DisplayText(
|
|
text: customerName,
|
|
labelTextKey: "discount.name",
|
|
iconData: Feather.user,
|
|
)),
|
|
IconButton(
|
|
icon: Icon(Icons.search, color: primaryColor),
|
|
onPressed: () => searchUser(context, onUserSelect: (u) {
|
|
setState(() {
|
|
customerId = u.id ?? "";
|
|
customerName = u.name ?? "";
|
|
});
|
|
}, popPage: true)),
|
|
],
|
|
);
|
|
|
|
return LocalProgress(
|
|
inAsyncCall: _isLoading,
|
|
child: Scaffold(
|
|
backgroundColor: Colors.white,
|
|
appBar: LocalAppBar(
|
|
labelKey: "discount.form",
|
|
backgroundColor: Colors.white,
|
|
arrowColor: primaryColor,
|
|
labelColor: primaryColor,
|
|
actions: [
|
|
_isNew
|
|
? const SizedBox()
|
|
: IconButton(
|
|
icon: Icon(Icons.delete, color: primaryColor),
|
|
onPressed: _delete)
|
|
],
|
|
),
|
|
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: 15),
|
|
customerBox,
|
|
widget.discount == null
|
|
? Container()
|
|
: Container(
|
|
padding: EdgeInsets.only(top: 10),
|
|
child: statusBox,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
fcsButton(context, getLocalString(context, "btn.save"),
|
|
callack: _save),
|
|
SizedBox(
|
|
height: 30,
|
|
)
|
|
],
|
|
),
|
|
),
|
|
)),
|
|
);
|
|
}
|
|
|
|
_save() async {
|
|
if (!_discountFormKey.currentState!.validate()) {
|
|
return null;
|
|
}
|
|
setState(() {
|
|
_isLoading = true;
|
|
});
|
|
try {
|
|
DiscountModel discountModel =
|
|
Provider.of<DiscountModel>(context, listen: false);
|
|
Discount _discount = Discount(
|
|
code: _codeController.text,
|
|
customerName: customerName,
|
|
customerId: customerId,
|
|
amount: double.parse(_amountController.text));
|
|
if (_isNew) {
|
|
await discountModel.addDiscount(_discount);
|
|
} else {
|
|
_discount.id = this._discount.id;
|
|
await discountModel.updateDiscount(_discount);
|
|
}
|
|
Navigator.pop(context);
|
|
} catch (e) {
|
|
showMsgDialog(context, "Error", e.toString());
|
|
} finally {
|
|
setState(() {
|
|
_isLoading = false;
|
|
});
|
|
}
|
|
}
|
|
|
|
_delete() {
|
|
showConfirmDialog(
|
|
context, "discount.edit.delete.confirm", _deleteCargoType);
|
|
}
|
|
|
|
_deleteCargoType() async {
|
|
setState(() {
|
|
_isLoading = true;
|
|
});
|
|
try {
|
|
var discountModel = Provider.of<DiscountModel>(context, listen: false);
|
|
await discountModel.deleteDiscount(_discount);
|
|
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));
|
|
return widget.discount!.isChangedForEdit(_discount);
|
|
}
|
|
}
|
|
}
|