【问题标题】:Flutter: update widget status with notifyListeners()Flutter:使用 notifyListeners() 更新小部件状态
【发布时间】: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


【解决方案1】:
StatefulBuilder(builder: (context, setState) {
          return 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) {
                  setState(() {
                    sharingMethod = 0;
                  });
                },
                selected: sharingMethod == 0 ? true : false,
              ),
              RadioListTile(
                contentPadding: EdgeInsets.all(0),
                value: 1,
                groupValue: sharingMethod,
                title: Text(translate('share_files_op1a')),
                onChanged: (val) {
                  setState(() {
                    sharingMethod = 1;
                  });
                },
                selected: sharingMethod == 1 ? true : false,
              ),
            ],
          );
        }),

【讨论】:

    猜你喜欢
    • 2018-09-01
    • 2020-10-20
    • 1970-01-01
    • 2020-11-28
    • 1970-01-01
    • 2018-07-28
    • 2020-03-18
    • 1970-01-01
    • 2021-12-23
    相关资源
    最近更新 更多