我写得很忙。我想更改字符串变量的状态。设置字符串变量的状态后,ModalRoute PopUpMenu不显示更改后的变量。如果我关闭ModalRoute PopUpMenu并再次打开它,则可以看到更改后的变量。
我试图弹出上下文,但是我想在PopUpMenu上进行更改。我有自己的“覆盖”小部件。
class MyOverlay extends ModalRoute {
...
}
// this is my main.dart:
List<String> categories = ['please', 'help', 'me'];
String _selectedCategory = 'category';
// this is where the PopUpMenu starts
floatingActionButton: FloatingActionButton(
child: ...,
onPressed: () {
_showPopup(context, _popupBody(), 'Add');
},
),
_showPopup(BuildContext context, Widget widget, String title, {BuildContext popupContext}) {
Navigator.push(
context,
MyOverlay(
...
onPressed: () {
try {
Navigator.pop(context); //close the popup
} catch (e) {
print(e);
}
},
...
body: widget,
) ...
);
}
Widget _popupBody() {
...
PopupMenuButton<String>(
// HERE IS THE PROBLEM THIS SHOULD CHANGE WHEN I SELECT
child: Text('$_selectedCategory'),
itemBuilder: (BuildContext context) {
return categories.map((String choice) {
return PopupMenuItem<String>(
value: choice,
child: Text(choice),
);
}).toList();
},
onSelected: _selectCategory,
),
...
}
void _selectCategory(String category) {
setState(() => this._selectedCategory = category);
}
如果我选择PopupMenuItem,则文本小部件不会更改。
答案 0 :(得分:1)
我有同样的问题,我暂时使用changedExternalState()修复了;强制进行重建,但我认为这可能不是最佳选择。
示例:
CheckboxListTile(
value: _checkboxValue,
title: Text(phone),
onChanged: (value){
_checkboxValue = value;
//Fix
changedExternalState();
},
)