Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BusinessAvailableProductsDisplay extends StatefulWidget {
- const BusinessAvailableProductsDisplay(this._ownerID, {Key? key})
- : super(key: key);
- final String _ownerID;
- @override
- State<BusinessAvailableProductsDisplay> createState() => _BusinessAvailableProductsDisplayState();
- }
- class _BusinessAvailableProductsDisplayState extends State<BusinessAvailableProductsDisplay> {
- bool _nextPageAvailable = true;
- int _curPage = 1;
- String? _lastUsedID;
- Future<CategorySelectionButton> _createProductCategoryButton(String productID) async {
- DocumentReference doc = await GetData.document('products', productID);
- DocumentSnapshot snapshot = await doc.get();
- String name = await snapshot.get('productName');
- String imageName = await snapshot.get('productImage');
- await FirebaseAuth.instance.signInAnonymously();
- String downloadSource = await FirebaseStorage.instance.ref(imageName).getDownloadURL();
- Image image = Image.network(
- downloadSource,
- width: 30.0,
- height:30.0,
- );
- CategorySelectionButton newButton = CategorySelectionButton(name, image.image, ProductDisplay(productID));
- return newButton;
- }
- Future<List<Widget>> _getProducts(BuildContext context) async {
- List<Widget> productWidgets = [];
- if (_curPage == 1) {
- _lastUsedID = null;
- }
- List<String> products = await FilterCollection.limitNumberByField(
- 'products',
- 'owner',
- widget._ownerID,
- numProductsDisplayedPerPage,
- _lastUsedID,
- );
- _lastUsedID = products.last;
- if (products.isEmpty) {
- productWidgets.add(_noProductsFoundDisplay(context));
- return productWidgets;
- }
- int totalProducts = await FilterCollection.sizeOfQuery('products', 'owner', widget._ownerID);
- _nextPageAvailable = totalProducts > _curPage * numProductsDisplayedPerPage;
- for (int i = (_curPage - 1) * numProductsDisplayedPerPage; i < _curPage * numProductsDisplayedPerPage && i < totalProducts; i++) {
- productWidgets.add(await _createProductCategoryButton(products[i]));
- }
- return productWidgets;
- }
- @override
- Widget build(BuildContext context) {
- return FutureBuilder(
- future: _getProducts(context),
- builder: (context, snapshot) {
- if (!snapshot.hasData && snapshot.connectionState == ConnectionState.done) {
- return const Text(
- 'ERROR: Could not load data',
- );
- } else if (snapshot.hasData) {
- return Column(
- children: [
- Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: [
- _curPage > 1 ? LeftArrow(callback: () => setState(() {
- _curPage--;
- })) : Container(),
- _nextPageAvailable ? RightArrow(callback: () => setState(() {
- _curPage++;
- })) : Container(),
- ],
- ),
- Padding(
- padding: const EdgeInsets.all(10.0),
- child: Container(
- padding: const EdgeInsets.all(8.0),
- width: double.infinity,
- height: 400,
- decoration: _boxDecoration(),
- child: SingleChildScrollView(
- scrollDirection: Axis.vertical,
- child: Column(
- children: [
- SectionLabel(AppLocalizations.of(context)!.availableProducts),
- Column(
- children: snapshot.data!,
- ),
- ],
- ),
- ),
- ),
- ),
- ],
- );
- } else {
- return const CircularProgressIndicator();
- }
- }
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement