我试图在列表视图中添加一个Row
并获取错误信息
I/flutter (13858): ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
I/flutter (13858): The following assertion was thrown during performLayout():
I/flutter (13858): BoxConstraints forces an infinite height.
I/flutter (13858): These invalid constraints were provided to RenderParagraph's layout() function by the following
I/flutter (13858): function, which probably computed the invalid constraints in question:
I/flutter (13858): RenderFlex.performLayout (package:flutter/src/rendering/flex.dart:805:17)
I/flutter (13858): The offending constraints were:
I/flutter (13858): BoxConstraints(w=72.0, h=Infinity)
根据针对该错误的指导,我尝试将我的
ListView
包装在一个Expanded
中,但这只会导致Expanded widgets must be placed inside Flex widgets.
,这导致我尝试另一种方法……最后又出现了同样的错误。我的小部件在下面。有没有办法让这个工作?我最终要做的是显示多组行,它们之间有一些文本,允许用户上下滚动页面。
class _RoutineEditState extends State<RoutineEdit> {
String routineName;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Edit Routine'),
),
body: ListView(
//padding: EdgeInsets.fromLTRB(10, 15, 10, 5),
children: <Widget>[
Text('ddedede'),
Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Set'),
],
),
],
),
);
}
}
最佳答案
因为我不知道你想要的布局。
只是为了修正错误。我们有三种选择。
首先:使用-IntrinsicHeight
小部件。
代码:
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Edit Routine'),
),
body: ListView(
//shrinkWrap: true,
//padding: EdgeInsets.fromLTRB(10, 15, 10, 5),
children: <Widget>[
Text('ddedede'),
IntrinsicHeight(
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Set'),
Text('Set'),
Text('Set'),
],
),
),
],
),
);
}
第二:将您的行包装在
LimitedBox
中并保持-crossAxisAlignment: CrossAxisAlignment.stretch
代码:
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Edit Routine'),
),
body: ListView(
shrinkWrap: true,
//padding: EdgeInsets.fromLTRB(10, 15, 10, 5),
children: <Widget>[
Text('ddedede'),
LimitedBox(
maxHeight: 200.0,
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Set'),
Text('Set'),
Text('Set'),
],
),
),
],
),
);
}
第三:删除或评论-
crossAxisAlignment: CrossAxisAlignment.stretch
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Edit Routine'),
),
body: ListView(
shrinkWrap: true,
//padding: EdgeInsets.fromLTRB(10, 15, 10, 5),
children: <Widget>[
Text('ddedede'),
Row(
// crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Set'),
Text('Set'),
Text('Set'),
],
),
],
),
);
}
关于flutter - ListView内的 flutter 行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57300552/