【问题标题】:Flutter 2: maxLength inside the textfield without using onChangedFlutter 2:文本字段内的 maxLength 不使用 onChanged
【发布时间】:2022-01-19 05:39:20
【问题描述】:

我正在尝试找到一种方法来使用 maxLength 并将其放在文本字段中以进行良好的渲染。但是,我不想使用 onChanged 参数,因为我必须使用 setState,但这不是使用它的好方法。

flutter 文本字段中的参数 maxLength 旨在不更新状态,因此每个有状态的小部件都不必重建。

所以我想把 0/50 放在里面,在文本字段的末尾,与标题在同一行。

从此:

到这里:

所以有人知道如何在不使用参数 onChanged 的​​情况下做到这一点?

flutter 有实现这个功能吗?

【问题讨论】:

    标签: flutter textfield


    【解决方案1】:

    试试这个,希望你能得到解决方案。

    TextField(
      maxLength: 250,
      buildCounter: (_, {currentLength, maxLength, isFocused}) => Padding(
        padding: const EdgeInsets.only(left: 16.0),
        child: Container(
            alignment: Alignment.centerLeft,
            child: Text(currentLength.toString() + "/" + maxLength.toString())),
      ),
    )
    

    另外,试试这个,

      TextField(
              controller: nameController,
                  maxLength: 50,
                   prefixIcon: Icon(
                    Icons.account_circle,
                    color: Colors.black45,
                  ),
                  
              ),
    

    【讨论】:

    • 不,不是那样,“一种使用 maxLength 并将其放在文本字段内的方法”。这个问题可能解释得不够充分,我用另一张图片更新了它。
    猜你喜欢
    • 2020-10-31
    • 2023-04-02
    • 2020-09-22
    • 1970-01-01
    • 1970-01-01
    • 2020-05-20
    • 2021-10-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多