dart语言是否可以通过TextFormField
确定最大输入长度?
答案 0 :(得分:11)
使用inputFormatters
属性。以下代码将textFormField的长度限制为42:
new TextFormField(
inputFormatters: [
new LengthLimitingTextInputFormatter(42),
],
);
更新:首先导入包。 import 'package:flutter/services.dart';
答案 1 :(得分:2)
使用maxLength
TextFormField(
controller: _deskripsiController,
onFieldSubmitted: (String value){
setState(() {
_deskripsiController.text = value;
});
},
decoration: const InputDecoration(
border: const UnderlineInputBorder(),
labelText: "Deskripsi",
),
maxLength: 8,
)
答案 2 :(得分:0)
最好的方法是inputFormatters选项:
inputFormatters: [
new LengthLimitingTextInputFormatter(11),// for mobile
],
答案 3 :(得分:-1)
在添加maxLength之后,除了在hahnsaja答案中-计数器显示在TextFormField小部件的底部。为了能够隐藏它,请将其添加到InputDecoration:
counterText: '',
counterStyle: TextStyle(fontSize: 0),
示例:
TextFormField(
controller: _deskripsiController,
onFieldSubmitted: (String value){
setState(() {
_deskripsiController.text = value;
});
},
decoration: const InputDecoration(
border: const UnderlineInputBorder(),
labelText: "Deskripsi",
counterText: '',
counterStyle: TextStyle(fontSize: 0),
),
maxLength: 8,
)