我有一个小部件,它呈现了一个ListView的TextFormFields来输入数据。然而,我想动态地改变其中一个文本字段的键盘类型。以下是该小部件的概述:
class CreateTodo extends StatefulWidget {
@override
_CreateTodoState createState() => _CreateTodoState();
}
class _CreateTodoState extends State<CreateTodo> {
final _formKey = GlobalKey<FormState>();
final double formWidth = 175.0;
final Map<String, Icon> itemsMap = {
'Heading': Icon(Icons.title),
'Description': Icon(Icons.edit),
'Date': Icon(Icons.calendar_today)
};
final items = ['Heading', 'Description', 'Date'];
_changeKeyboardType(entry) {
if (entry == "Description") {
return TextInputType.multiline;
}
return TextInputType.text;
}
ListView createListItems() {
return ListView.builder(
padding: EdgeInsets.only(
left: 50.0,
right: 50.0,
top: 20.0
),
scrollDirection: Axis.vertical,
itemCount: 3,
itemBuilder: (context, index) {
var currentHeading = this.items[index];
var currentIcon = this.itemsMap[currentHeading];
return Padding(
padding: EdgeInsets.only(bottom: 50.0),
child: new TextFormField(
validator: (input) {
if (input.isEmpty) {
return 'You must enter a ${currentHeading.toLowerCase()} for your to-do';
}
},
keyboardType: this._changeKeyboardType(currentHeading),
decoration: new InputDecoration(
prefixIcon: currentIcon,
labelText: currentHeading,
border: OutlineInputBorder(),
),
)
);
}
);
}
@override
Widget build(BuildContext context) {
return Form(
autovalidate: true,
child: Expanded(
child: (this.createListItems())
)
);
}
}
主要出问题的是这一行代码:keyboardType: this._changeKeyboardType(currentHeading)
... 我是否走错了方向?任何方向都会非常有帮助。我基本上想让描述的textFormField成为多行文本,以便用户可以更好地可视化他们的描述。
maxLines
属性,以允许它超过一行。当我需要多行输入时,我将其设置为null
,而当我需要单行输入时,我将其设置为 1。 - awaisbair