Flutter 如何让孩子在飘动中适合身高?
我有一个带有输入文件和搜索按钮的Flutter 如何让孩子在飘动中适合身高?,flutter,widget,row,height,Flutter,Widget,Row,Height,我有一个带有输入文件和搜索按钮的行小部件。并且该行位于列中 如何使搜索按钮适合行的高度,使按钮大小等于输入字段的高度 我尝试将crossAxisAlignment:crossAxisAlignment.stretch,添加到行中,但出现错误:- BoxConstraints强制无限高。 我没有输入字段的高度。不要硬编码按钮的高度,因为它应该始终与输入框的高度匹配 另外,为行指定高度并添加CrossAxisAlignment.stretch不会更改输入字段的高度,唯一的按钮是更改 这里的问题是什
行
小部件。并且该行位于列中
如何使搜索按钮适合行的高度,使按钮大小等于输入字段的高度
我尝试将crossAxisAlignment:crossAxisAlignment.stretch,
添加到行中,但出现错误:-
BoxConstraints强制无限高。
我没有输入字段的高度。不要硬编码按钮的高度,因为它应该始终与输入框的高度匹配
另外,为行指定高度并添加CrossAxisAlignment.stretch
不会更改输入字段的高度,唯一的按钮是更改
这里的问题是什么?如何解决
Row(
// crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(
child: Container(
child: TextField(
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'search',
hintStyle:
Theme.of(context).textTheme.bodyText2.copyWith(
color: Theme.of(context).accentColor,
),
contentPadding: EdgeInsets.symmetric(
vertical: 8, horizontal: 20
),
fillColor: Theme.of(context).primaryColor,
filled: true,
),
style: Theme.of(context).textTheme.bodyText2,
),
),
),
FlatButton(
onPressed: () {},
color: Colors.grey,
child: Icon(
Icons.search,
),
),
],
)
我可以给你一个主意
将
扁平按钮
也包裹在容器
内,并定义这些按钮的宽度和高度。类似这样的东西, Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
width: MediaQuery.of(context).size.width * 0.8,
color: Theme.of(context).primaryColor,
height: 50.0,
child: TextField(
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'search',
hintStyle: Theme.of(context).textTheme.bodyText2.copyWith(
color: Theme.of(context).accentColor,
),
contentPadding:
EdgeInsets.symmetric(vertical: 8, horizontal: 20),
filled: true,
),
style: Theme.of(context).textTheme.bodyText2,
),
),
Container(
height: 50.0,
width: MediaQuery.of(context).size.width * 0.2,
child: FlatButton(
onPressed: () {},
color: Colors.grey,
child: Icon(
Icons.search,
),
),
),
],
)
或者,至少为
扁平按钮
定义高度
和宽度
,并将文本字段
保留为展开
希望能成功 要使
按钮始终具有与文本字段相同的高度
,最好将行
小部件包装为具有固定高度的容器
,将按钮
包装为具有两倍无限高度的容器
:
Container(
height: 48,
child: Row(
// crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(
child: Container(
child: TextField(
decoration: InputDecoration(
border: InputBorder.none,
hintText: 'search',
hintStyle:
Theme.of(context).textTheme.bodyText2.copyWith(
color: Theme.of(context).accentColor,
),
contentPadding: EdgeInsets.symmetric(
vertical: 8, horizontal: 20
),
fillColor: Theme.of(context).primaryColor,
filled: true,
),
style: Theme.of(context).textTheme.bodyText2,
),
),
),
Container(
height: double.infinity,
child: FlatButton(
onPressed: () {},
color: Colors.grey,
child: Icon(
Icons.search,
),
),
),
],
),
);
我有同样的要求-我想在文本字段的末尾有一个方形容器。但由于文本大小和文本输入高度可能会随时间而变化,所以我不想硬编码其大小
一种解决方案是简单地在TextField
上使用,作为文本输入的一部分(红方块)
在我的例子中,我不得不在TextField
之外使用额外的Widget
。
这里的关键是使用:
IntrinsicHeight
asRow
parent
AspectRatio
1作为容器的父级(灰色正方形)
见代码:
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
视觉密度:视觉密度。自适应平台密度,
),
家:脚手架(
appBar:appBar(
标题:文本(“颤振演示主页”),
),
正文:专栏(
mainAxisAlignment:mainAxisAlignment.start,
儿童:[
内在和谐(
孩子:排(
儿童:[
扩大(
孩子:TextField(
textAlignVertical:textAlignVertical.center,
装饰:输入装饰(
hintText:“搜索…”,
是的,
填充颜色:颜色。蓝灰色,
边框:InputBorder.none,
前缀:常量图标(
Icons.search,
颜色:颜色,白色,
),
容器(
颜色:颜色,红色,
子:常量图标(
图标。关闭,
颜色:颜色,黑色,
),
),
),
),
),
AspectRatio(
方面:1,
儿童:手势检测器(
onTap:(){
//点击这里
},
子:容器(
颜色:颜色。灰色,
子:常量图标(
图标。清除,
颜色:颜色,白色,
),
),
),
),
],
),
),
],
),
),
);
}
}
看看
注意,我在中找到了这个解决方案的灵感。它不起作用,按钮的这个高度没有改变,但宽度不是总宽度的一半。有点像,高度50几乎是输入大小的完美猜测,但是如果您指定的高度不是50,比如100,我们也有同样的问题。您给行的高度是多少(),在这里,你必须猜测输入框的高度。这应该是可以接受的答案,因为你不能总是设置一个恒定的高度