我正在尝试创建一个简单的布局,在同一行中插入2个小部件,为此我使用了Row,但是出现了以下错误:
在performLayout()期间引发了以下断言: BoxConstraints强制无限宽度。这些无效的约束 由RenderSemanticsAnnotations的layout()函数提供给 跟随函数,可能计算了 问题:RenderConstrainedBox.performLayout令人讨厌的约束 是:BoxConstraints(w = Infinity,50.0 <= h <= Infinity)
但是如果我不使用“行”窗口小部件而渲染窗口小部件,一切都将得到渲染。
class CardWithTitle extends StatelessWidget {
const CardWithTitle({Key key, this.title, this.child}) : super(key: key);
final String title;
final child;
@override
Widget build(BuildContext context) {
return Container(
child: Column(
children: [
Row(
children: [
Text(title),
],
),
Container(
width: double.infinity,
constraints: BoxConstraints(minHeight: 50),
child: Card(
child: Padding(
padding: EdgeInsets.all(10.0),
child: child,
),
),
)
],
),
);
}
}
class SellsCard extends StatelessWidget {
SellsCard({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.all(10.0),
child: CardWithTitle(
title: 'Sells',
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Total',
),
Text(
'\$ 96.500,54'
),
Text(
'Lorem ipsum dolor'
)
],
),
),
);
}
}
class HomePage extends StatefulWidget {
HomePage({Key key}) : super(key: key);
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<HomePage> {
String _username;
String _title;
String _usernameAbbr;
@override
Widget build(BuildContext context) {
_username = 'James';
_title = 'Some title';
_usernameAbbr = _username[0].toUpperCase();
return Scaffold(
appBar: AppBar(
title: Text(_title),
elevation: 0,
actions: [
Padding(
padding: EdgeInsets.all(20.0),
child: GestureDetector(
onTap: () {},
child: CircleAvatar(
backgroundColor: Theme.of(context).backgroundColor,
radius: 10.0,
child: FittedBox(
fit: BoxFit.fitWidth,
child: Text(_usernameAbbr),
),
),
))
],
),
body: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
SellsCard(), // <-- this is working
SellsCard(), // <-- this is working
Row(children: [SellsCard(), SellsCard()]) // <-- This is throwing the errors
],
),
),
);
}
}
这是我要创建的布局结构:
关于我做错了什么或如何在同一行中获得这两项的任何见解都会很棒。
答案 0 :(得分:1)
用Expanded()
包裹Row的孩子:
Row(
children: [
Expanded(
child: SellsCard(),
),
Expanded(
child: SellsCard(),
),
]
)