Flutter中的字符串xml文件 [英] String xml file in Flutter
问题描述
在颤动字符串中,文本直接设置为 TextField
小部件,例如:
In flutter string text are directly set to the TextField
widget like:
new Text('Hello, How are you?')
正确的方法吗?或者我们可以将所有字符串保存在一个文件中,并像这样使用它:
Is correct way ? or we can maintain all string in one file and use it like:
<string name="name_hint">Hello, How are you?</string>
有可能吗?
推荐答案
Flutter目前没有专门的类似于资源的字符串系统。目前,最佳做法是将复制文本作为静态字段放在一个类中,然后从那里进行访问。例如:
Flutter currently doesn’t have a dedicated resources-like system for strings. At the moment, the best practice is to hold your copy text in a class as static fields and accessing them from there. For example:
class Strings {
static const String welcomeMessage = "Welcome To Flutter";
}
然后在代码中,您可以这样访问字符串:
Then in your code, you can access your strings as such:
Text(Strings.welcomeMessage)
编辑19年5月:
现在有此程序包可以您可以使用字符串创建json文件。它将允许您为复数,性别和语言等创建字符串。
There's now this package that allows you to create json files with your Strings. It will allow you to create Strings for plurals, genders and languages etc
您可以为每种语言创建一个单独的json文件,如下所示:
You can create a separate json file for each language like so:
string_en.json
{
"thanks": "Thanks."
}
string_nl.json
{
"thanks": "Dankjewel."
}
然后使用它来访问它
S.of(context).thanks;
它将根据手机的默认语言知道选择哪种语言。
It will know which language to choose based on your phone's default language.
这篇关于Flutter中的字符串xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!