Skip to content

Commit

Permalink
Issue: 4seer#81: 7.1-Listing favorite items from local db.
Browse files Browse the repository at this point in the history
  • Loading branch information
ptyagicodecamp committed Apr 22, 2020
1 parent 67f6b1c commit a4b69e6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 38 deletions.
3 changes: 2 additions & 1 deletion lib/data/fake_model/fake_product_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ class FakeProductRepository extends ProductRepository with FavoritesRepository {
SortRules sortRules = const SortRules(),
FilterRules filterRules}) async {
return (await _getFavorites())
.map((value) => FavoriteProduct(value, HashMap()));
.map((value) => FavoriteProduct(value, HashMap()))
.toList();
}

@override
Expand Down
53 changes: 19 additions & 34 deletions lib/presentation/features/favorites/views/favourites_list_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:openflutterecommerce/config/routes.dart';
import 'package:openflutterecommerce/config/theme.dart';
import 'package:openflutterecommerce/presentation/features/favorites/favorites.dart';
import 'package:openflutterecommerce/presentation/features/product_details/product_screen.dart';
import 'package:openflutterecommerce/presentation/widgets/data_driven/blank_product_list_item.dart';
import 'package:openflutterecommerce/presentation/widgets/extensions/product_view.dart';

class FavoritesListView extends StatelessWidget {
Expand All @@ -18,38 +16,25 @@ class FavoritesListView extends StatelessWidget {
builder: (context, state) {
return SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
if (state.data == null) {
return Padding(
padding: EdgeInsets.symmetric(horizontal: AppSizes.sidePadding),
child: BlankProductListItem(),
);
} else {
return Padding(
padding:
EdgeInsets.symmetric(horizontal: AppSizes.sidePadding),
child: state.data[index].getListView(
context: context,
showProductInfo: () {
Navigator.of(context).pushNamed(
OpenFlutterEcommerceRoutes.product,
arguments: ProductDetailsParameters(
state.data[index].product.id,
selectedAttributes:
state.data[index].favoriteForm));
},
onRemoveFromFavorites: () {
BlocProvider.of<FavouriteBloc>(context).add(
RemoveFromFavoriteEvent(
state.data[index].product.id));
},
onAddToCart: () {
BlocProvider.of<FavouriteBloc>(context)
.add(AddToCartEvent(state.data[index].product.id));
},
));
}
},
(BuildContext context, int index) => state.data[index].getListView(
context: context,
showProductInfo: () {
Navigator.of(context).pushNamed(
OpenFlutterEcommerceRoutes.product,
arguments: ProductDetailsParameters(
state.data[index].product.id,
selectedAttributes: state.data[index].favoriteForm));
},
onRemoveFromFavorites: () {
BlocProvider.of<FavouriteBloc>(context)
.add(RemoveFromFavoriteEvent(state.data[index].product.id));
},
onAddToCart: () {
BlocProvider.of<FavouriteBloc>(context)
.add(AddToCartEvent(state.data[index].product.id));
},
),
childCount: state.data != null ? state.data.length : 0,
),
);
});
Expand Down
8 changes: 5 additions & 3 deletions lib/presentation/widgets/extensions/product_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ extension View on Product {

Widget _getFavoritesButton(VoidCallback onFavoritesClick) {
return FloatingActionButton(
heroTag: Random().nextInt(
1000000), //TODO make sure that there is only one product with specified id on screen and use it as a tag
heroTag: title +
Random()
.nextInt(1000000)
.toString(), //TODO make sure that there is only one product with specified id on screen and use it as a tag
mini: true,
backgroundColor: AppColors.white,
onPressed: onFavoritesClick,
Expand All @@ -99,7 +101,7 @@ extension View on Product {
return Text(
price != null ? '\$' + price.toStringAsFixed(0) : '',
style: _theme.textTheme.display3.copyWith(
decoration: discountPrice!=null && discountPrice > 0
decoration: discountPrice != null && discountPrice > 0
? TextDecoration.lineThrough
: TextDecoration.none,
),
Expand Down

0 comments on commit a4b69e6

Please sign in to comment.