【问题标题】:ItemList in an ItemList not scrollingItemList 中的 ItemList 不滚动
【发布时间】:2021-01-31 05:49:26
【问题描述】:

我有一个嵌套的 ItemList,如下所示:

SafeArea(
  child: ListView(
    padding: EdgeInsets.zero,
    children: <Widget>[
      ListTile(
        title: Text('Placeholder'),
      ),
      ListView.builder(
        shrinkWrap: true,
        itemCount: itemList.length,
        itemBuilder: (BuildContext context, int index) {
          final itemData = itemList[index];
          return Card(
            child: ListTile(
              title: Container(
                width: MediaQuery.of(context).size.width,
                child: Text(
                  itemData.text,
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  style: Theme.of(context).textTheme.headline5,
                ),
              ),
            ),
          );
        },
      ),
    ],
  ),
),

按住直接放置在第一个列表中的项目时,我可以上下滚动,这样做也会从嵌套列表中移动项目。 (想要的行为)

当在子列表(带有构建器的那个)中保存项目时,什么都不会移动。

我想让所有项目在按住滚动时移动,我该怎么做?

有没有办法在没有 ListView.builder 的情况下构建列表,还是我错过了一些 ListView 参数?

【问题讨论】:

    标签: flutter listview flutter-layout


    【解决方案1】:

    您可以使用Listviewphysics 参数来获得预期的结果。

    SafeArea(
        child: ListView(
          physics: AlwaysScrollableScrollPhysics(), // add this
          padding: EdgeInsets.zero,
          children: <Widget>[
            ListTile(
              title: Text('Placeholder'),
            ),
            ListView.builder(
              physics: NeverScrollableScrollPhysics(), // and this
              shrinkWrap: true,
              itemCount: itemList.length,
              itemBuilder: (BuildContext context, int index) {
                final itemData = itemList[index];
                return Card(
                  child: ListTile(
                    title: Container(
                      width: MediaQuery.of(context).size.width,
                      child: Text(
                        itemData.text,
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: Theme.of(context).textTheme.headline5,
                      ),
                    ),
                  ),
                );
              },
            ),
          ],
        ),
      )
    

    现在您的列表应该可以滚动了。

    【讨论】:

    • 完美运行,非常感谢。我对物理学一无所知
    猜你喜欢
    • 1970-01-01
    • 2018-12-05
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-06
    • 1970-01-01
    • 2011-04-06
    相关资源
    最近更新 更多