在设置不同的内容高度时,我调整动画容器大小时遇到了问题。
调整大小时会抛出异常:
════════ 渲染库捕获的异常 ════════
在布局期间抛出了以下断言:
一个RenderFlex在底部溢出154个像素。
以下是一个最小化的示例,可重现该问题(在我的真实应用程序中要复杂得多,但你有自己的观点):
bool expanded;
initState() {
super.initState();
expanded = false;
}
void _toggleMode() {
setState(() {
expanded = !expanded;
});
}
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Test")),
body: AnimatedContainer(
height: expanded ? 230 : 70,
duration: Duration(milliseconds: 800),
child: Column(
children: <Widget>[
Expanded(
child: PageView.builder(
itemBuilder: (context, position) {
return expanded
? Column(
children: <Widget>[
Container(height: 40, color: Colors.blue),
Container(height: 40, color: Colors.blue[400]),
Container(height: 40, color: Colors.blue[300]),
Container(height: 40, color: Colors.blue[200]),
Container(height: 40, color: Colors.blue[100]),
],
)
: Column(
children: <Widget>[
Container(height: 40, color: Colors.blue),
],
);
},
),
),
InkWell(onTap: _toggleMode, child: expanded ? Icon(Icons.keyboard_arrow_up) : Icon(Icons.keyboard_arrow_down))
],
),
),
);
}
在展开或不展开的情况下,内容都适配容器(没有溢出),问题只会在调整大小时出现。
当然,如果使用基本的容器而不进行动画处理,就不会出现问题。
我该如何处理这个问题?
SingleChildScrollView
而不是ListView
,我想这可能会更节约资源(?)。非常好用。 - Yann39