gpt4 book ai didi

flutter - 动画容器 : RenderFlex overflowed by 154 pixels on the bottom

转载 作者:行者123 更新时间:2023-12-04 00:01:56 25 4
gpt4 key购买 nike

在设置不同的内容高度时调整动画容器的大小时遇到​​问题。
它在调整大小时抛出异常:

════════ Exception caught by rendering library ════════

The following assertion was thrown during layout:

A RenderFlex overflowed by 154 pixels on the bottom.


Animated container overflow
这是重现问题的最小示例(在我的实际应用程序中要复杂得多,但您明白了):
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))
],
),
),
);
}
在两种模式下(扩展与否),内容都适合容器(无溢出),问题仅在调整大小时出现。
当然,没有动画的基本容器不会发生问题。
我该如何处理?

最佳答案

发生是因为您检查了已扩展,然后在容器达到最终大小之前立即返回容器

解决方法 将使用带有 NeverScrollableScrollPhysics() 的 ListView 更改更大的列

编辑:您甚至不需要再检查是否展开,您将获得正确的动画

 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 ListView(
physics: NeverScrollableScrollPhysics(),
children: <Widget>[
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))
],
),
),
);
}

enter image description here

关于flutter - 动画容器 : RenderFlex overflowed by 154 pixels on the bottom,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60084415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com