【问题标题】:How can I use a localized string from GlobalMaterialLocalizations?如何使用 GlobalMaterialLocalizations 中的本地化字符串?
【发布时间】:2019-11-07 16:08:32
【问题描述】:

我想使用GlobalMaterialLocalizations class 中可用的预定义本地化字符串之一。我已将必要的点点滴滴添加到我的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
    },

    ...

我的自定义 LocalizationDelegate 工作正常。我只是不知道如何使用GlobalMaterialLocalizations 中的预定义字符串,因为没有GlobalMaterialLocalizations.of(BuildContext) 方法?

【问题讨论】:

    标签: flutter localization material-design flutter-layout


    【解决方案1】:

    原来我在错误的类中寻找.of(BuildContext) 方法。要实际使用字符串,应使用MaterialLocalizations 类。

    Text( MaterialLocalizations.of(context).okButtonLabel )
    

    希望它可以帮助其他人解决同样的问题。

    【讨论】:

      猜你喜欢
      • 2013-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-10
      • 2015-11-30
      • 1970-01-01
      相关资源
      最近更新 更多