英文:
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.builder和Button,请在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.
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
- 移除包裹ListView.builder的带有固定高度的SizedBox小部件。
 - 移除包裹ListView.builder内Column小部件的SingleChildScrollView小部件。
 - 使用一个具有固定高度的Container小部件包裹ListView.builder。
 
Container(
  height: 500,
  child: ListView.builder(
    ...
  ),
英文:
- Remove the SizedBox widget with a fixed height that wraps the ListView.builder.
 - Remove the SingleChildScrollView widget that wraps the Column widget inside the ListView.builder.
 - Wrap the ListView.builder with a Container widget that has a fixed height.
 
Container(
  height: 500,
  child: ListView.builder(
    ...
  ),
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。



评论