user-interface - Flutter:如何移动文本字段字符计数器?

标签 user-interface flutter dart textfield maxlength

标题很好地总结了这个问题。我有一个 TextFieldmaxLength: 250这是它的样子:

enter image description here

有没有办法将计数器放在其他地方?最好在发送按钮的左侧,但也可能在 TextField 的上方和左侧.有任何想法吗?谢谢!

可能没有必要,但这是我的代码:

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

最佳答案

您需要构建和传递您自己的计数器作为 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())),
  ),
)

关于user-interface - Flutter:如何移动文本字段字符计数器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58019207/

相关文章:

google-chrome - 为什么我无法访问 Glassfish 管理界面?

python - 如何将微调器值设置为等于 kivy 中 python 方法的输出列表

listview - Flutter 反转 ListView 从顶部开始

dart - Flutter - 模拟器和手机中的布局不同

flutter - 文本表单字段验证器错误消息未显示

wordpress - 没有用户登录内页将禁用 WordPress

html - 添加一个简单的向下箭头图像到下拉菜单

xcode - GoogleService-Info.plist : Copy bundle resources or not

Flutter/Dart - 在我的构建中使用 FutureBuilder 和 Normal 非异步元素?

dart - 在 Flutter 中创建尖头/斜角小部件