Android 在颤振应用程序的ListView.builder中添加滚动

Android 在颤振应用程序的ListView.builder中添加滚动,android,firebase,flutter,google-cloud-firestore,Android,Firebase,Flutter,Google Cloud Firestore,我试图使列表视图滚动,当我谷歌,无法找到一个可以理解的简单的解决方案,我试图使自定义滚动(从链接的例子),目前它不工作 代码如下: CustomScrollView( shrinkWrap: true, slivers: <Widget>[ SliverPadding( padding: const EdgeInsets.all(20.0), sliver: SliverList( delegate: SliverChildLi

我试图使列表视图滚动,当我谷歌,无法找到一个可以理解的简单的解决方案,我试图使自定义滚动(从链接的例子),目前它不工作

代码如下:

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

              }
            },
          )
          ],
        ),
      ),
    ),
  ],
)
CustomScrollView(
收缩膜:对,
条子:[
填缝料(
填充:常数边集全部(20.0),
银条:银条列表(
委托:SliverChildListDelegate(
[
StreamBuilder(
流:Firestore.instance.collection(“Items”).snapshots(),
生成器:(BuildContext上下文,异步快照){
if(snapshot.hasData){
返回新的ListView.builder(
填充:仅限常量边集(顶部:5.0),
滚动方向:轴垂直,
收缩膜:对,
itemCount:snapshot.data.documents.length,
itemBuilder:(构建上下文,int索引){
DocumentSnapshot ds=snapshot.data.documents[索引];
返回新行(
textDirection:textDirection.ltr,
儿童:[
扩展(子项:文本(ds[“item1]”)),
扩展(子项:文本(ds[“item2]”)),
扩展(子项:文本(ds[“price”].toString()),
],
);
});
}
否则{
返回对齐(
对齐:分馏Loffset.bottomCenter,
子对象:CircularProgressIndicator(),
);
}
},
)
],
),
),
),
],
)
以下是emulator的屏幕截图(在手机上相同):


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

您不需要使用自定义滚动视图。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);
      }
    },
  );
}
@覆盖
小部件构建(构建上下文){
返回流生成器(
流:员额,
生成器:(BuildContext上下文,异步快照){
if(snapshot.hasError){
返回文本('Error:${snapshot.Error}');
}
交换机(快照.连接状态){
案例连接状态。正在等待:
返回常量文本('Loading…');
违约:
if(snapshot.data.isEmpty){
return const NoContent();
}
返回_itemList(snapshot.data);
}
},
);
}

CustomScrollView用于在其中添加小部件

您将
列表视图
包装在
滑动列表
中,如果它们具有相同的滚动方向,这绝不是一个好主意。您可以使用
List.generate()
generator(低效)创建
,也可以删除
列表视图中的一个:

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(),
            ),
          );
        }
      },
    ),
  ],
);
CustomScrollView(
收缩膜:对,
条子:[
StreamBuilder(
流:Firestore.instance.collection(“Items”).snapshots(),
生成器:(BuildContext上下文,异步快照){
if(snapshot.hasData){
回程轧光(
填充:常数边集全部(20.0),
银条:银条列表(
代表:SliverChildBuilderDelegate(
(BuildContext上下文,int索引){
DocumentSnapshot ds=snapshot.data.documents[索引];
返回新行(
textDirection:textDirection.ltr,
儿童:[
扩展(子项:文本(ds[“item1]”)),
扩展(子项:文本(ds[“item2]”)),
扩展(子项:文本(ds[“price”].toString()),
],
);
},
childCount:snapshot.data.documents.length,
),
),
);
}否则{
剩余回油量(
儿童:中心(
子对象:CircularProgressIndicator(),
),
);
}
},
),
],
);

如果此代码段不起作用,请将
StreamBuilder
CustomScrollView

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

您只是想创建一个可以向右滚动的列表吗?是的,我必须使用ListView生成器。如果您想要简单的可滚动列表,只需使用
ListView.builder
-您不需要其他任何东西,例如,请参见使用customscrollview的StreamBuilder是什么意思将customscrollview放入StreamBuilder的build函数中。但只有当它不这样工作时才这样做!
 /*Build a single List Tile*/
Widget _buildTodoItem(TodoItem todoItem,int index) {
  //return a custom build single tile for your list
}