Victor Camargo
Victor Camargo

Reputation: 364

Variable hintText TextField

I'm using intl for translations on my app, and I'm facing an problem when I use the translation on hintText, I got this error:

Arguments of a constant creation must be constant expressions

Example code:

new TextField(
  decoration: const InputDecoration(
    contentPadding: EdgeInsets.only(top: 16.0),
    hintText: AppLocalizations.of(context).search_message, // Variable
      border: InputBorder.none,
    ),
    keyboardType: TextInputType.text,
    textInputAction: TextInputAction.search,
    style: new TextStyle(
    fontSize: 16.0,
    color: Colors.black
  )
)

I understand that the error is caused because I am using AppLocalizations.of(context).search_message, (Which is variable) but the question is: How do I translate this hintText?

Upvotes: 3

Views: 1752

Answers (1)

Yamin
Yamin

Reputation: 3008

The InputDecoration has const prefix which is for creating a const instance. so, the data within it should be constant(available during compiling).

In order to solve this issue, just change the const keyword to new,This should work fine:

new TextField(
  decoration: new InputDecoration(
    contentPadding: EdgeInsets.only(top: 16.0),
    hintText: AppLocalizations.of(context).search_message, // Variable
      border: InputBorder.none,
    ),
    keyboardType: TextInputType.text,
    textInputAction: TextInputAction.search,
    style: new TextStyle(
    fontSize: 16.0,
    color: Colors.black
  )
)

Upvotes: 6

Related Questions