Dart 变量hintText TextField
我在我的应用程序上使用intl进行翻译,当我在hintText上使用翻译时遇到问题,我遇到了以下错误: 常量创建的参数必须是常量表达式 示例代码:Dart 变量hintText TextField,dart,flutter,Dart,Flutter,我在我的应用程序上使用intl进行翻译,当我在hintText上使用翻译时遇到问题,我遇到了以下错误: 常量创建的参数必须是常量表达式 示例代码: new TextField( decoration: const InputDecoration( contentPadding: EdgeInsets.only(top: 16.0), hintText: AppLocalizations.of(context).search_message, // Variable
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
)
)
我知道错误是因为我使用的是AppLocalizations.of(context).search\u message(这是变量),但问题是:如何翻译此hintText?InputDecoration有const前缀,用于创建const实例。因此,其中的数据应该是常量(在编译期间可用) 为了解决这个问题,只需将const关键字更改为new,这应该可以正常工作:
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
)
)
哦,我不知道输入装饰是否是常量,hintText也必须是常量。非常感谢。