为什么只有ListView.builder()中的内容不滚动?

时间:2019-10-31 09:45:43

标签: flutter flutter-layout flutter-listview flutter-widget

我有一个带有文本小部件和ListView的屏幕,当我尝试在ListView中滚动时,它不允许我滚动。

我的身体是SingleChildScrollView,但是没有为Lis​​tView.builder提供scrollView。我试图将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),
                                  ),
                                ]),

                          ))
                        ],
                      );
                    }))
          ],
        )));}
  } 

任何人都知道如何使listView.builder()可滚动。

1 个答案:

答案 0 :(得分:2)

您需要使ListView.builder不可滚动,以便SingleChildScrollView可以滚动。您可以通过将以下两个属性之一设置为ListView.builder来实现:

physics: NeverScrollableScrollPhysics()

primary: false

但是使用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(...);
  }
)