我有一个带有项目构建器的小部件。在项目构建器中,我要为每个项目创建下拉菜单。
对于此value
的DropdownMenuItem
属性,我使用SaleRef _selectedRef;
问题是,当我在itemBuilder中声明此SaleRef _selectedRef;
时,选择一个项目后该值不变。
当我在此小部件外声明它但在类中时,它会更改每个下拉列表的值
如何在每个下拉列表中选择单独的值?
这是用于创建项目的代码
ListView.separated(
itemBuilder: (context, index) {
return Row(
children: <Widget>[
Container(
height: 40,
width: MediaQuery.of(context).size.width * 1 / 5,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: new BorderRadius.circular(25.0),
border: Border.all(
color: Colors.grey,
style: BorderStyle.solid,
width: 0.80),
),
child: DropdownButton<SaleRef>(
hint: Text(" Select Ref"),
value: _selectedRef,
onChanged: (SaleRef value) {
setState(() {
_selectedRef = value;
print(_selectedRef.refID);
});
},
items: _filteredSaleRef.map((SaleRef ref) {
return DropdownMenuItem<SaleRef>(
value: ref,
child: Row(
children: <Widget>[
Text(
" " + ref.refName,
style: TextStyle(color: Colors.black),
),
],
),
);
}).toList(),
),
),
],
);
},
separatorBuilder: (context, index) {
return Divider(height: 16);
},
itemCount: filteredShopItem.length,
)
当我这样宣布时
class _AssignRefPageState extends State<AssignRefPage>
with {
SaleRef _selectedRef;
这是在选择值后发生
当我这样在构建器中这样声明时
itemBuilder: (context, index) {
SaleRef _selectedRef;
return Row(
这是我得到的,即使我选择了一个也总是提示。>
我有一个带有项目构建器的小部件。在项目构建器中,我要为每个项目创建下拉菜单。对于此DropdownMenuItem的value属性,我使用SaleRef _selectedRef;问题是当...
问题是,您为每个下拉按钮使用相同的变量,并且当您尝试在ListView itemBuilder
中声明它并调用setState时,将构建整个Widget,并将其再次设置为null
正如@ Josteve-Adekanbi所说的,问题出在使用相同的变量