【发布时间】:2021-08-05 23:06:35
【问题描述】:
我有这样的事情:
class ShareFiles extends ChangeNotifier {
int sharingMethod = 0;
askShareMethod(BuildContext context) {
return showPlatformDialog(
context: context,
builder: (context) => PlatformAlertDialog(
title: Text(translate("share_files_title")),
content: Column(mainAxisSize: MainAxisSize.min, children: <Widget>[
RadioListTile(
contentPadding: EdgeInsets.all(0),
value: 0,
groupValue: sharingMethod,
title: Text(translate('share_files_op0a')),
subtitle: Text(translate('share_files_op0b').toLowerCase()),
onChanged: (val) {
sharingMethod = 0;
notifyListeners();
},
selected: sharingMethod == 0 ? true : false,
),
RadioListTile(
contentPadding: EdgeInsets.all(0),
value: 1,
groupValue: sharingMethod,
title: Text(translate('share_files_op1a')),
subtitle: Text(translate('share_files_op1b').toLowerCase()),
onChanged: (val) {
sharingMethod = 1;
notifyListeners();
},
selected: sharingMethod == 1 ? true : false,
),
]),
actions: <Widget>[
TextButton(
child: PlatformText((translate('cancel'))),
onPressed: () {
Navigator.of(context).pop();
},
),
TextButton(
child: PlatformText((translate('send'))),
onPressed: () {
Navigator.of(context).pop();
},
),
],
),
);
}
}
我会这样称呼它:
await shareFiles.askShareMethod(context);
这会生成一个包含 2 个选项的对话框。当我选择一个时,尽管我正在使用 notifyListeners(),但小部件并未更新状态。
我做错了什么?
有更好的选择来更新这个值吗?我不需要在其他任何地方显示sharingMethod
【问题讨论】:
-
您使用的是 context.watch 还是您希望执行更新的 Consumer 小部件?
-
刚刚添加了一个 StatefulBuilder 就可以了
标签: flutter