Skip to content

Commit

Permalink
Added shapes and positions
Browse files Browse the repository at this point in the history
  • Loading branch information
yadaniyil committed Nov 5, 2018
1 parent 05d6bd3 commit 2b010ea
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 17 deletions.
4 changes: 2 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ class _MyHomePageState extends State<MyHomePage> {
BadgeIconButton(
itemCount: _counter,
badgeColor: _color,
badgeTextColor: Colors.white,
icon: _icon,
toAnimate: false,
shape: BadgeShape.card,
position: BadgePosition.bottomRight,
onPressed: () {}),
],
),
Expand Down
5 changes: 4 additions & 1 deletion lib/badges.dart
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export 'src/badge_icon_button.dart';
export 'src/badge_icon_button.dart';
export 'src/badge_position.dart';
export 'src/badge_positions.dart';
export 'src/badge_shape.dart';
35 changes: 21 additions & 14 deletions lib/src/badge_icon_button.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
library badges;

import 'package:badges/src/badge_position.dart';
import 'package:badges/src/badge_positions.dart';
import 'package:badges/src/badge_shape.dart';
import 'package:flutter/material.dart';

class BadgeIconButton extends StatefulWidget {
final VoidCallback onPressed;
final int itemCount;
final Color badgeColor;
final Color badgeTextColor;
final Icon icon;
final bool hideZeroCount;
final bool toAnimate;
final BadgePosition position;
final BadgeShape shape;
final TextStyle textStyle;

BadgeIconButton(
{Key key,
Expand All @@ -18,11 +23,16 @@ class BadgeIconButton extends StatefulWidget {
this.onPressed,
this.hideZeroCount: true,
this.badgeColor: Colors.red,
this.badgeTextColor: Colors.white,
this.toAnimate: true})
this.toAnimate: true,
this.position: BadgePosition.topRight,
this.shape: BadgeShape.circle,
this.textStyle: const TextStyle(
fontSize: 13.0,
color: Colors.white,
fontWeight: FontWeight.bold,
)})
: assert(itemCount >= 0),
assert(badgeColor != null),
assert(badgeTextColor != null),
super(key: key);

@override
Expand Down Expand Up @@ -55,10 +65,9 @@ class BadgeIconButtonState extends State<BadgeIconButton>
overflow: Overflow.visible,
children: [
widget.icon,
Positioned(
top: -8.0,
right: -3.0,
child: widget.toAnimate == true
BadgePositioned(
position: widget.position,
child: widget.toAnimate
? SlideTransition(
position: _badgePositionTween.animate(_animation),
child: _getBadge())
Expand All @@ -71,18 +80,16 @@ class BadgeIconButtonState extends State<BadgeIconButton>

Widget _getBadge() {
return Material(
type: MaterialType.circle,
type: widget.shape == BadgeShape.circle
? MaterialType.circle
: MaterialType.card,
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,
),
style: widget.textStyle,
),
));
}
Expand Down
1 change: 1 addition & 0 deletions lib/src/badge_position.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
enum BadgePosition { topLeft, topRight, center, bottomRight, bottomLeft }
27 changes: 27 additions & 0 deletions lib/src/badge_positions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import 'package:badges/src/badge_position.dart';
import 'package:flutter/widgets.dart';

class BadgePositioned extends StatelessWidget {
final Widget child;
final BadgePosition position;

const BadgePositioned({Key key, this.position, this.child}) : super(key: key);

@override
Widget build(BuildContext context) {
switch (position) {
case BadgePosition.topLeft:
return Positioned(top: -8.0, left: -3.0, child: child);
case BadgePosition.topRight:
return Positioned(top: -8.0, right: -3.0, child: child);
case BadgePosition.center:
return Positioned(child: child);
case BadgePosition.bottomLeft:
return Positioned(bottom: -8.0, left: -3.0, child: child);
case BadgePosition.bottomRight:
return Positioned(bottom: -8.0, right: -3.0, child: child);
default:
return Positioned(top: -8.0, right: -3.0, child: child);
}
}
}
1 change: 1 addition & 0 deletions lib/src/badge_shape.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
enum BadgeShape { circle, card }

0 comments on commit 2b010ea

Please sign in to comment.