我的应用程序主页上有一个FutureBuilder,可以在将来完成后返回SliverFixedExtentList。看起来像这样:
class HomeScreen extends StatelessWidget {
Future<QuerySnapshot> getQuerySnapshot(path){
return Firestore.instance.collection(path).orderBy('date').getDocuments();
}
@override
Widget build(BuildContext context) {
final List<DocumentSnapshot> _documents = List<DocumentSnapshot>();
var uid = Provider.of<UserState>(context).firebaseUser.uid;
return FutureBuilder(
future: getQuerySnapshot('eatenFoods/'+ uid +'/food'),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot){
Widget child;
if (snapshot.hasData){
_documents.clear();
snapshot.data.documents.forEach((doc)=> _documents.add(doc));
child = SliverFixedExtentList(
itemExtent: 150,
delegate: new SliverChildBuilderDelegate(
(context, index) => FoodItemCard(_documents[index]),
childCount: _documents.length,
),
);
}else {
child = SliverToBoxAdapter(child: CircularProgressIndicator());
}
return Scaffold(
//some widgets
body: CustomScrollView(
slivers: <Widget> [
SliverAppBar()
child, //<-- here is the SliverFixedExtentList
]
)
将来会从数据库中获取文档列表,然后依次将它们传递给帮助程序类,该帮助程序类会将数据包装在Card中。问题在于此实现将最新文档放在底部,而我希望它们位于顶部。我可以更改从数据库Firestore.instance.collection(path).orderBy('date', descending: true).getDocuments();
中检索文档的顺序,但是由于每次返回页面时列表都不会完全重建,因此除非重新构建应用程序,否则不会显示新数据。我还尝试在自定义滚动视图中设置reverse: true
,但这会反转所有内容,包括SliverAppBar。
是否有某种方法可以改变SliverFixedExtentList的构建方式,从而使较高的索引位于顶部?
而不是尝试反转SliverFixedExtentList,您可以反转项目...您要做的就是在for each loop之后反转_ documents。
请注意我添加的位置_ documents = _documents.reversed.toList()
class HomeScreen extends StatelessWidget {
Future<QuerySnapshot> getQuerySnapshot(path){
return Firestore.instance.collection(path).orderBy('date').getDocuments();
}
@override
Widget build(BuildContext context) {
final List<DocumentSnapshot> _documents = List<DocumentSnapshot>();
var uid = Provider.of<UserState>(context).firebaseUser.uid;
return FutureBuilder(
future: getQuerySnapshot('eatenFoods/'+ uid +'/food'),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot){
Widget child;
if (snapshot.hasData){
_documents.clear();
snapshot.data.documents.forEach((doc)=> _documents.add(doc));
_documents = _documents.reversed.toList();
child = SliverFixedExtentList(
itemExtent: 150,
delegate: new SliverChildBuilderDelegate(
(context, index) => FoodItemCard(_documents[index]),
childCount: _documents.length,
),
);
}else {
child = SliverToBoxAdapter(child: CircularProgressIndicator());
}
return Scaffold(
//some widgets
body: CustomScrollView(
slivers: <Widget> [
SliverAppBar()
child, //<-- here is the SliverFixedExtentList
]
)