Skip to content

Instantly share code, notes, and snippets.

@iamporus
Last active July 11, 2020 14:05
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 iamporus/c293a7798726d134dfd984b875441341 to your computer and use it in GitHub Desktop.
Save iamporus/c293a7798726d134dfd984b875441341 to your computer and use it in GitHub Desktop.
Flutter Widget: FavoriteItemListTile - A Custom widget implementation to create a list tile which can be marked as favorite or not by the user.
import 'package:flutter/material.dart';
/// A custom [FavoriteItemListTile] widget.
///
/// The widget is composed on an [Text] and [Icon]. Tapping on the widget shows
/// a colored [InkWell] animation.
/// Can be used to create a list tile which can be marked as favorite or not by the user.
class FavoriteItemListTile extends StatelessWidget {
final String itemTitle;
final bool isFavorite;
final Color backgroundColor;
final VoidCallback onTap;
const FavoriteItemListTile(
{Key key,
@required this.itemTitle,
@required this.isFavorite,
this.backgroundColor = Colors.transparent,
this.onTap})
: assert(itemTitle != null),
super(key: key);
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: onTap,
child: Material(
color: Colors.transparent,
child: Container(
child: InkWell(
borderRadius: BorderRadius.circular(8),
highlightColor: backgroundColor,
splashColor: backgroundColor,
onTap: onTap ?? () {},
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Row(
children: <Widget>[
Center(
child: Text(
itemTitle,
style: TextStyle(fontSize: 20),
)),
Expanded(
child: Align(
child: Icon(
isFavorite ? Icons.favorite : Icons.favorite_border,
color: isFavorite ? Colors.red : null),
alignment: Alignment.centerRight,
))
],
),
),
),
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment