Skip to content

Commit

Permalink
Added toAnimate field to enable or disable animation
Browse files Browse the repository at this point in the history
  • Loading branch information
yadaniyil committed Nov 5, 2018
1 parent 8d8935d commit 05d6bd3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 27 deletions.
1 change: 1 addition & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class _MyHomePageState extends State<MyHomePage> {
badgeColor: _color,
badgeTextColor: Colors.white,
icon: _icon,
toAnimate: false,
onPressed: () {}),
],
),
Expand Down
61 changes: 34 additions & 27 deletions lib/src/badge_icon_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ class BadgeIconButton extends StatefulWidget {
final Color badgeTextColor;
final Icon icon;
final bool hideZeroCount;
final bool toAnimate;

BadgeIconButton({
Key key,
@required this.itemCount,
@required this.icon,
this.onPressed,
this.hideZeroCount: true,
this.badgeColor: Colors.red,
this.badgeTextColor: Colors.white,
}) : assert(itemCount >= 0),
BadgeIconButton(
{Key key,
@required this.itemCount,
@required this.icon,
this.onPressed,
this.hideZeroCount: true,
this.badgeColor: Colors.red,
this.badgeTextColor: Colors.white,
this.toAnimate: true})
: assert(itemCount >= 0),
assert(badgeColor != null),
assert(badgeTextColor != null),
super(key: key);
Expand Down Expand Up @@ -56,30 +58,35 @@ class BadgeIconButtonState extends State<BadgeIconButton>
Positioned(
top: -8.0,
right: -3.0,
child: SlideTransition(
position: _badgePositionTween.animate(_animation),
child: Material(
type: MaterialType.circle,
elevation: 2.0,
color: widget.badgeColor,
child: Padding(
padding: const EdgeInsets.all(5.0),
child: Text(
widget.itemCount.toString(),
style: TextStyle(
fontSize: 13.0,
color: widget.badgeTextColor,
fontWeight: FontWeight.bold,
),
),
)),
),
child: widget.toAnimate == true
? SlideTransition(
position: _badgePositionTween.animate(_animation),
child: _getBadge())
: _getBadge(),
),
],
),
onPressed: widget.onPressed);
}

Widget _getBadge() {
return Material(
type: MaterialType.circle,
elevation: 2.0,
color: widget.badgeColor,
child: Padding(
padding: const EdgeInsets.all(5.0),
child: Text(
widget.itemCount.toString(),
style: TextStyle(
fontSize: 13.0,
color: widget.badgeTextColor,
fontWeight: FontWeight.bold,
),
),
));
}

@override
void didUpdateWidget(BadgeIconButton oldWidget) {
if (widget.itemCount != oldWidget.itemCount) {
Expand Down

0 comments on commit 05d6bd3

Please sign in to comment.