import 'package:diplomaticquarterapp/config/shared_pref_kay.dart'; import 'package:diplomaticquarterapp/core/model/pharmacies/PharmacyProduct.dart'; import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart'; import 'package:diplomaticquarterapp/pages/pharmacies/screens/product-details/product-detail.dart'; import 'package:diplomaticquarterapp/uitl/app_shared_preferences.dart'; import 'package:diplomaticquarterapp/widgets/data_display/text.dart'; import 'package:diplomaticquarterapp/widgets/others/StarRating.dart'; import 'package:diplomaticquarterapp/widgets/transitions/fade_page.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; class ProductTileItem extends StatelessWidget { final AppSharedPreferences sharedPref = AppSharedPreferences(); final PharmacyProduct item; final double itemHeight; ProductTileItem(this.item, this.itemHeight); void productOnClick(BuildContext ctx) { _saveLastVisitProducts(); Navigator.push(ctx, FadePage(page: ProductDetailPage(item))); } void _saveLastVisitProducts() async { String lastVisited = ""; bool isIdExist = false; List items = []; if (await this.sharedPref.getString(PHARMACY_LAST_VISITED_PRODUCTS) != null) { lastVisited = await this.sharedPref.getString(PHARMACY_LAST_VISITED_PRODUCTS); lastVisited.split(",").forEach((id) { if (id == item.id) { isIdExist = true; } items.add(id); }); } if(items.length >= 15){ items.removeAt(0); // lastVisited = lastVisited.replaceFirst(RegExp('$itemToRemove'), ''); } lastVisited = ""; for(int i = items.length - 1; i >= 0; i--){ if (lastVisited == "") { // it means there is no lastVisited yet lastVisited = "${items[i]}"; } else { // there is lastVisited id's and this product id is not found lastVisited += ",${items[i]}"; } } if (!isIdExist) { if (lastVisited == "") { // it means there is no lastVisited yet lastVisited = "${item.id}"; } else { // there is lastVisited id's and this product id is not found lastVisited += ",${item.id}"; } } sharedPref.setString(PHARMACY_LAST_VISITED_PRODUCTS, lastVisited); } @override Widget build(BuildContext context) { ProjectViewModel projectProvider = Provider.of(context); return InkWell( onTap: () => productOnClick(context), splashColor: Theme.of(context).primaryColor, child: Container( margin: EdgeInsets.all(7), decoration: BoxDecoration( border:Border.all(color: Colors.grey.shade300,width: 0.5), borderRadius: BorderRadius.circular(8) ), padding: EdgeInsets.symmetric(horizontal: 4), width: MediaQuery.of(context).size.width / 2.8, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Stack( children: [ Container( margin: EdgeInsets.fromLTRB(0, 0, 0, 0), alignment: Alignment.center, child: (item.images != null && item.images.length > 0) ? Image.network( item.images[0].src, fit: BoxFit.cover, height: itemHeight / 2, ) : Image.asset( "assets/images/no_image.png", fit: BoxFit.cover, height: itemHeight / 2, ), ), // Container( // width: item.rxMessage != null // ? MediaQuery.of(context).size.width / 5 // : 0, // padding: EdgeInsets.all(4), // decoration: BoxDecoration( // color: Color(0xffb23838), // borderRadius: // BorderRadius.only(topLeft: Radius.circular(6)), // ), // child: item.rxMessage != null // ? Texts( // projectProvider.isArabic // ? item.rxMessagen // : item.rxMessage, // color: Colors.white, // regular: true, // fontSize: 10, // fontWeight: FontWeight.w400, // ) // : Texts(""), // ) ], ), Padding( padding: EdgeInsets.fromLTRB(1,1,1,1), child: Container( width: item.rxMessage != null ? MediaQuery.of(context).size.width / 1.0 : 0, padding: EdgeInsets.fromLTRB(8,2,8,2), decoration: BoxDecoration( color: Color(0xffb23838), ), child: item.rxMessage != null ? Texts( projectProvider.isArabic ? item.rxMessagen : item.rxMessage, color: Colors.white, regular: true, fontSize: 10, fontWeight: FontWeight.w400, ) : Texts(""), ), ), // SizedBox(height: 4,), Padding( padding: const EdgeInsets.symmetric(horizontal: 4.0), child: Texts( projectProvider.isArabic ? item.namen : item.name, regular: true, fontSize: 12, fontWeight: FontWeight.w400, ), ), Expanded( child: Container( margin: EdgeInsets.symmetric( horizontal: 4, vertical: 0, ), child: Column( crossAxisAlignment: CrossAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.end, children: [ Padding( padding: const EdgeInsets.only(top: 2, bottom: 2), child: Texts( "SAR ${item.price}", fontWeight: FontWeight.w600, fontSize: 14, ), ), Row( children: [ Expanded( child: StarRating( totalAverage: item.approvedTotalReviews > 0 ? (item.approvedRatingSum.toDouble() / item.approvedTotalReviews.toDouble()) .toDouble() : 0, forceStars: true), ), ], ), ], ), ), ), SizedBox(height: 5,), ], ), ), ); } }