gpt4 book ai didi

flutter - TextFormField的后缀图标问题

转载 作者:行者123 更新时间:2023-12-05 00:56:44 31 4
gpt4 key购买 nike

我有一个带后缀图标的TextFormField,用作IconButton:

TextFormField(
autocorrect: false,
decoration: InputDecoration(
prefixIcon: widget.icon,
isDense: true,
suffixIcon: IconButton(
icon: Icon(Icons.clear),
onPressed: () {...}
)
),
controller: _controller,
maxLines: null,
)

我的问题是,后缀图标增加了文本字段的高度:

enter image description here

(另外文字末尾和图标之间的空间太大):

enter image description here

我尝试了不同的方法来避免这种情况,但几乎一切都失败了。最后我在这里找到了一个可能的解决方案:https://github.com/flutter/flutter/issues/21908#issuecomment-516434465

所以,我尝试使用提到的 IntrinsicHeight 小部件:

IntrinsicHeight(
child: TextFormField(...)
)

确实,它标准化了我的 TextFormField 的高度,但现在有了自动换行/换行符:

enter image description here

如您所见,多线调整不再正常工作,几乎无法确定何时展开。

那么:您知道如何解决我最初关于后缀图标和生成的文本字段高度的问题吗?或者您现在如何解决使用 IntrisicHeight 时的多行问题?

最佳答案

您可以在下面复制粘贴运行完整代码
第 1 步:您可以使用 suffixIconConstraints 来减少填充 https://github.com/flutter/flutter/pull/50058
第二步:使用InkWell包裹Icon并设置Icon大小

TextFormField(
autocorrect: false,
decoration: InputDecoration(
prefixIcon: Icon(Icons.add),
prefixIconConstraints: BoxConstraints(
minWidth: 5,
minHeight: 5,
),
isDense: true,
suffixIconConstraints: BoxConstraints(
minWidth: 2,
minHeight: 2,
),
suffixIcon: InkWell(
child: Icon(Icons.clear, size: 14), onTap: () {})),
controller: _controller1,
maxLines: null,
),

工作演示

enter image description here

完整代码

import 'package:flutter/material.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);

final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
TextEditingController myController1 = TextEditingController();
TextEditingController _controller1 = TextEditingController();
TextEditingController _controller2 = TextEditingController();

void _incrementCounter() {
setState(() {
_counter++;
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(
child: TextFormField(
autocorrect: false,
decoration: InputDecoration(
prefixIcon: Icon(Icons.add),
prefixIconConstraints: BoxConstraints(
minWidth: 5,
minHeight: 5,
),
isDense: true,
suffixIconConstraints: BoxConstraints(
minWidth: 2,
minHeight: 2,
),
suffixIcon: InkWell(
child: Icon(Icons.clear, size: 14), onTap: () {})),
controller: _controller1,
maxLines: null,
),
),
Expanded(
child: TextFormField(
autocorrect: false,
decoration: InputDecoration(
prefixIcon: Icon(Icons.add),
prefixIconConstraints: BoxConstraints(
minWidth: 5,
minHeight: 5,
),
isDense: true,
suffixIconConstraints: BoxConstraints(
minWidth: 2,
minHeight: 2,
),
suffixIcon: InkWell(
child: Icon(Icons.clear, size: 14), onTap: () {})),
controller: _controller2,
maxLines: null,
),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}

关于flutter - TextFormField的后缀图标问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61761880/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com