我在如何实现一些很酷的东西方面遇到了一些挑战。我有一个动态的下拉菜单项。 dropdown 的端点返回除 id 和 value 之外的其他属性,即将显示在 dropdownmenuItems 中的名称。所以我现在的挑战是:如何根据所选下拉项填充其他文本表单字段,例如数量和单价,因此这些属性的最后更新值位于下拉端点的响应 json 中。
这是返回下拉列表的函数:
Future _scanQR(stockoid, context) async {
try {
// This take the barcode Id and parse it to the addItem function to populate the add dialog
// String qrResult = await BarcodeScanner.scan();
String qrResult = 'result'; - // this is just to bypass the barcode scan process
// ignore: avoid_print
print('$qrResult poi');
// Make a call to the endpoints that will get the products with a response what contains
products attributes
// print('Anchor ${widget.stockData.anchorOid}, Season ${widget.stockData.seasonOid}, State ${widget.stockData.stateOid}',);
_prefs = await SharedPreferences.getInstance();
var _productService = ProductService();
var result = await _productService.getAllProducts(
widget.stockData.seasonOid,
_prefs.getInt('providerOid'),
widget.stockData.anchorOid);
var products = await json.decode(result.body);
print("Stock Item Products");
print(products); - // this results contain more than name and oid other attributes can be seen in the json response below
products.forEach((product) {
setState(() {
_productLists.add(DropdownMenuItem(
child: Text(product['name']),
value: product['oid'],
));
});
});
setState(() {
_isLoading = false;
});
addItem(qrResult, stockoid, context);
setState(() {
result = qrResult;
});
} on PlatformException catch (ex) {
if (ex.code == BarcodeScanner.CameraAccessDenied) {
setState(() {
result = "Camera permission was denied";
});
} else {
setState(() {
result = "Unknown Error $ex";
});
}
} on FormatException {
setState(() {
result = "You pressed the back button before scanning anything";
});
} catch (ex) {
setState(() {
result = "Unknown Error $ex";
});
}
}
这是表单域:
SingleChildScrollView(
child: Column(
children: [
Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container(
child: Text(
"Add Stock Items",
style: style.PoppinsSemiBold(
color: Colors.black,
fontSize: size.convert(context, 20),
),
),
),
BoxIcon(
CustomIcon: Icon(
Icons.cancel,
color: Colors.blueGrey,
),
ontap: () {
Navigator.pop(context);
},
)
],
),
),
SpacebetweenTextField(
spacing: 20,
),
Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
child: Text(
"Enter Date",
style: style.PoppinsSemiBold(
fontSize: size.convert(context, 14),
color: Colors.black,
),
),
),
SpacebetweenTextField(
spacing: 5,
),
DropdownButtonFormField(
value: _selectedProduct,
items: _productLists,
hint: const Text('Select Product'),
onChanged: (value) {
setState(() {
_selectedProduct = value;
});
},
),
SpacebetweenTextField(
spacing: 5,
),
TextFormField(
controller: quantity,
decoration: InputDecoration(
border: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.red, //this has no effect
),
borderRadius: BorderRadius.circular(10.0),
),
hintText: "0.00",
),
),
TextFormField(
controller: price,
decoration: InputDecoration(
border: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.red, //this has no effect
),
borderRadius: BorderRadius.circular(10.0),
),
hintText: "NGN 0.00",
),
),
],
),
),
SpacebetweenTextField(
spacing: 40,
),
Container(
child: filledButton(
leftWidget: Icon(Icons.save, color: Colors.white),
txt: "Submit",
color1: Color(0xff1b7ec2),
onTap: () {
setState(() {
_isLoading = true;
});
_addStock(widget.anchorOid, selectedDate, widget.seasonOid,
widget.stateOid, context);
Navigator.pop(context);
},
),
)
],
),
));
响应结果:
Response body
Download
[
{
"oid": 2,
"name": "Certified Seed (Per Kg)",
"unitPrice": 400,
"quantity": 1,
"total": 400,
"isProduct": true,
"boldHeader": false
},
{
"oid": 3,
"name": "Empty Bags",
"unitPrice": 150,
"quantity": 1,
"total": 150,
"isProduct": true,
"boldHeader": false
},
{
"oid": 4,
"name": "Fertilizer UREA",
"unitPrice": 8000,
"quantity": 1,
"total": 8000,
"isProduct": true,
"boldHeader": false
},
{
"oid": 5,
"name": "Fertilzer NPK 15-15-15",
"unitPrice": 7500,
"quantity": 1,
"total": 7500,
"isProduct": true,
"boldHeader": false
}
]