flutter - 达到最大线时如何使 TextField 停止接收输入?
问题描述
作为标题,我设置了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