String xml file in Flutter

后端 未结 7 1888
难免孤独
难免孤独 2021-02-01 15:18

In flutter string text are directly set to the TextField widget like:

new Text(\'Hello,  How are you?\')

Is correct way ? or we ca

7条回答
  •  太阳男子
    2021-02-01 15:30

    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)
    

    source


    Edit May '19:

    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

    You can create a separate json file for each language like so:

    string_en.json

    {
    "thanks": "Thanks."
    }
    

    string_nl.json

    {    
    "thanks": "Dankjewel."
    }
    

    And then use this to access it

    S.of(context).thanks;
    

    It will know which language to choose based on your phone's default language.

提交回复
热议问题