dart - Flutter 又抛出一个异常:RenderBox was not layout: RenderRepaintBoundary#eaea6 NEEDS-LAYOUT NEEDS-PAINT
问题描述
我正在使用 SingleChildScrollView 和 Column 来显示 swiper 和 gridview。
如果我在列中使用其他一些小部件,如文本、图像,则应用程序显示正常。但是我的swiper和gridview无法显示,并报错:
I/flutter ( 4687): ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
I/flutter ( 4687): The following assertion was thrown during performLayout():
I/flutter ( 4687): RenderFlex children have non-zero flex but incoming height constraints are unbounded.
I/flutter ( 4687): When a column is in a parent that does not provide a finite height constraint, for example if it is
I/flutter ( 4687): in a vertical scrollable, it will try to shrink-wrap its children along the vertical axis. Setting a
I/flutter ( 4687): flex on a child (e.g. using Expanded) indicates that the child is to expand to fill the remaining
I/flutter ( 4687): space in the vertical direction.
I/flutter ( 4687): These two directives are mutually exclusive. If a parent is to shrink-wrap its child, the child
I/flutter ( 4687): cannot simultaneously expand to fit its parent.
I/flutter ( 4687): Consider setting mainAxisSize to MainAxisSize.min and using FlexFit.loose fits for the flexible
I/flutter ( 4687): children (using Flexible rather than Expanded). This will allow the flexible children to size
I/flutter ( 4687): themselves to less than the infinite remaining space they would otherwise be forced to take, and
I/flutter ( 4687): then will cause the RenderFlex to shrink-wrap the children rather than expanding to fit the maximum
I/flutter ( 4687): constraints provided by the parent.
I/flutter ( 4687): The affected RenderFlex is:
I/flutter ( 4687): RenderFlex#1f4de relayoutBoundary=up14 NEEDS-LAYOUT NEEDS-PAINT
I/flutter ( 4687): The creator information is set to:
I/flutter ( 4687): Column ← _SingleChildViewport ← IgnorePointer-[GlobalKey#ae070] ← Semantics ← Listener ←
I/flutter ( 4687): _GestureSemantics ← RawGestureDetector-[LabeledGlobalKey<RawGestureDetectorState>#e4f77] ←
I/flutter ( 4687): _ScrollableScope ← _ScrollSemantics-[GlobalKey#4bb86] ← RepaintBoundary ← CustomPaint ←
I/flutter ( 4687): RepaintBoundary ← ⋯
我曾经是一名 uwp 开发人员,并且是 Flutter 的新手。任何人都可以帮忙吗?这是代码:
class _HomePageState extends State<HomePage> {
final List<ListItem> _children;
@override
Widget build(BuildContext context) {
return Scaffold(
body:SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Expanded(
child: Container(
height: 200,
child: Swiper(
itemBuilder: (BuildContext context, int index) {
return Image.network(
"http://via.placeholder.com/350x150",
fit: BoxFit.fitHeight,
);
},
itemCount: 5,
pagination: SwiperPagination(),
control: SwiperControl(),
),
)),
Expanded(
child: GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 4, childAspectRatio: 1.0),
itemBuilder: (BuildContext context, int index) {
return ListItemWidget(_children[index]);
},
itemCount: _children.length,
),
),
],
),
),
);
}
}
解决方案
您不能Expanded
在SingleChildScrollView
其中拥有一个具有无限垂直尺寸的小部件。
class _HomePageState extends State<HomePage> {
final List<ListItem> _children;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Expanded(
child: Container(
height: 200,
child: Swiper(
itemBuilder: (BuildContext context, int index) {
return Image.network(
"http://via.placeholder.com/350x150",
fit: BoxFit.fitHeight,
);
},
itemCount: 5,
pagination: SwiperPagination(),
control: SwiperControl(),
),
)),
Expanded(
child: GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 4, childAspectRatio: 1.0),
itemBuilder: (BuildContext context, int index) {
return ListItemWidget(_children[index]);
},
itemCount: _children.length,
),
),
],
),
);
}
}
推荐阅读
- ruby - 在 Windows 中安装 zxing_cpp gem 时 cmake 抛出错误
- oracle - obiee12c RCU 创建失败,Oracle 数据库 19c
- intellij-idea - IntelliJ 在同一个包中显示源文件和测试文件
- c# - StyleBundle 打破相对路径
- wordpress - 如何在 sendgrid wordpress 的联系人列表中添加订阅者?
- php - 我想计算属于同一类别的那些产品的数量
- python - 将带有列表的列拆分为数据框中的多列
- flutter - 在flutter App中首次显示获取的数据时出现问题
- javascript - JavaScript 数组属性更改
- python - discord.py 机器人命令 - 如何为不同的用户设置不同的冷却时间