'Flutter: How to add item to favourite and display favourites in listview
I am working on an e-commerce demo project that uses woocommerce for its backend. I have a product card widget for listing all products displayed in a grid or list view. This is the code for the product card
InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ProductDetails(
product: data,
productName: data.name,
)));
},
child: Column(
children: [
Stack(children: [
Center(
child: Container(
width: 160,
height: 185,
alignment: Alignment.center,
decoration: BoxDecoration(
shape: BoxShape.rectangle,
border: Border.all(color: Colors.white, width: 2),
borderRadius: BorderRadius.circular(8),
color: Colors.white,
image: DecorationImage(
image: NetworkImage(
data.images[0].src,
),
fit: BoxFit.fill,
))),
),
Positioned(
top: 7,
left: 20,
child: Visibility(
visible: data.calculateDiscount() > 0,
child: Align(
alignment: Alignment.topLeft,
child: Container(
padding: const EdgeInsets.all(5),
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(50),
),
child: Text(
'-${data.calculateDiscount()}%',
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.normal,
),
),
),
)),
),
]),
Container(
width: 100,
alignment: Alignment.center,
child: Column(
children: [
const SizedBox(
height: 3,
),
Center(
child: Text(
data.name,
maxLines: 2,
style: const TextStyle(
height: 1,
fontSize: 14,
fontWeight: FontWeight.w500,
),
textAlign: TextAlign.center,
),
),
Text(
data.type == "variable"
? (data.attributes[0].name +
": " +
data.attributes[0].options.join("-").toString())
: '₦${data.regularPrice}',
style: TextStyle(
color: data.type == "variable" ? Colors.black : Colors.red,
fontSize: data.type == "variable" ? 12 : 14,
fontWeight: FontWeight.w600,
decoration: data.type == "variable"
? TextDecoration.none
: TextDecoration.lineThrough,
),
textAlign: TextAlign.center,
),
Text(
data.type == "variable"
? '₦${data.price}'
: '₦${data.salePrice}',
style: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.w600,
),
textAlign: TextAlign.center,
),
],
),
),
],
),
);
How can I integrate add to favourite logic on this product card and display all favourites in a listview? I need to have a favourites page in my app. Thanks
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
