如何使ListView.builder在容器中可滚动

huangapple go评论66阅读模式
英文:

How do I make the listview.builder scrollable in the container

问题

I have a list view that I want to make scrollable inside a container. However, when I scroll down and release my finger, the list locks back to the top. I tried wrapping the list view in a SingleChildScrollView, but it didn't work. Here's the relevant code:

// The container that holds the list view
SizedBox(
  height: 501,
  child: SingleChildScrollView(
    child: Column(
      children: [
        // A button to add a new item to the list
        TextButton.icon(
          onPressed: () { ... },
          icon: Icon(Icons.add),
          label: Text("Add Course"),
        ),
        // The list view that needs to be scrollable
        ListView.builder(
          shrinkWrap: true,
          itemCount: ...,
          itemBuilder: (context, index) { ... },
        ),
      ],
    ),
  ),
);
英文:

I have a list view that I want to make scrollable inside a container. However, when I scroll down and release my finger, the list locks back to the top. I tried wrapping the list view in a SingleChildScrollView, but it didn't work. Here's the relevant code:

// The container that holds the list view
SizedBox(
  height: 501,
  child: SingleChildScrollView(
    child: Column(
      children: [
        // A button to add a new item to the list
        TextButton.icon(
          onPressed: () { ... },
          icon: Icon(Icons.add),
          label: Text("Add Course"),
        ),
        // The list view that needs to be scrollable
        ListView.builder(
          shrinkWrap: true,
          itemCount: ...,
          itemBuilder: (context, index) { ... },
        ),
      ],
    ),
  ),
);

答案1

得分: 1

你两次使用了滚动。如果只想滚动ListView,请移除SingleChildScrollView。你需要停止其中一个。如果想同时滚动Listview.builderButton,请在Listview.builder上添加primary : false

SizedBox(
  height: 501,
  child: SingleChildScrollView(
    child: Column(
      children: [
        // 一个用于向列表添加新项目的按钮
        TextButton.icon(
          onPressed: () { ... },
          icon: Icon(Icons.add),
          label: Text("Add Course"),
        ),
        // 需要可以滚动的列表视图
        ListView.builder(
          shrinkWrap: true,
          primary: false,
          itemCount: ...,
          itemBuilder: (context, index) { ... },
        ),
      ],
    ),
  ),
);

你可以在这里获取关于primary属性的更多信息:
https://api.flutter.dev/flutter/widgets/ScrollView/primary.html

英文:

You are using scroll twice.

如何使ListView.builder在容器中可滚动

If you want to scroll the ListView only, remove the SingleChildScrollView. You need to stop one of them. if you want to scroll the Listview.builder and Button together, add primary : false to Listview.builder:

SizedBox(
  height: 501,
  child: SingleChildScrollView(
    child: Column(
      children: [
        // A button to add a new item to the list
        TextButton.icon(
          onPressed: () { ... },
          icon: Icon(Icons.add),
          label: Text("Add Course"),
        ),
        // The list view that needs to be scrollable
        ListView.builder(
          shrinkWrap: true,
          primary: false,
          itemCount: ...,
          itemBuilder: (context, index) { ... },
        ),
      ],
    ),
  ),
);

You can get more information about primary property here:
https://api.flutter.dev/flutter/widgets/ScrollView/primary.html

答案2

得分: 0

  1. 移除包裹ListView.builder的带有固定高度的SizedBox小部件。
  2. 移除包裹ListView.builder内Column小部件的SingleChildScrollView小部件。
  3. 使用一个具有固定高度的Container小部件包裹ListView.builder。
Container(
  height: 500,
  child: ListView.builder(
    ...
  ),
英文:
  1. Remove the SizedBox widget with a fixed height that wraps the ListView.builder.
  2. Remove the SingleChildScrollView widget that wraps the Column widget inside the ListView.builder.
  3. Wrap the ListView.builder with a Container widget that has a fixed height.
Container(
  height: 500,
  child: ListView.builder(
    ...
  ),

huangapple
  • 本文由 发表于 2023年4月11日 03:17:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75980031.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定