我在最大課程上,遇到了問題。我有 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的課程現在有點過時了,我通常自己分析問題,但這里一切似乎都合乎邏輯。有沒有人遇到過類似的問題?
uj5u.com熱心網友回復:
將 _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,
);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/459958.html
