下面是我尝试过的代码。我也附上了视频,点击这里看视频。我的问题是,在选择产品和相关方案,去下一页,然后回到页面方案消失,不应该发生。我有4层导航,当导航到第二层时,我可以选择产品,如果产品有方案,就会弹出,当选择方案时,方案显示在第二页,但如果我转到第三页,然后返回到第二页,只剩下产品,相关的方案产品栏就会消失。
Obx(() {
return ListView.builder(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemCount: salesController.currentOrderItems.length,
itemBuilder: (context, index) {
if (schemeMsgController.text.isNotEmpty) {
//Scheme Layout Builder
schemeTitleController.text =
salesController.currentOrderItems[index].title;
schemeNetPriceController.text =
salesController.currentOrderItems[index].netRate;
String quantity =
salesController.currentOrderItems[index].qty;
double schemeRawQuantity = (double.parse(quantity) / 5);
if (schemeRawQuantity > 50) {}
int schemeQuantity = schemeRawQuantity.toInt();
schemeQuantityController.text = schemeQuantity.toString();
//return Text(salesController.currentOrderItems[index].title);
return Column(
children: [
CreateOrderTiles(
orders: salesController.currentOrderItems[index]),
// Scheme UI
Card(
color: Colors.white.withOpacity(0.1),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
//schemeTitleController.text,
salesController.currentProductTitle,
textAlign: TextAlign.start,
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
Text(schemeQuantityController.text +
" pcs"),
Text("Rs: " +
schemeNetPriceController.text),
Text("0.00"),
],
),
],
),
),
),
],
);
} else {
return CreateOrderTiles(
orders: salesController.currentOrderItems[index]);
}
});
})
下面是控制器代码:
class SalesController extends GetxController {
var isLoading = false.obs;
var orderList = <OrdersModel>[].obs;
var ordersItems = <OrdersModel>[].obs;
var currentOrder;
var availableStock = 0.0.obs;
var currentProductID = '';
var currentProductTitle = '';
var currentProductModel = '';
var grandTotal = 0.0.obs;
var totalAmount = 0.0.obs;
var currentOrderItems = <OrderItemModel>[].obs;
final TextEditingController productTitleController =
TextEditingController(text: '0');
final TextEditingController qtyController = TextEditingController(text: '0');
final TextEditingController qtySchemeController =
TextEditingController(text: '0');
final TextEditingController mrpController = TextEditingController(text: '0');
final TextEditingController discountController =
TextEditingController(text: '0');
final TextEditingController netRateController =
TextEditingController(text: '0');
final TextEditingController amountController =
TextEditingController(text: '0');
void fetchUserOrders() async {
isLoading(true); // isLoading.value = true;
try {
var orders = await SalesService.getUserOrder(12, 66, '');
if (orders != null) {
orderList.value = orders;
}
} finally {
isLoading(false);
}
isLoading(false);
}
void resetFormFields() {
productTitleController.text = "";
qtyController.text = '0';
mrpController.text = '0';
discountController.text = '0';
netRateController.text = '0';
amountController.text = '0';
}
double getPercentValue(String myDiscount) {
if (myDiscount == '') return 0.0;
var pos = myDiscount.indexOf('%');
if (pos < 0) {
return double.parse(myDiscount);
} else {
return (double.parse(myDiscount.replaceAll('%', '')) / 100) *
double.parse(mrpController.text);
}
}
void addItemToOrder(bool isScheme) {
// print(currentProductID);
OrderItemModel orderItem = new OrderItemModel(
key: Random().toString(),
productID: currentProductID,
image: Image.network(''),
title: currentProductTitle,
qty: qtyController.text,
mrp: amountController.text,
discount: discountController.text,
netRate: netRateController.text,
amount: amountController.text,
isScheme: isScheme,
ofSchemekey: '',
modelNumber: currentProductModel,
);
currentOrderItems.value.add(orderItem);
double totalGross = 0.0;
currentOrderItems.refresh();
for (var i = 0; i < currentOrderItems.value.length; i++) {
totalGross += double.parse(currentOrderItems.value[i].amount);
}
print('total amount');
print(totalGross);
grandTotal.value = totalGross;
totalAmount.value = totalGross;
}
void calculateScheme() {
double data = double.parse(qtyController.text);
int qtyScheme = data ~/ 5;
qtySchemeController.text = qtyScheme.toString();
}
void updateProductRowCalc() {
calculateScheme();
double total =
double.parse(qtyController.text) * double.parse(mrpController.text);
double discount = getPercentValue(discountController.text);
double netRate = double.parse(mrpController.text) - discount;
double amount = double.parse(qtyController.text) * netRate;
netRateController.text = netRate.toString();
amountController.text = amount.toString();
}
}
SalesController saleController = Get.put(SalesController(), permanent : true);
我认为问题在于状态,一旦你推到另一个屏幕,getx库认为你的控制器是不必要的,销毁它,为了保留值,使用一个永久标志来保留值,只要你需要,一旦你的控制器完成了它的目的,确保销毁它,否则,它会增加不必要的头到应用程序的性能。
从TextEditingControllers中删除final