我正在嘗試創建一個TextInput用于自定義的新小部件,TextFormField但我無法自定義labelText。我需要labelText為我 發送建構式TextFormField并顯示這個字串。
class TextInput extends StatelessWidget {
final TextEditingController textControler;
final String textLabelHint;
const TextInput({Key? key, required this.textControler,required this.textLabelHint}) : super(key: key);
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(top: 10.0),
child: TextFormField(
controller: textControler,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: textLabelHint,
),
),
);
}
}
但我有問題:
labelText: textLabelHint, //Invalid constant value.
uj5u.com熱心網友回復:
出現此錯誤是因為textLabelHint最終類屬性(不是 const)可能會根據建構式值而更改。但是,您嘗試傳遞此值的位置是InputDecoration您標記為const. 因此,錯誤表明了這一點。
要解決此問題,請const洗掉InputDecoration:
class TextInput extends StatelessWidget {
final TextEditingController textControler;
final String textLabelHint;
const TextInput({Key? key, required this.textControler,required this.textLabelHint}) : super(key: key);
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(top: 10.0),
child: TextFormField(
controller: textControler,
decoration: InputDecoration( // <-- Notice the removed const
border: OutlineInputBorder(),
labelText: textLabelHint,
),
),
);
uj5u.com熱心網友回復:
const您需要從中 洗掉decoration: const InputDecoration(...),因為textLabelHint它不是一個const值:
class TextInput extends StatelessWidget {
final TextEditingController textControler;
final String textLabelHint;
const TextInput(
{Key? key, required this.textControler, required this.textLabelHint})
: super(key: key);
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(top: 10.0),
child: TextFormField(
controller: textControler,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: textLabelHint,
),
),
);
}
}
uj5u.com熱心網友回復:
嘗試這個:
class TextInput extends StatelessWidget {
final TextEditingController textControler;
final String textLabelHint;
const TextInput({Key? key, required this.textControler,required this.textLabelHint}) : super(key: key);
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(top: 10.0),
child: TextFormField(
controller: textControler,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: textLabelHint,
),
),
);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/439357.html
上一篇:如何使用Azure.Data.Tables列出表(按前綴)?
下一篇:在顫動中格式化所有文本欄位
