Advertisement
Guest User

Untitled

a guest
Apr 17th, 2023
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.88 KB | None | 0 0
  1. class BusinessAvailableProductsDisplay extends StatefulWidget {
  2.   const BusinessAvailableProductsDisplay(this._ownerID, {Key? key})
  3.       : super(key: key);
  4.  
  5.   final String _ownerID;
  6.  
  7.   @override
  8.   State<BusinessAvailableProductsDisplay> createState() => _BusinessAvailableProductsDisplayState();
  9. }
  10.  
  11. class _BusinessAvailableProductsDisplayState extends State<BusinessAvailableProductsDisplay> {
  12.  
  13.   bool _nextPageAvailable = true;
  14.   int _curPage = 1;
  15.   String? _lastUsedID;
  16.  
  17.   Future<CategorySelectionButton> _createProductCategoryButton(String productID) async {
  18.  
  19.     DocumentReference doc = await GetData.document('products', productID);
  20.     DocumentSnapshot snapshot = await doc.get();
  21.     String name = await snapshot.get('productName');
  22.  
  23.     String imageName = await snapshot.get('productImage');
  24.     await FirebaseAuth.instance.signInAnonymously();
  25.     String downloadSource = await FirebaseStorage.instance.ref(imageName).getDownloadURL();
  26.     Image image = Image.network(
  27.       downloadSource,
  28.       width: 30.0,
  29.       height:30.0,
  30.     );
  31.  
  32.     CategorySelectionButton newButton = CategorySelectionButton(name, image.image, ProductDisplay(productID));
  33.     return newButton;
  34.   }
  35.  
  36.   Future<List<Widget>> _getProducts(BuildContext context) async {
  37.     List<Widget> productWidgets = [];
  38.  
  39.     if (_curPage == 1) {
  40.       _lastUsedID = null;
  41.     }
  42.  
  43.     List<String> products = await FilterCollection.limitNumberByField(
  44.       'products',
  45.       'owner',
  46.       widget._ownerID,
  47.       numProductsDisplayedPerPage,
  48.       _lastUsedID,
  49.     );
  50.  
  51.     _lastUsedID = products.last;
  52.  
  53.     if (products.isEmpty) {
  54.       productWidgets.add(_noProductsFoundDisplay(context));
  55.       return productWidgets;
  56.     }
  57.  
  58.     int totalProducts = await FilterCollection.sizeOfQuery('products', 'owner', widget._ownerID);
  59.     _nextPageAvailable = totalProducts > _curPage * numProductsDisplayedPerPage;
  60.  
  61.     for (int i = (_curPage - 1) * numProductsDisplayedPerPage; i < _curPage * numProductsDisplayedPerPage && i < totalProducts; i++) {
  62.       productWidgets.add(await _createProductCategoryButton(products[i]));
  63.     }
  64.  
  65.     return productWidgets;
  66.   }
  67.  
  68.   @override
  69.   Widget build(BuildContext context) {
  70.     return FutureBuilder(
  71.       future: _getProducts(context),
  72.       builder: (context, snapshot) {
  73.         if (!snapshot.hasData && snapshot.connectionState == ConnectionState.done) {
  74.           return const Text(
  75.             'ERROR: Could not load data',
  76.           );
  77.         } else if (snapshot.hasData) {
  78.           return Column(
  79.             children: [
  80.               Row(
  81.                 mainAxisAlignment: MainAxisAlignment.spaceBetween,
  82.                 children: [
  83.                   _curPage > 1 ? LeftArrow(callback: () => setState(() {
  84.                     _curPage--;
  85.                   })) : Container(),
  86.                   _nextPageAvailable ? RightArrow(callback: () => setState(() {
  87.                     _curPage++;
  88.                   })) : Container(),
  89.                 ],
  90.               ),
  91.               Padding(
  92.                 padding: const EdgeInsets.all(10.0),
  93.                 child: Container(
  94.                   padding: const EdgeInsets.all(8.0),
  95.                   width: double.infinity,
  96.                   height: 400,
  97.                   decoration: _boxDecoration(),
  98.                   child: SingleChildScrollView(
  99.                     scrollDirection: Axis.vertical,
  100.                     child: Column(
  101.                       children: [
  102.                         SectionLabel(AppLocalizations.of(context)!.availableProducts),
  103.                         Column(
  104.                           children: snapshot.data!,
  105.                         ),
  106.                       ],
  107.                     ),
  108.                   ),
  109.                 ),
  110.               ),
  111.             ],
  112.           );
  113.         } else {
  114.           return const CircularProgressIndicator();
  115.         }
  116.       }
  117.     );
  118.   }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement