【发布时间】:2022-09-28 03:12:21
【问题描述】:
下面是我尝试过的代码。我还附上了创建视频Click here to see Video。 我的问题是,在选择产品和相关方案并转到下一页然后返回该页面后,该方案消失了,这是不应该发生的。我有4层导航,当导航到第二级时,我可以选择产品,如果产品有方案,则弹出弹出窗口,选择该方案时该方案在第二页中显示,但是如果我转到第三页,则返回第2页只剩下产品,相关方案产品栏消失。
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();
}
}
-
你能提供控制器的代码,还要检查控制器是否是永久的,因为一旦你离开页面控制器就会被破坏,导致值消失。另外使用 .obs 作为可观察列表
-
我添加了控制器代码@C.MTalha
-
从控制器中删除 final 关键字,将您的 GetxController 标记为永久
-
另一种解决方案是使用 RestorationMixin,因为您需要传递您想要保留在页面存储桶中的值,这将导致您的页面从之前的状态恢复 api.flutter.dev/flutter/widgets/RestorationMixin-mixin.html
标签: flutter dart user-interface