Flutter 布局:并排渲染两个小部件
Flutter layout: render two widgets side by side
我正在尝试创建一个简单的布局,在同一行中插入 2 个小部件,为此我使用了一行,但我遇到了这些错误:
The following assertion was thrown during performLayout():
BoxConstraints forces an infinite width. These invalid constraints
were provided to RenderSemanticsAnnotations's layout() function by the
following function, which probably computed the invalid constraints in
question: RenderConstrainedBox.performLayout The offending constraints
were: BoxConstraints(w=Infinity, 50.0<=h<=Infinity)
但是如果我只渲染小部件而不使用 Row 小部件,一切都会按应有的方式呈现。
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
],
),
),
);
}
}
这是我要创建的布局结构:
任何关于我做错了什么或如何在同一行中获得这两项的见解都很棒。
将行的 children 换成 Expanded()
:
Row(
children: [
Expanded(
child: SellsCard(),
),
Expanded(
child: SellsCard(),
),
]
)
我正在尝试创建一个简单的布局,在同一行中插入 2 个小部件,为此我使用了一行,但我遇到了这些错误:
The following assertion was thrown during performLayout(): BoxConstraints forces an infinite width. These invalid constraints were provided to RenderSemanticsAnnotations's layout() function by the following function, which probably computed the invalid constraints in question: RenderConstrainedBox.performLayout The offending constraints were: BoxConstraints(w=Infinity, 50.0<=h<=Infinity)
但是如果我只渲染小部件而不使用 Row 小部件,一切都会按应有的方式呈现。
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
],
),
),
);
}
}
这是我要创建的布局结构:
任何关于我做错了什么或如何在同一行中获得这两项的见解都很棒。
将行的 children 换成 Expanded()
:
Row(
children: [
Expanded(
child: SellsCard(),
),
Expanded(
child: SellsCard(),
),
]
)