隐藏TextField最大长度的计数

问题描述 投票:0回答:2

我试图隐藏

TextField
的最大长度。

我补充了:

  • counterText: ""
  • counter: Offstage()
  • counter: SizedBox.shrink()

它正在工作,唯一的问题是

TextField
的大小发生了变化。我已经在
sizedbox
中设置了,但是对
TextField
的高度和宽度没有任何影响。

Row(
  mainAxisAlignment: MainAxisAlignment.start,
  crossAxisAlignment: CrossAxisAlignment.start,
  children: [
    Expanded(
      child: Row(
        children: [
          const Padding(
            //padding: EdgeInsets.only(left: 50.0),
            padding: EdgeInsets.fromLTRB(50, 0, 0, 20),
            child: Text(
              "Father:",
              style: TextStyle(
                  fontWeight: FontWeight.bold,
                  fontSize: 14,
                  height: 1.5),
            ),
          ),
          const SizedBox(
            width: 5,
          ),
          /*Text(
            "Father:",
            style: TextStyle(
                fontWeight: FontWeight.bold,
                fontSize: 14,
                height: 1.5),
          ),*/
          Expanded(
            child: SizedBox(
              height: 60,
              child: TextField(
                controller: _fatherhistory,
                maxLength: 1000,
                decoration: InputDecoration(
                  border: OutlineInputBorder(),
                  contentPadding: EdgeInsets.only(left: 10),
                ),

                style: TextStyle(fontSize: 14),
                //maxLines: 1,
              ),
            ),
          ),
        ],
      ),
    ),
  ],
),
flutter dart textfield
2个回答
0
投票

为了解决在 Flutter 中隐藏最大长度指示器时 TextField 大小发生变化的问题,您可以在 SizedBox 中结合使用 InputDecoration 和约束。这是代码的修改版本,无论 counterText 是什么,它都应该保持 TextField 的大小

Row(
  mainAxisAlignment: MainAxisAlignment.start,
  crossAxisAlignment: CrossAxisAlignment.start,
  children: [
  Expanded(
    child: Row(
    children: [
      const Padding(
        padding: EdgeInsets.fromLTRB(50, 0, 0, 20),
        child: Text(
          "Father:",
          style: TextStyle(
            fontWeight: FontWeight.bold,
            fontSize: 14,
            height: 1.5
          ),
        ),
      ),
      const SizedBox(width: 5),
      Expanded(
        child: SizedBox(
          height: 60,
          child: TextField(
            controller: _fatherhistory,
            maxLength: 1000,
            decoration: InputDecoration(
              border: OutlineInputBorder(),
              contentPadding: EdgeInsets.only(left: 10),
              counterText: "", // Hide counter text
            ),
            style: TextStyle(fontSize: 14),
            // Applying constraints to maintain the size
            constraints: BoxConstraints(
              minHeight: 60, // Minimum height
              maxHeight: 60, // Maximum height
            ),
          ),
        ),
      ),
    ],
     ),
    ),
  ],
),

0
投票

您可以使用

contentPadding
调整文本字段高度。

contentPadding
设置为
contentPadding: EdgeInsets.fromLTRB(15,40,15,40)
,调整顶部和底部值来改变textField的大小

并且您可以更改

cursor
的大小以适合文本字段
cursorHeight: 30

© www.soinside.com 2019 - 2024. All rights reserved.