1

Does anybody know how do I get the list of CheckedPopupMenuItems to repaint (they obviously represent the list with options PopupMenuButton shows when pressed) when the list opened and visible at the very moment when I choose to change the locale/language of the android device?

For now when I do this everything on the screen gets repainted to reflect the change of the language, except the opened list. It gets repainted once I close it and open again.

Thanks for your suggestions!

hicnar
  • 163
  • 3
  • 9

2 Answers2

0

You'll likely end up needing to use Keys. Basically your list (Even though information is being changed) is still technically the same widget as it was originally, and so your program doesn't rebuild the list when the information changes. If the rest of your app's widgets are being updated properly, that means you are setting the state properly, so adding the keys should be sufficient.

It'd be something like,

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

for more information on keys take a look at:

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

Hope this helps!

ArthurEKing
  • 158
  • 2
  • 16
  • Yep I was thinking the same and tried it already, I mean I tried to add a UniqueKey to every CheckedPopupMenuItem but it does not seem to be working – hicnar Feb 06 '20 at 18:49
  • Think you could try to post either a Code-Snippet or a Minimal-Reproduceable Example? – ArthurEKing Feb 06 '20 at 19:10
0

Here's the code:

  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());
  }

So the only interesting bit here is the L10n.of(context).tr((m) => m['settings']['orientations'][o._name] which essentially retrieves a localized string from the right json file whose name is determined based on the current locale.

hicnar
  • 163
  • 3
  • 9