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

问题描述 投票:0回答:1

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

我的身体是SingleChildScrollView,但不为ListView.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()可滚动。

listview flutter scrollable
1个回答
0
投票

您需要使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(...);
  }
)
© www.soinside.com 2019 - 2024. All rights reserved.