How can i make const variables with flutter dart

Viewed 31

I need to send a value as const, how do I do that?

EmojiPicker(
          textEditingController: ..,
          config: Config(
            showRecentsTab: true,
            noRecents: Text( // I need send const
              Localization
                  .of(context)
                  .noRecent,
              style: TextStyle(
                  fontSize: 14,
                  color: Theme
                      .of(context)
                      .brightness == Brightness.dark
                      ? Colors.white
                      : Colors.black),
              textAlign: TextAlign.center,
            )
          ))

If I don't send the text widget as const it is constantly being refreshed but how can I send it as const in a multilingual application?

1 Answers

You can only make something const that doesn't have variables inside it.

Text("myText"); // can be const

Text(myString); // cannot be const

Widgets are usually only refreshed if you call setState(() {}) or if you use a provider only when its values change.

If you don't want parts of the Widget tree to be rebuilt you need to extract it into a seperate stateless/stateful widget that has a const constructor and determines by itself when it needs to be rebuilt.

Related