Flutter 行内的文本字段导致布局异常:无法计算大小
我得到一个渲染异常,我不知道如何修复。我正在尝试创建一个包含3行的列 行[图像] 行[文本字段] 行[按钮] 以下是我构建容器的代码:Flutter 行内的文本字段导致布局异常:无法计算大小,flutter,dart,flutter-text,flutter-row,Flutter,Dart,Flutter Text,Flutter Row,我得到一个渲染异常,我不知道如何修复。我正在尝试创建一个包含3行的列 行[图像] 行[文本字段] 行[按钮] 以下是我构建容器的代码: Container buildEnterAppContainer(BuildContext context) { var container = new Container( padding: const EdgeInsets.all(8.0), child: new Column( mainAxisAlignme
Container buildEnterAppContainer(BuildContext context) {
var container = new Container(
padding: const EdgeInsets.all(8.0),
child: new Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
buildImageRow(context),
buildAppEntryRow(context),
buildButtonRow(context)
],
),
);
return container;
}
我不再得到例外。行实现缺少什么导致它无法计算该行的大小?(我假设您使用的是行
,因为您希望将来将其他小部件放在文本字段旁边。)
行
小部件希望确定其非灵活子项的固有大小,以便知道为灵活子项保留了多少空间。但是,TextField
没有固有的宽度;它只知道如何将自身大小调整为其父容器的全宽。尝试将其包装在灵活的
或扩展的
中,告诉行
您希望文本字段
占用剩余空间:
new Row(
children: <Widget>[
new Flexible(
child: new TextField(
decoration: const InputDecoration(helperText: "Enter App ID"),
style: Theme.of(context).textTheme.body1,
),
),
],
),
新行(
儿童:[
新柔性(
孩子:新文本字段(
装饰:常量输入装饰(helperText:“输入应用程序ID”),
样式:Theme.of(context).textTheme.body1,
),
),
],
),
您应该使用Flexible在行内使用文本字段
new Row(
children: <Widget>[
new Text("hi there"),
new Container(
child:new Flexible(
child: new TextField( ),
),//flexible
),//container
],//widget
),//row
新行(
儿童:[
新文本(“你好”),
新容器(
儿童:新的灵活(
子项:新的TextField(),
),//灵活
),//容器
],//小部件
),//行
一个简单的解决方案是将文本()
包装在容器()中。
因此,您的代码如下所示:
Container(
child: TextField()
)
Expanded(
child: TextField(
decoration: InputDecoration(
hintText: "Demo Text",
hintStyle: TextStyle(fontWeight: FontWeight.w300, color: Colors.red)
),
),
),
这里还可以获得容器的宽度和高度属性,以调整文本字段的外观。如果将文本字段包装在容器中,则无需使用Flexible
。正如@Asif Shiraz提到的,我也遇到了同样的问题,并通过将列包装在Flexible中解决了这个问题,如下所示
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new Scaffold(
body: Row(
children: <Widget>[
Flexible(
child: Column(
children: <Widget>[
Container(
child: TextField(),
)
//container
],
))
],
mainAxisAlignment: MainAxisAlignment.spaceBetween,
),
));
}
}
类MyApp扩展了无状态小部件{
//此小部件是应用程序的根。
@凌驾
小部件构建(构建上下文){
返回新材料PP(
标题:“颤振演示”,
主题:新主题数据(
主样本:颜色。蓝色,
),
家:新脚手架(
正文:世界其他地区(
儿童:[
灵活的(
子:列(
儿童:[
容器(
子项:TextField(),
)
//容器
],
))
],
mainAxisAlignment:mainAxisAlignment.spaceBetween,
),
));
}
}
解决方案是将文本()包装在以下小部件之一中:
不是…就是…因此,使用Expanded的代码如下所示:
Container(
child: TextField()
)
Expanded(
child: TextField(
decoration: InputDecoration(
hintText: "Demo Text",
hintStyle: TextStyle(fontWeight: FontWeight.w300, color: Colors.red)
),
),
),
之所以会出现此错误,是因为TextField
在水平方向上展开,行也会展开,因此我们需要限制TextField
的宽度,有很多方法
使用扩展的
Row(
children: <Widget>[
Expanded(child: TextField()),
OtherWidget(),
],
)
将其包装在容器中
或大小框中
并提供宽度
Row(
children: <Widget>[
SizedBox(width: 100, child: TextField()),
OtherWidget(),
],
)
行(
儿童:[
SizedBox(宽度:100,子项:TextField()),
OtherWidget(),
],
)
我也有同样的问题
如果您愿意,您可以使用表小部件来避免文本字段出现这种问题,只需将文本字段用灵活的或扩展的包装起来就可以了。这不应该在flifter doc的某个地方吗?@stt106它是-->但我同意,这不容易找到。他们也没有像柯林·杰克逊(Collin Jackson)所做的那样让解决方案变得显而易见。使用此方法会中断行小部件的mainAxisAlignment
。使用两个文本小部件没有问题,但是使用Flexible
中包含的文本小部件和文本字段小部件,它会向左对齐,没有间隔。我可以请您在这里看一个与颤振相关的问题吗:?出于某种原因,这个小更改需要颤振热重启(不仅仅是热重新加载)在我的例子中,为了消除布局异常。我认为您不需要容器
,您可以直接使用灵活的
。此建议非常有用。当您在一行中有多个TextField
时,行内灵活是最好的选择。Expanded具有固定高度,如果不添加width:n
请参见此解决上述问题:
Row(
children: [
Expanded(
flex: 1,
child: Padding(
padding: EdgeInsets.only(left: 5.0,right: 5.0),
child: TextFormField(
controller: commentController,
validator: (String value){
if(value.isEmpty){
// ignore: missing_return
return 'Comment cannot be blank.';
}
},
decoration: InputDecoration(
labelText: "Comment",
labelStyle: TextStyle(
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Colors.grey),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.green))),
),
),
),
],
),
Row(
children: <Widget>[
SizedBox(width: 100, child: TextField()),
OtherWidget(),
],
)
Row(
children: [
Expanded(
flex: 1,
child: Padding(
padding: EdgeInsets.only(left: 5.0,right: 5.0),
child: TextFormField(
controller: commentController,
validator: (String value){
if(value.isEmpty){
// ignore: missing_return
return 'Comment cannot be blank.';
}
},
decoration: InputDecoration(
labelText: "Comment",
labelStyle: TextStyle(
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Colors.grey),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.green))),
),
),
),
],
),