Magnus
Magnus

Reputation: 18728

How can I use a localized string from GlobalMaterialLocalizations?

I want to use one of the predefined localized strings available in the GlobalMaterialLocalizations class. I have added the necessary bits and pieces to my MaterialApp

MaterialApp(
    localizationsDelegates: [
        const LocalizationDelegate(),
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
    ],
    supportedLocales: [
        const Locale('en', ''),
        const Locale('sv', ''),
    ],
    localeResolutionCallback:(Locale locale, Iterable<Locale> supportedLocales) {
         return locale; // Return a different locale if the user choose another language in the settings
    },

    ...

and my custom LocalizationDelegate is working fine. I just can't figure out how to use the predefined strings in GlobalMaterialLocalizations, since there is no GlobalMaterialLocalizations.of(BuildContext) method?

Upvotes: 2

Views: 335

Answers (1)

Magnus
Magnus

Reputation: 18728

Turns out I was looking for the .of(BuildContext) method in the wrong class. To actually use the strings, the MaterialLocalizations class should be used.

Text( MaterialLocalizations.of(context).okButtonLabel )

Hope it might help someone else struggling with the same problem.

Upvotes: 3

Related Questions