0

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

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

在此处输入图像描述

4

2 回答 2

3

用 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

于 2020-11-10T04:37:18.933 回答
-1

我有办法解决我的问题。希望它可以节省某人的时间。
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;
                      }
                    },
于 2020-11-10T07:48:00.413 回答