Skip to content

Instantly share code, notes, and snippets.

@ndungudedan
Created May 14, 2022 21:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ndungudedan/40671200c5ce6787351f11b444557fa9 to your computer and use it in GitHub Desktop.
Save ndungudedan/40671200c5ce6787351f11b444557fa9 to your computer and use it in GitHub Desktop.
class FavoritesPage extends StatelessWidget {
static String routeName = '/favorites_page';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Favorites'),
),
body: Consumer<Favorites>(
builder: (context, favorite, child) => ListView.builder(
key: ValueKey('Favorites Listview'),
shrinkWrap: true,
itemCount: favorite.items.length,
padding: const EdgeInsets.symmetric(vertical: 16),
itemBuilder: (context, index) => FavoriteItemTile(
() {
favorite.remove(index);
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('Note removed'),
duration: const Duration(seconds: 1),
));
},
favorite.items[index],
key: ObjectKey(favorite.items[index]),
),
),
),
);
}
}
class FavoriteItemTile extends StatelessWidget {
final Note note;
final Function() onPressed;
FavoriteItemTile(this.onPressed, this.note, {Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Builder(builder: (context) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: ListTile(
title: Text(
note.title,
),
subtitle: Text(note.description),
trailing: IconButton(onPressed: onPressed, icon: Icon(Icons.delete)),
),
);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment