简体   繁体   English

在Flutter应用程序的ListView.builder中添加滚动

[英]Add Scrolling in the ListView.builder in Flutter application

I am trying to make a List view scroll able, when I googled and could not found an understandable and simple solution, I tried to make a custom scroll (example from link https://docs.flutter.io/flutter/widgets/ListView-class.html ), at the moment it is not working. 我试图使列表视图滚动成为可能,当我用Google搜索并且找不到可理解和简单的解决方案时,我试图进行自定义滚动(例如,来自链接https://docs.flutter.io/flutter/widgets/ListView的示例-class.html ),目前无法正常工作。

Here is the code: 这是代码:

CustomScrollView(
  shrinkWrap: true,
  slivers: <Widget>[
    SliverPadding(
      padding: const EdgeInsets.all(20.0),
      sliver: SliverList(
        delegate: SliverChildListDelegate(
          <Widget>[
            StreamBuilder(
            stream: Firestore.instance.collection("Items").snapshots(),
            builder: (BuildContext context, AsyncSnapshot snapshot) {
              if (snapshot.hasData) {
                return new ListView.builder(
                  padding: const EdgeInsets.only(top: 5.0),
                  scrollDirection: Axis.vertical,
                    shrinkWrap: true,
                    itemCount: snapshot.data.documents.length,

                    itemBuilder: (BuildContext context,int index) {
                      DocumentSnapshot ds = snapshot.data.documents[index];
                      return new Row(

                        textDirection: TextDirection.ltr,
                        children: <Widget>[
                          Expanded(child: Text(ds["item1"])),
                          Expanded(child: Text(ds["item2"])),
                          Expanded(child: Text(ds["price"].toString())),
                        ],
                      );
                    });
              }
              else {
                return Align(
                  alignment: FractionalOffset.bottomCenter,
                  child: CircularProgressIndicator(),
                );

              }
            },
          )
          ],
        ),
      ),
    ),
  ],
)

Below is the screenshot from emulator (Kindly node, same on the phone): 下面是模拟器的截图(亲切的节点,在手机上相同): 在此处输入图片说明

Kindly help me with pointers or sample code for scroll-able list view. 请帮助我提供可滚动列表视图的指针或示例代码。

You don't need to use a CustomScrollView. 您不需要使用CustomScrollView。 ListView is a scrolling widget itself. ListView是一个滚动小部件本身。 So you only need to create it inside your StreamBuilder. 因此,您只需要在StreamBuilder中创建它。

@override
Widget build(BuildContext context) {
  return StreamBuilder<List<int>>(
    stream: posts,
    builder: (BuildContext context, AsyncSnapshot<List<int>> snapshot) {
      if (snapshot.hasError) {
        return Text('Error: ${snapshot.error}');
      }
       switch (snapshot.connectionState) {
        case ConnectionState.waiting:
          return const Text('Loading...');
        default:
          if (snapshot.data.isEmpty) {
            return const NoContent();
          }
          return _itemList(snapshot.data);
      }
    },
  );
}

CustomScrollView is used for adding Sliver widget inside it. CustomScrollView用于在其中添加Sliver小部件。

You are wrapping a ListView inside a SliverList , which is never a good idea if they have the same scrolling direction. 您将ListView包裹在SliverList ,如果它们具有相同的滚动方向,则永远不是一个好主意。 You could either do a Column with a List.generate() generator (inefficient) or get rid of one of the ListView 's: 您可以使用List.generate()生成器(效率低下)来做一Column ,也可以摆脱ListView的其中之一:

CustomScrollView(
  shrinkWrap: true,
  slivers: <Widget>[
    StreamBuilder(
      stream: Firestore.instance.collection("Items").snapshots(),
      builder: (BuildContext context, AsyncSnapshot snapshot) {
        if (snapshot.hasData) {
          return SliverPadding(
            padding: const EdgeInsets.all(20.0),
            sliver: SliverList(
              delegate: SliverChildBuilderDelegate(
                (BuildContext context, int index) {
                  DocumentSnapshot ds = snapshot.data.documents[index];
                  return new Row(
                    textDirection: TextDirection.ltr,
                    children: <Widget>[
                      Expanded(child: Text(ds["item1"])),
                      Expanded(child: Text(ds["item2"])),
                      Expanded(child: Text(ds["price"].toString())),
                    ],
                  );
                },
                childCount: snapshot.data.documents.length,
              ),
            ),
          );
        } else {
          return SliverFillRemaining(
            child: Center(
              child: CircularProgressIndicator(),
            ),
          );
        }
      },
    ),
  ],
);

If this code snippet doesn't work swap the StreamBuilder with the CustomScrollView 如果此代码段不起作用, StreamBuilderCustomScrollView交换

ListView itself is a scroll able list so you just create it with your custom tiles. ListView本身是可滚动的列表,因此您只需使用自定义图块即可创建它。 Here is the code from my to-do list app that I used to create a list of my to-do items. 这是我的待办事项列表应用程序中的代码,用于创建待办事项列表。 Well I call this function when I have to create a list. 好了,当我必须创建一个列表时,我就调用此函数。

/*Called each time the widget is built.
* This function creates the list with all items in '_todoList'
* */
Widget _buildTodoList() {
  return new ListView.builder(
    // itemBuilder will be automatically be called as many times as it takes for the
    // list to fill up its available space, which is most likely more than the
    // number of to do items we have. So, we need to check the index is OK.
    itemBuilder: (context, index) {
      if (index < _todoList.length) {
        return _buildTodoItem(_todoList[index],index);
      }
    },
  );
}

Now this function calls a _buildTodoItem function which creates a single custom list tile. 现在,此函数调用_buildTodoItem函数,该函数创建一个自定义列表_buildTodoItem贴。

 /*Build a single List Tile*/
Widget _buildTodoItem(TodoItem todoItem,int index) {
  //return a custom build single tile for your list
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM