简体   繁体   English

Flutter:如何移动 TextField 字符计数器?

[英]Flutter: How to move TextField characer counter?

The title sums up the question pretty well.标题很好地总结了这个问题。 I have a TextField with a maxLength: 250 and this is what it looks like:我有一个maxLength: 250TextField ,这就是它的样子:

在此处输入图像描述

Is there a way to put the counter somewhere else?有没有办法把柜台放在别的地方? Optimally to the left of the send button, but otherwise maybe just above and on the left of the TextField .最好在发送按钮的左侧,否则可能就在TextField的上方和左侧。 Any ideas?有任何想法吗? Thanks!谢谢!

Probably not necessary, but here's my code:可能没有必要,但这是我的代码:

TextField(
              controller: inputTextEditingController,
              focusNode: inputFocusNode,
              style: TextStyle(color: Platform.isAndroid ? Colors.green : Colors.blue, height: 0.8),
              maxLength: 250,
              maxLines: null,
              decoration: InputDecoration(
                  contentPadding: const EdgeInsets.fromLTRB(20, 15, 0, 15),
                  border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(28)),
                  focusedBorder: OutlineInputBorder(
                      borderSide: BorderSide(color: Platform.isAndroid ? Colors.green : Colors.blue),
                      borderRadius: BorderRadius.circular(28)),
                  suffixIcon: IconButton(
                    onPressed: _handleSubmitted,
                    icon: Padding(
                      padding: const EdgeInsets.fromLTRB(0, 0, 20, 0),
                      child: Icon(Icons.send,
                          color: inputFocusNode.hasFocus
                              ? Platform.isAndroid ? Colors.green : Colors.blue
                              : Colors.black54),
                    ),
                  ),
                  hintText: "Say something!",
                  hintStyle: inputFocusNode.hasFocus
                      ? TextStyle(color: Platform.isAndroid ? Colors.green : Colors.blue, fontSize: 16)
                      : TextStyle(color: Colors.black54)),

You need to build and pass your own counter as a buildCounter parameter of a TextField Widget.您需要构建并传递您自己的计数器作为 TextField 小部件的 buildCounter 参数。

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())),
  ),
)

First initialise two variables in your stateful class as below,首先在有状态的 class 中初始化两个变量,如下所示,

  int maxlength = 50;
  int currentLength = 0;

Then, make subsequent changes in your widget as below.然后,在您的小部件中进行后续更改,如下所示。

      TextField(
                controller: inputTextEditingController,
                focusNode: inputFocusNode,
                style: TextStyle(
                    color: Platform.isAndroid ? Colors.green : Colors.blue,
                    height: 0.8),
                maxLength: maxlength,
                maxLines: null,
                decoration: InputDecoration(
                  counter: Container(
                  alignment: Alignment.center,
                  child: Text("${currentLength}/${maxlength}"),
                  ),
                  contentPadding: const EdgeInsets.fromLTRB(20, 15, 0, 15),
                  border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(28)),
                  focusedBorder: OutlineInputBorder(
                      borderSide: BorderSide(
                          color:
                              Platform.isAndroid ? Colors.green : Colors.blue),
                      borderRadius: BorderRadius.circular(28)),
                  suffixIcon: IconButton(
                    onPressed: _handleSubmitted,
                    icon: Padding(
                      padding: const EdgeInsets.fromLTRB(0, 0, 20, 0),
                      child: Icon(Icons.send,
                          color: inputFocusNode.hasFocus
                              ? Platform.isAndroid ? Colors.green : Colors.blue
                              : Colors.black54),
                    ),
                  ),
                  hintText: "Say something!",
                  hintStyle: inputFocusNode.hasFocus
                      ? TextStyle(
                          color:
                              Platform.isAndroid ? Colors.green : Colors.blue,
                          fontSize: 16)
                      : TextStyle(color: Colors.black54),
                ),
              ),

Now, you can use any other widget for your counter in "decoration: InputDecoration" .现在,您可以在"decoration: InputDecoration"中为您的计数器使用任何其他小部件。 I have used container with alignment property.我使用了具有 alignment 属性的容器 you can change as per your requirements.您可以根据您的要求进行更改。

This will surely benefit for you.这肯定对你有好处。

  child: new TextField(
                  style: BurmeseUtil.textStyle(context),
                  controller: txtController,
                  maxLength: 1500,
                  maxLines: null,
                  decoration: new InputDecoration(
                    counterText: '',
                    border: OutlineInputBorder(),
                  ),
                ),

Use decoration in TextField.Add counterText:' ' Good luck在 TextField 中使用装饰。添加 counterText:' ' 祝你好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM