flutter - 有没有办法在flutter和firestore中创建类别
问题描述
我正在为我的商店应用程序创建类别部分,与 firestore 一起颤动,我已经能够在列表视图中检索所有类别,但我想知道当我点击它时如何检索每个类别集合。谢谢
这是为changeNotifier和firestore查询编写的代码
class CategoryNotifier with ChangeNotifier {
List<Category> _categoryList = [];
Category _currentCategory;
UnmodifiableListView<Category> get categoryList => UnmodifiableListView(_categoryList);
Category get currentCategory => _currentCategory;
set categoryList(List<Category> categoryList){
_categoryList = categoryList;
notifyListeners();
}
set currentCategory(Category category){
_currentCategory = category;
notifyListeners();
}
}
getCategory(CategoryNotifier categoryNotifier)async{
QuerySnapshot snapshot = await Firestore
.instance.collection("Categories")
.getDocuments();
List<Category> _categoryList = [];
snapshot.documents.forEach((document){
Category category = Category.fromMap(document.data);
_categoryList.add(category);
});
categoryNotifier.categoryList = _categoryList;
}
这是在水平列表视图中从 firestore 中检索所有类别的代码
class HorizontalCategoryWidget extends StatelessWidget {
const HorizontalCategoryWidget({
Key key,
@required this.categoryNotifier
}) : super(key: key);
final CategoryNotifier categoryNotifier;
@override
Widget build(BuildContext context) {
return ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemCount: categoryNotifier.categoryList.length,
itemBuilder: (BuildContext context, int index) {
return Padding(
padding: const EdgeInsets.all(4.0),
child: Container(
height: 100,
width: 80,
child: GestureDetector(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
padding: EdgeInsets.only(top: 4),
height: 40,
width: 80.0,
child: FadeInImage(
placeholder: AssetImage('assets/tsplaceholder.png'),
image: NetworkImage( categoryNotifier.categoryList[index].icon),
fit: BoxFit.contain,
),
),
SizedBox(height: 4,),
Text(
categoryNotifier.categoryList[index].categoryName,
style: TextStyle(fontSize: 12,fontWeight: FontWeight.bold),
maxLines: 1,
textAlign: TextAlign.center,
overflow: TextOverflow.ellipsis,
),
],
),
onTap: () {
}),
),
);
},
);
}
}
解决方案
推荐阅读
- java - 如何在 gradle 构建中有许多任务配置?
- python - 为 Ray Actor 函数实现缓存
- node.js - 如何创建文件流并异步写入流?
- javascript - 为什么 js 中的滑块不适用于所有元素?
- serilog - 如何在 Serilog 中将对象序列化为字符串?
- ruby-on-rails - 如何在 Ruby 中实现 JWS (RFC 7797) 签名验证?
- javascript - 如何从条带验证我的 api 密钥?
- javascript - 在固定网格区域内适合可变内容(文本或 div)
- python - Python numpy 从训练数据集中过滤标签零的简单方法
- mobile - 是否可以将 Js Interop 与 Mobile Blazor 绑定一起使用?