首页 > 解决方案 > 有没有办法在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: () {

                    }),
              ),
            );
          },
        );
      }
    }

fsdb

标签: flutter

解决方案


推荐阅读