mercredi 24 juin 2020

Flutter - Filter out the display items according to Firebase keys, and in descending order

I am trying to create a Flutter portfolio sorted by "categories" with flat buttons. When one click on the categories, the related projects will be filtered and displayed accordingly, in descending order based on the timestamp.

I have succeeded in displaying the projects in descending order, however have been unable to display my projects by the key value.

Code for the "categories" flat buttons:

class PortfolioCategories extends StatefulWidget {
  @override
  _PortfolioCategoriesState createState() => new _PortfolioCategoriesState();
}

class _PortfolioCategoriesState extends State<PortfolioCategories> {
  void _select(CategoryChoice category) {
    setState(() {
    PortfolioRow.itemBuilder(context, category.categoryIndex);
    });
  }

class CategoryChoice {
  const CategoryChoice({this.category, this.categoryIndex});

  final String category;
  final String categoryIndex; //value of 'category' key in Firebase
}

const List<CategoryChoice> categories = const <CategoryChoice>[
  const CategoryChoice(category: 'ALL', categoryIndex: 'Category_ALL'),
  const CategoryChoice(
      category: 'MULTIMEDIA DESIGN', categoryIndex: 'Category_1'),
  const CategoryChoice(category: 'CURATORIAL', categoryIndex: 'Category_2'),
  const CategoryChoice(category: 'ART', categoryIndex: 'Category_3'),
];

Code for my portfolio:

class PortfolioRow extends StatelessWidget {
  const PortfolioRow({Key key, this.category}) : super(key: key);

  final CategoryChoice category;
  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
      stream: Firestore.instance
          .collection('portfolio')
          .orderBy('createdAt', descending: true)
          .snapshots(),
      builder: (context, snapshot) {
        if (!snapshot.hasData) return Text("Loading...");
        return GridView.builder(
            physics: ScrollPhysics(),
            shrinkWrap: true,
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 3,
            ),
            itemCount: snapshot.data.documents.length,
            itemBuilder: (context, index) =>
            portfolioContainer(context, snapshot.data.documents[index])); //portfolioContainer is the portfolio projects that will be displayed
      },
    );
  }
}



Aucun commentaire:

Enregistrer un commentaire