嗨,我正在尝试隐藏隐藏最大长度文本字段。我添加了 counterText: ""、counter: Offstage() 和 counter: SizedBox.shrink()。它正在工作,唯一的问题是文本字段的大小发生了变化。我已将其设置在sizedbox中,但它对文本字段的高度和宽度没有任何影响。
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 中隐藏最大长度指示器时 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
),
),
),
),
],
),
),
],
),
您可以使用
contentPadding
调整文本字段高度。
将
contentPadding
设置为contentPadding: EdgeInsets.fromLTRB(15,40,15,40)
,调整顶部和底部值来改变textField的大小
并且您可以更改
cursor
的大小以适合文本字段 cursorHeight: 30