1

有谁知道我如何让 CheckedPopupMenuItems 列表重新绘制(它们显然代表带有选项 PopupMenuButton 按下时显示的列表)当列表打开并在我选择更改 android 设备的语言环境/语言的那一刻可见?

现在,当我这样做时,屏幕上的所有内容都会重新绘制以反映语言的变化,除了打开的列表。一旦我关闭它并再次打开它就会重新粉刷。

感谢您的建议!

4

2 回答 2

0

您可能最终需要使用密钥。基本上,您的列表(即使信息正在更改)在技术上仍然与最初的小部件相同,因此您的程序不会在信息更改时重建列表。如果您的应用程序的其余小部件正在正确更新,则意味着您正在正确设置状态,因此添加键就足够了。

会是这样的,

MapSample({Key key, this.title}) : super(key: key);

有关键的更多信息,请查看:

https://api.flutter.dev/flutter/widgets/Widget/key.html

希望这可以帮助!

于 2020-02-06T17:57:29.143 回答
0

这是代码:

  Widget _buildAndroid(BuildContext context) {
    final width = MediaQuery.of(context).size.width;
    return PopupMenuButton<ScreenOrientation>(
        child: SizedBox(
            width: width,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.end,
                children: <Widget>[
                Icon(ScreenOrientation._getAndroidIconData(currentSelection), size: 30, color: iconColor),
                SizedBox(width: 20),
                Icon(Icons.expand_more, color: fontColor)
            ])),
        padding: EdgeInsets.zero,
        onSelected: (ScreenOrientation orientation) {
          onScreenOrientationChanged(orientation);
        },
        itemBuilder: (BuildContext context) =>
            ScreenOrientation.orientations
                .map((o) => CheckedPopupMenuItem<ScreenOrientation>(
                      key: UniqueKey(),
                      selectedColor: selectedColor,
                      child: Row(
                        children: <Widget>[
                          Icon(ScreenOrientation._getAndroidIconData(o), size: 30, color: iconColor),
                          SizedBox(width: 10),
                          Text(L10n.of(context).tr((m) =>
                            m['settings']['orientations'][o._name]),
                            style: TextStyle(color: fontColor),
                          )
                        ],
                      ),
                      value: o,
                      checked: o == currentSelection,
                    ))
                .toList());
  }

所以这里唯一有趣的一点是L10n.of(context).tr((m) => m['settings']['orientations'][o._name]它基本上从正确的 json 文件中检索本地化字符串,其名称是根据当前语言环境确定的。

于 2020-02-06T19:42:46.807 回答