Ant*_*nov 4 flutter flutter-layout flutter-listview flutter-widget
我有一个带有 Text 小部件和 ListView 的屏幕,当我尝试在 ListView 内滚动时,它不允许我滚动。
我的身体是 SingleChildScrollView 但它没有为 ListView.builder 提供滚动视图。我试图将 ListView.build 包装在 Expanded 中,但没有成功。
class HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: new AppBar(
title: Text("MyBar"),
centerTitle: true,
),
body: SingleChildScrollView(child: Column(
children: <Widget>[
Text(
"Information"),
Container(
child: ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemCount: widget.match.players.length,
itemBuilder: (context, index) {
return Column(
children: <Widget>[
Card(
child: ListTile(
leading: CircleAvatar(
radius: 30.0,
foregroundColor:
Theme.of(context).primaryColor,
backgroundColor: Colors.grey,
backgroundImage:
CachedNetworkImageProvider("url"),
),
title: new Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: <Widget>[
new Text("Some text",
style: new TextStyle(
fontWeight: FontWeight.bold),
),
new Text(
"Some text",
style: new TextStyle(
fontWeight: FontWeight.bold),
),
]),
))
],
);
}))
],
)));}
}
Run Code Online (Sandbox Code Playgroud)
任何人都知道如何使 listView.builder() 可滚动。
Pab*_*era 22
您需要使ListView.builder不可滚动SingleChildScrollView才能滚动。您可以通过将这两个属性之一设置为ListView.builder:
physics: NeverScrollableScrollPhysics()
Run Code Online (Sandbox Code Playgroud)
或者
primary: false
Run Code Online (Sandbox Code Playgroud)
但是使用ListView.builder您使用它的方式,它将一次创建所有项目。如果您想ListView.builder有效地使用仅当它们滚动到屏幕上时创建项目,您可以删除SingleChildScrollView并ListView.builder像这样使用:
ListView.builder(
itemCount: widget.match.players.length + 1,
itemBuilder: (context, index) {
if (index == 0) {
return Text("Information");
}
int listIndex = index - 1;
// then you could use: widget.match.players[listIndex];
return Column(...);
}
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4624 次 |
| 最近记录: |