无法在标准 ListView 小部件上滚动;总是过度渲染 150px
Posted
技术标签:
【中文标题】无法在标准 ListView 小部件上滚动;总是过度渲染 150px【英文标题】:Can't scroll on standard ListView widget; always over-renders by 150px 【发布时间】:2020-09-23 14:08:07 【问题描述】:ListView 根本不会滚动。错误说问题出在 main.dart 文件中,而不是在我正在处理的模块中,但这是溢出的模块。
尝试了多种方法:
主要:假, 物理:常量 AlwaysScrollableScrollPhysics(), 控制器:_控制器 关于制作列表视图的 Flutter 开发文档 关于制作列表视图的中等帖子 无数堆栈溢出Qs这是我的代码:
import 'package:flutter/material.dart';
class Listicle extends StatefulWidget
@override
ListicleState createState() => ListicleState();
class ListicleState extends State<Listicle>
final List<String> entries = <String>['A', 'B', 'C', 'D', 'E', 'F'];
final List<int> colorCodes = <int>[600, 500, 100, 600, 500, 100];
ScrollController _controller = new ScrollController();
@override
Widget build(BuildContext context)
return Container(
child: ListView.builder(
primary: false,
physics: const AlwaysScrollableScrollPhysics(),
controller: _controller,
shrinkWrap: true,
padding: const EdgeInsets.all(8),
itemCount: entries.length,
itemBuilder: (BuildContext context, int index)
return Container(
height: 100,
color: Colors.amber[colorCodes[index]],
child: Center(child: Text('Entry $entries[index]')),
);
)
);
这是错误:
════════ Exception caught by rendering library ═════════════════════════════════════════════════════
The following assertion was thrown during layout:
A RenderFlex overflowed by 150 pixels on the bottom.
The relevant error-causing widget was:
Column file:///Users/christopher/androidStudioProjects/test_app/lib/main.dart:76:13
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#c9341 relayoutBoundary=up1 OVERFLOWING
... needs compositing
... parentData: offset=Offset(0.0, 80.0); id=_ScaffoldSlot.body (can use size)
... constraints: BoxConstraints(0.0<=w<=392.7, 0.0<=h<=679.3)
... size: Size(392.7, 679.3)
... direction: vertical
... mainAxisAlignment: start
... mainAxisSize: max
... crossAxisAlignment: center
... verticalDirection: down
◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤
════════════════════════════════════════════════════════════════════════════════════════════════════
我对颤振/飞镖非常新手(从字面上看是昨天开始的),这已经让我难住了。
这是一张快照:https://i.stack.imgur.com/qlDMZ.png
【问题讨论】:
你能把 main.dart 贴在这里吗?如需快速回答,请尝试使用Scaffold
小部件包装 Container
请在您使用 Listicle() 的地方添加您的代码
【参考方案1】:
这行得通:
import 'package:flutter/material.dart';
final Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main()
runApp(MyApp());
class MyApp extends StatelessWidget
@override
Widget build(BuildContext context)
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: Listicle(),
),
),
);
class Listicle extends StatefulWidget
@override
ListicleState createState() => ListicleState();
class ListicleState extends State<Listicle>
final List<String> entries = <String>['A', 'B', 'C', 'D', 'E', 'F','G','H'];
final List<int> colorCodes = <int>[600, 500, 100, 600, 500, 100,100,100];
@override
Widget build(BuildContext context)
return Container(
child: ListView.builder(
primary: false,
shrinkWrap: true,
padding: const EdgeInsets.all(8),
itemCount: entries.length,
itemBuilder: (BuildContext context, int index)
return Container(
height: 100,
color: Colors.amber[colorCodes[index]],
child: Center(child: Text('Entry $entries[index]')),
);
)
);
【讨论】:
【参考方案2】:发生错误是因为ListView
在主轴方向扩展至最大尺寸。
解决方案:
正如@rocigno-medeiros 在评论中正确所说的那样
通过将您的代码包装在Scaffold
中使其工作,因为Scaffold
限制了ListView
的高度。
class Listicle extends StatefulWidget
@override
ListicleState createState() => ListicleState();
class ListicleState extends State<Listicle>
final List<String> entries = <String>['A', 'B', 'C', 'D', 'E', 'F'];
final List<int> colorCodes = <int>[600, 500, 100, 600, 500, 100];
@override
Widget build(BuildContext context)
return Scaffold(
body: Container(
child: ListView.builder(
primary: false,
physics: AlwaysScrollableScrollPhysics(),
shrinkWrap: true,
padding: const EdgeInsets.all(8),
itemCount: entries.length,
itemBuilder: (BuildContext context, int index)
return Container(
height: 100,
color: Colors.amber[colorCodes[index]],
child: Center(child: Text('Entry $entries[index]')),
);
,
),
),
);
如果您想要默认滚动行为,也不需要额外的滚动控制器。
更多信息和一个很好的答案here。
【讨论】:
以上是关于无法在标准 ListView 小部件上滚动;总是过度渲染 150px的主要内容,如果未能解决你的问题,请参考以下文章
Flutter - 与子小部件交互时停止 ListView 滚动
即使在使用 SingleChildScrollView() 包装 column() 后也无法在小部件上滚动
listview.builder底部溢出和使用灵活和扩展小部件的错误