作为标题,我设置了maxLines:2,但是当我达到最大线时,我仍然可以输入。
并且controller.value有超出该区域的文本。
如何使文本字段被填充时用户无法输入,或剪切超出区域的值。
用 maxLines 试试 maxLength
maxLines 属性仅用于行,不限制文本的长度。
TextField(
maxLength: 10, //Any specific length
maxLines: 2,
decoration: InputDecoration(
counterText: ''
),
)
你也可以使用这个:
使用 inputFormatters 属性
TextFormField(
inputFormatters: [
LengthLimitingTextInputFormatter(10),
]
)
命名空间
import 'package:flutter/services.dart';
参考
https://api.flutter.dev/flutter/material/TextField/maxLength.html
我有办法解决我的问题。希望它可以节省某人的时间。
放minLines: 1 maxLines: 2
scrollPhysics: NeverScrollableScrollPhysics(),
并检查 maxScrollExtent when onChaged,如果它可以滚动,则意味着它超过maxLines
onChanged: (input) async {
await Future.delayed(Duration(milliseconds: 50));
if (scrollController.position.maxScrollExtent > 0.0) {
controller.text = oldText;
} else {
oldText = input;
}
},