mirrored_repositories/shopping/lib/ui/shared/widgets/shared/widgets
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:shopping/blocs/cart.bloc.dart';
import 'package:shopping/models/cart-item.model.dart';
import 'package:shopping/models/product-list-item.model.dart';

class AddToCart extends StatelessWidget {
  final ProductListItemModel item;

  AddToCart({@required this.item});

  @override
  Widget build(BuildContext context) {
    final bloc = Provider.of<CartBloc>(context);
    var cartItem = new CartItemModel(
      id: item.id,
      price: item.price,
      image: item.image,
      quantity: 1,
      title: item.title,
    );

    if (!bloc.itemInCart(cartItem)) {
      return Container(
        width: 80,
        height: 40,
        child: FlatButton(
          color: Theme.of(context).primaryColor,
          child: const Icon(
            Icons.add_shopping_cart,
          ),
          textColor: Colors.white,
          onPressed: () {
            bloc.add(
              cartItem,
            );
            final snackBar =
                SnackBar(content: Text('${item.title} adicionado'));
            Scaffold.of(context).showSnackBar(
              snackBar,
            );
          },
        ),
      );
    } else {
      return Container(
        width: 80,
        height: 40,
        child: FlatButton(
          color: Colors.red,
          child: const Icon(
            Icons.remove_shopping_cart,
          ),
          textColor: Colors.white,
          onPressed: () {
            bloc.remove(
              cartItem,
            );
            final snackBar = SnackBar(content: Text('${item.title} removido'));
            Scaffold.of(context).showSnackBar(snackBar);
          },
        ),
      );
    }
  }
}
