我遇到了开关颤振的问题



我在最大的课程上,遇到了一个问题。我有4个开关是由builder创建的:

//here are variables used later to pass data into builder
bool _glutenFree = false;
bool _vegetarian = false;
bool _vegan = false;
bool _lactoseFree = false;
Widget _buildSwitchListTile(
String title,
String description,
bool currentValue,
Function(bool) updateValue,

) {
return SwitchListTile(
title: Text(title),
value: _glutenFree,
subtitle: Text(description),
onChanged: updateValue,
);
}

当我尝试点击第2,第3,第4个开关时,什么都没有发生。但是当我尝试点击第一个按钮时,四个按钮都切换了。

下面是_buildSwitchListTile的用法:
_buildSwitchListTile(
'Gluten-free',
'Only include gluten-free meals.',
_glutenFree,
(newValue) {
setState(
() {
_glutenFree = newValue;
},
);
},
),
_buildSwitchListTile(
'Lactose-free',
'Only include Lactose-free meals.',
_lactoseFree,
(newValue) {
setState(
() {
_lactoseFree = newValue;
},
);
},
),
_buildSwitchListTile(
'Vegetarian',
'Only include Vegetarian meals.',
_vegetarian,
(newValue) {
setState(
() {
_vegetarian = newValue;
},
);
},
),
_buildSwitchListTile(
'Vegan',
'Only include Vegan meals.',
_vegan,
(newValue) {
setState(
() {
_vegan = newValue;
},
);
},
),

我知道Max的课程现在有点过时了,我通常自己分析问题,但这里的一切似乎都合乎逻辑。有人遇到过类似的问题吗?

将_glutenFree更改为currentValue

//here are variables used later to pass data into builder
bool _glutenFree = false;
bool _vegetarian = false;
bool _vegan = false;
bool _lactoseFree = false;
Widget _buildSwitchListTile(
String title,
String description,
bool currentValue,
Function(bool) updateValue,

) {
return SwitchListTile(
title: Text(title),
value: currentValue,    // change _glutenFree to currentValue
subtitle: Text(description),
onChanged: updateValue,
);
}

相关内容

  • 没有找到相关文章

最新更新