首页 > 解决方案 > 达到最大线时如何使 TextField 停止接收输入?

问题描述

作为标题,我设置了maxLines:2,但是当我达到最大线时,我仍然可以输入。
并且controller.value有超出该区域的文本。

如何使文本字段被填充时用户无法输入,或剪切超出区域的值。

在此处输入图像描述

标签: flutter

解决方案


用 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


推荐阅读