skyturkish / second_hand

An application made with firebase, where users can upload a photo of their products, sell or buy them along with a description and price.
MIT License
10 stars 1 forks source link

Update FavoriteButton HomeView without Stream #46

Closed skyturkish closed 2 years ago

skyturkish commented 2 years ago

We can only stream isFavorite or not ? and wrap StreamBuilder Iconbutton,

skyturkish commented 2 years ago

we already take any change about userInformation we should just listen to user information, this is the solution.


class FavoriteIconButton extends StatefulWidget {
  const FavoriteIconButton({
    super.key,
    required this.product,
    required this.provider,
  });

  final Product product;
  final UserInformationNotifier provider;

  @override
  State<FavoriteIconButton> createState() => _FavoriteIconButtonState();
}

class _FavoriteIconButtonState extends State<FavoriteIconButton> {
  @override
  Widget build(BuildContext context) {
    final isFavorite =
        context.watch<UserInformationNotifier>().userInformation.favoriteAds.contains(widget.product.productId);
    return IconButton(
      onPressed: () {
        isFavorite
            ? widget.provider.removeFavoriteProduct(productId: widget.product.productId)
            : widget.provider.addFavoriteProduct(productId: widget.product.productId);
        setState(() {});
      },
      icon: Icon(
        Icons.favorite,
        color: isFavorite ? Colors.red : Colors.black,
      ),
    );
  }
}