SingleChildScrollView 内的颤振列不滚动
Posted
技术标签:
【中文标题】SingleChildScrollView 内的颤振列不滚动【英文标题】:Flutter Column inside SingleChildScrollView doesn't scroll 【发布时间】:2021-08-11 16:41:18 【问题描述】:当我尝试在 SingleScrollChildView 中使用 Card 时,我收到此错误:
The following assertion was thrown during layout:
A RenderFlex overflowed by 178 pixels on the bottom.
The relevant error-causing widget was:
Column file:///C:/Users/emirs/androidStudioProjects/view_met/lib/home.dart:197:16
The overflowing RenderFlex has an orientation of Axis.vertical.
The edge of the RenderFlex that is overflowing has been marked in the rendering with a yellow and black striped pattern. This is usually caused by the contents being too big for the RenderFlex.
Consider applying a flex factor (e.g. using an Expanded widget) to force the children of the RenderFlex to fit within the available space instead of being sized to their natural size.
This is considered an error condition because it indicates that there is content that cannot be seen. If the content is legitimately bigger than the available space, consider clipping it with a ClipRect widget before putting it in the flex, or using a scrollable container rather than a Flex, like a ListView.
The specific RenderFlex in question is: RenderFlex#d861f relayoutBoundary=up2 OVERFLOWING
... needs compositing
... parentData: offset=Offset(0.0, 0.0) (can use size)
... constraints: BoxConstraints(0.0<=w<=411.4, 0.0<=h<=820.6)
... size: Size(411.4, 820.6)
... direction: vertical
... mainAxisAlignment: start
... mainAxisSize: max
... crossAxisAlignment: center
... verticalDirection: down
◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤
这是我的代码:
Padding(
padding: EdgeInsets.fromLTRB(0, 20, 0, 0),
child: Column(
children: <Widget>[
Text("Random Items You Could Like", style: GoogleFonts.merriweather(fontSize: 18, color: Colors.black)),
SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Column(
children: <Widget>[
builder(randint1.toString()),
builder(randint2.toString()),
builder(randint3.toString()),
builder(randint4.toString()),
builder(randint5.toString()),
],
)
),
],
),
)
这是builder()
函数:
builder(String id)
return FutureBuilder(
future: fetchData(id),
builder: (BuildContext context, snapshot)
if (snapshot.connectionState == ConnectionState.waiting)
return Padding(
padding: EdgeInsets.fromLTRB(0, 20, 0, 0),
child: CircularProgressIndicator(),
);
var data = jsonDecode(snapshot.data.toString());
var leading;
var artist;
if (data["primaryImageSmall"] == "")
leading = Icon(Icons.dangerous);
else
leading = Image.network(data["primaryImageSmall"]);
if (data["artistDisplayName"]== "")
artist = "Unknown";
else
artist = data["artistDisplayName"];
return Card(
clipBehavior: Clip.antiAlias,
child: Column(
children: [
ListTile(
leading: leading,
title: Text(data["title"]),
subtitle: Text(
"by $artist",
style: TextStyle(color: Colors.black.withOpacity(0.6)),
),
),
ButtonBar(
alignment: MainAxisAlignment.start,
children: [
TextButton(
onPressed: ()
Navigator.push(
context,
MaterialPageRoute(builder: (context) => DetailsPage()),
);
,
child: Text("Details", style: TextStyle(color: Color(0xFF6200EE))),
),
TextButton(
onPressed: ()
Navigator.push(
context,
MaterialPageRoute(builder: (context) => DetailsPage()),
);
,
child: Text("Add to Favorites", style: TextStyle(color: Color(0xFF6200EE))),
),
],
),
],
),
);
,
);
这是结果:
我也查看了其他问题,但没有一个真正有帮助,也没有我想要的答案。我现在真的不知道该怎么办。我将不胜感激任何类型的帮助。如果需要,我可以提供更多信息。
【问题讨论】:
【参考方案1】:用Expanded
包裹SingleChildScrollView
。
Column(
children: [
// ...
Expanded(
child: SingleChildScrollView(),
),
],
),
【讨论】:
成功了,非常感谢。我遇到了一个错误,但我在另一个问题上用这个答案修复了它:***.com/a/52801899/12920146【参考方案2】:我认为您错误地使用了SingleChildScrollView
,删除SingleChildScrollView
和scrollDirection;用它来包装这部分:
Column(
children: <Widget>[
builder(randint1.toString()),
builder(randint2.toString()),
builder(randint3.toString()),
builder(randint4.toString()),
builder(randint5.toString()),
],
它看起来像这样:
SingleChildScrollView(
scrollDirection: Axis.vertical,
Column(
children: <Widget>[
builder(randint1.toString()),
builder(randint2.toString()),
builder(randint3.toString()),
builder(randint4.toString()),
builder(randint5.toString()),
],
),
【讨论】:
【参考方案3】:在下面试用代码
Padding(
padding: EdgeInsets.fromLTRB(0, 20, 0, 0),
child: Stack(
children: [
Align(
alignment:Alignment.topCenter,
child: Text("Random Items You Could Like", style: GoogleFonts.merriweather(fontSize: 18, color: Colors.black))),
SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Column(
children: <Widget>[
Column(
children: <Widget>[
builder(randint1.toString()),
builder(randint2.toString()),
builder(randint3.toString()),
builder(randint4.toString()),
builder(randint5.toString()),
],
),
],
),
),
],
),
)
【讨论】:
以上是关于SingleChildScrollView 内的颤振列不滚动的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Flutter 中自动滚动到 SingleChildScrollView 内的行的位置
SingleChildScrollView 内的水平滚动 ListView.builder - 包装内容高度
键盘溢出 * 像素。无法使用 SingleChildScrollView 或 ListView