Wishlist
Loading preview
Starting the Flutter app
Grid with heart toggle, Move to Cart action
import 'package:flutter/material.dart';
class WishlistPage extends StatefulWidget {
/// Called with the title of an item after it leaves the wishlist.
final ValueChanged<String>? onMoveToCart;
const WishlistPage({super.key, this.onMoveToCart});
@override
State<WishlistPage> createState() => _WishlistPageState();
}
class _WishlistPageState extends State<WishlistPage> {
final _items = [
{'title': 'Product A', 'price': '\$29.99', 'hearted': true},
{'title': 'Product B', 'price': '\$49.99', 'hearted': true},
];
void _moveToCart(Map<String, Object> item) {
final title = item['title'] as String;
setState(() => _items.remove(item));
widget.onMoveToCart?.call(title);
ScaffoldMessenger.of(context)
..hideCurrentSnackBar()
..showSnackBar(SnackBar(content: Text('$title moved to cart')));
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Wishlist')),
body: _items.isEmpty
? const Center(child: Text('Your wishlist is empty'))
: GridView.builder(
padding: const EdgeInsets.all(16),
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 0.8,
crossAxisSpacing: 16,
mainAxisSpacing: 16,
),
itemCount: _items.length,
itemBuilder: (context, i) {
final item = _items[i];
return Card(
child: Stack(
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
child: Container(
color: Colors
.primaries[i % Colors.primaries.length]
.withValues(alpha: 0.3),
),
),
Padding(
padding: const EdgeInsets.all(8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(item['title'] as String),
Text(item['price'] as String),
FilledButton(
onPressed: () => _moveToCart(item),
child: const Text('Move to Cart'),
),
],
),
),
],
),
Positioned(
top: 8,
right: 8,
child: IconButton(
tooltip: item['hearted'] == true
? 'Remove from favorites'
: 'Add to favorites',
icon: Icon(
item['hearted'] == true
? Icons.favorite
: Icons.favorite_border,
color: item['hearted'] == true ? Colors.red : null,
),
onPressed: () => setState(
() => item['hearted'] = !(item['hearted'] as bool),
),
),
),
],
),
);
},
),
);
}
}
Add it to your project
Copies the Dart source into your app, so you own it. Flutter SDK only, no extra packages.
First time? Run dart pub global activate fluttertemplates_dev, or read the CLI guide.
How it works
_items is a plain field in _WishlistPageState, not const data, so its maps can be mutated in place. A heart IconButton floats over each card through Stack and Positioned and flips hearted with setState. Unhearting only changes the icon: the product stays in the grid. Move to Cart removes the item, confirms with a SnackBar, and reports the title through onMoveToCart.
Adapting it
- Remove the entry from
_itemsin the heart’sonPressedif unhearting should delete it, ideally with an undoSnackBar. - Persist each toggle to your wishlist endpoint, since
_itemsonly lives in memory.