0

I followed the guide on localization and it is working great. But I would like the user to be able to change to a locale in the app that is different from the local of the device. Can I somehow just set the locale for the app and have the entire app switch to using this locale?

I tried experimenting with "localeResolutionCallback" but without luck. Any ideas on how I can do this?

Thank you
Søren

Neigaard
  • 3,726
  • 9
  • 49
  • 85

1 Answers1

0

The is no need to use localeResolutionCallback.

You can simply specify the locale like this:

Locale _locale = Locale(Platform.localeName);
        @override
        Widget build(BuildContext context) {
          return MaterialApp(
            locale: _locale,
            home: const WelcomePage(),
          );
        }

Then you can provide the user a list of Locales to choose and create a function like this to update the app Locale:

setLocale(Locale locale) {
setState(() {
  _locale = locale;
});

}

To call setLocale(), you can use provider or findAncestorStateOfType to expose the initial Widget (it should be stateful)

Best regards

Bymolda
  • 49
  • 6
  • 1
    That was so much easier than I thought, thank you – Neigaard Jul 08 '22 at 14:37
  • I'm glad I could help. Also I recommend getting the list of possible locales from SupportedLocales so you are always sure the selected language is there. – Bymolda Jul 08 '22 at 14:49