Flutter中的String xml文件

Mag*_*ian 21 resources android dart flutter

嗨,我是新开发的Android开发者.在flutter字符串文本中直接设置为文本字段小部件.

(即)

new Text('Hello,  How are you?')
Run Code Online (Sandbox Code Playgroud)

是正确的方法吗?或者我们可以将所有字符串保存在一个文件中并使用它.

(即)

<string name="name_hint">Hello, How are you?</string>
Run Code Online (Sandbox Code Playgroud)

有可能吗?.任何帮助将不胜感激,谢谢你提前.

Spe*_*eed 19

Flutter目前没有专门的字符串资源系统.目前,最佳做法是将复制文本作为静态字段保存在类中,并从那里访问它们.例如:

class Strings {
  static const String welcomeMessage = "Welcome To Flutter";
}
Run Code Online (Sandbox Code Playgroud)

然后在您的代码中,您可以访问您的字符串:

Text(Strings.welcomeMessage)
Run Code Online (Sandbox Code Playgroud)

资源

  • 我建议使每个字符串`静态常量` (2认同)

Cop*_*oad 5

在此处输入图片说明

对于那些不想使用任何第三方插件的人,这是您可以使用的方法。

  1. 创建一个文件夹stringsasset。将您的语言文件放入其中。

    asset
      strings
      - en.json // for english 
      - ru.json  // for russian
    
    Run Code Online (Sandbox Code Playgroud)
  2. 现在,在中en.json,例如,输入您的字符串。

    {
      "text1": "Hello",
      "text2": "World"
    }
    
    Run Code Online (Sandbox Code Playgroud)

    同样,在中ru.json

    {
      "text1": "??????",
      "text2": "???"
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 将此添加到pubspec.yaml文件(注意空格)

    flutter:
    
      uses-material-design: true
    
      assets:
        - assets/json/en.json
        - assets/json/ru.json
    
    Run Code Online (Sandbox Code Playgroud)
  4. 现在,您都可以在应用程序中使用这些字符串。这是示例代码,其中AppBar显示了翻译后的文本。

    void main() {
      runApp(
        MaterialApp(
          locale: Locale("ru"), // switch between en and ru to see effect
          localizationsDelegates: [const DemoLocalizationsDelegate(), GlobalMaterialLocalizations.delegate, GlobalWidgetsLocalizations.delegate],
          supportedLocales: [const Locale('en', ''), const Locale('ru', '')],
          home: HomePage(),
        ),
      );
    }
    
    class HomePage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(title: Text(DemoLocalizations.of(context).getText("text2") ?? "Error")),
        );
      }
    }
    
    // this class is used for localizations
    class DemoLocalizations {
      static DemoLocalizations of(BuildContext context) {
        return Localizations.of<DemoLocalizations>(context, DemoLocalizations);
      }
    
      String getText(String key) => language[key];
    }
    
    Map<String, dynamic> language;
    
    class DemoLocalizationsDelegate extends LocalizationsDelegate<DemoLocalizations> {
      const DemoLocalizationsDelegate();
    
      @override
      bool isSupported(Locale locale) => ['en', 'ru'].contains(locale.languageCode);
    
      @override
      Future<DemoLocalizations> load(Locale locale) async {
        String string = await rootBundle.loadString("assets/json/${locale.languageCode}.json");
        language = json.decode(string);
        return SynchronousFuture<DemoLocalizations>(DemoLocalizations());
      }
    
      @override
      bool shouldReload(DemoLocalizationsDelegate old) => false;
    }
    
    Run Code Online (Sandbox Code Playgroud)