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

时间:2019-01-10 17:00:55

标签: android firebase flutter google-cloud-firestore

我正在尝试使“列表”视图滚动成为可能,当我在Google上搜索并且找不到可理解和简单的解决方案时,我试图做出一个自定义滚动(例如,来自链接https://docs.flutter.io/flutter/widgets/ListView-class.html的滚动),不起作用。

代码如下:

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(),
                );

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

下面是模拟器的屏幕截图(亲切的节点,在手机上相同): enter image description here

请提供可滚动列表视图的指针或示例代码帮助我。

3 个答案:

答案 0 :(得分:0)

您不需要使用CustomScrollView。 ListView是一个滚动小部件本身。因此,您只需要在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用于在其中添加Sliver小部件。

答案 1 :(得分:0)

您正在将ListView包裹在SliverList内,如果它们具有相同的滚动方向,则永远不是一个好主意。您可以使用Column生成器(效率低下)来生成List.generate(),也可以摆脱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(),
            ),
          );
        }
      },
    ),
  ],
);

如果此代码段不起作用,请将StreamBuilderCustomScrollView交换

答案 2 :(得分:0)

ListView本身是一个可滚动列表,因此您只需使用自定义图块即可创建它。这是我的待办事项列表应用程序中的代码,用于创建待办事项列表。 好了,当我必须创建一个列表时,我就调用此函数。

/*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);
      }
    },
  );
}

现在,此函数调用_buildTodoItem函数,该函数创建单个自定义列表图块。

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