我想将交易标题和日期向左对齐,但是当我为父容器赋予行的全宽度(即double.infinity)时,将引发以下异常“ BoxConstraints强制无限宽度”。可能是什么原因?
body: new Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
new Card(child: new Text("CHART!")),
new Column(
children: [
...transactions.map((transaction) {
return new Card(
child: new Row(
children: [
new Container(
padding: EdgeInsets.all(10),
decoration: new BoxDecoration(
border: Border.all(
color: Colors.purple,
width: 2,
),
),
margin: EdgeInsets.symmetric(
vertical: 10,
horizontal: 15,
),
child: new Text(
transaction.amount.toString(),
style: new TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
color: Colors.purple),
),
),
new Column(
children: [
new Container(
width: double.infinity,
child: new Text(
transaction.title,
style: new TextStyle(
fontWeight: FontWeight.bold, fontSize: 17),
),
),
new Container(
child: new Text(
transaction.date.toString(),
style: new TextStyle(color: Colors.grey),
),
),
],
)
],
),
);
})
],);
答案 0 :(得分:2)
Row()
小部件不具有width属性,这意味着您应将Row()
放在另一个Container()
内,以便该行知道其约束,例如: / p>
new Row(
children: [
new Container(
padding: EdgeInsets.all(10),
decoration: new BoxDecoration(
border: Border.all(
color: Colors.purple,
width: 2,
),
),
margin: EdgeInsets.symmetric(
vertical: 10,
horizontal: 15,
),
child: new Text(
transaction.amount.toString(),
style: new TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
color: Colors.purple),
),
),
new Column(
children: [
new Container(
width: MediaQuery.of(context).size.width,,
child: new Text(
transaction.title,
style: new TextStyle(
fontWeight: FontWeight.bold, fontSize: 17),
),
),
new Container(
child: new Text(
transaction.date.toString(),
style: new TextStyle(color: Colors.grey),
),
),
],
)
],
注意:new
关键字是可选的。此外,将宽度设置为无穷大只会引起问题,因为它总是会超出屏幕,从而产生溢出。
答案 1 :(得分:0)
问题是You are setting stretch for crossAxisAlignment
,它为列提供了无限的宽度。您可以通过设置width属性来为Container提供约束条件
width: MediaQuery.of(context).size.width
这应该有效。