为什么只有ListView.builder()中的内容不滚动? [英] Why only the content in ListView.builder() is not scrolling?

查看:294
本文介绍了为什么只有ListView.builder()中的内容不滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a screen with Text widgets and ListView, and when I try to scroll inside the ListView, it doesn't allow me to scroll.

我的身体是SingleChildScrollView,但是没有为Lis​​tView.builder提供scrollView.我试图将ListView.build包装在Expanded中,但没有解决.

My body is SingleChildScrollView but that doesn't provide scrollView for the ListView.builder. I tried to wrap the ListView.build inside Expanded, but didn't work out.

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()可滚动.

Anyone has an idea how I can make the listView.builder() scrollable.

推荐答案

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

You need to make the ListView.builder not scrollable so the SingleChildScrollView can scroll. You could achieve that by setting one of these two properties to the ListView.builder:

physics: NeverScrollableScrollPhysics()

primary: false

但是使用ListView.builder的使用方式,它将立即创建所有项目.如果要仅在滚动到屏幕上时有效地使用ListView.builder创建项目,则可以删除SingleChildScrollView并仅使用ListView.builder像这样:

But using the ListView.builder the way you're using it, it will create all the items at once. If you want to use the ListView.builder efficiently to create the items only when they're scrolled onto the screen, you could remove the SingleChildScrollView and just use ListView.builder like this:

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(...);
  }
)

这篇关于为什么只有ListView.builder()中的内容不滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆