2020-05-06 16:52:35 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
/// Display an icon button with a badge
|
|
|
|
///
|
|
|
|
/// Based from Medium article called "Notification Badge in Flutter"
|
|
|
|
///
|
|
|
|
/// @author Pierre Hubert
|
|
|
|
|
|
|
|
class IconButtonWithBadge extends StatelessWidget {
|
|
|
|
final Widget icon;
|
|
|
|
final void Function() onPressed;
|
|
|
|
final int number;
|
2020-05-07 11:03:08 +00:00
|
|
|
final bool active;
|
2020-05-06 16:52:35 +00:00
|
|
|
|
|
|
|
const IconButtonWithBadge({
|
2022-03-10 18:39:57 +00:00
|
|
|
Key? key,
|
|
|
|
required this.icon,
|
|
|
|
required this.onPressed,
|
2020-05-06 16:52:35 +00:00
|
|
|
this.number = 0,
|
2020-05-07 11:03:08 +00:00
|
|
|
this.active = false,
|
2020-05-06 16:52:35 +00:00
|
|
|
}) : assert(icon != null),
|
|
|
|
assert(number != null),
|
2020-05-07 11:03:08 +00:00
|
|
|
assert(active != null),
|
2020-05-06 16:52:35 +00:00
|
|
|
super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2020-05-07 11:03:08 +00:00
|
|
|
return Container(
|
|
|
|
color: active ? Color(0x55000000) : null,
|
|
|
|
child: Stack(
|
|
|
|
alignment: AlignmentDirectional.center,
|
|
|
|
children: <Widget>[
|
|
|
|
IconButton(icon: icon, onPressed: onPressed),
|
|
|
|
number != 0
|
|
|
|
? Positioned(
|
|
|
|
right: 6,
|
|
|
|
top: 6,
|
|
|
|
child: Container(
|
|
|
|
padding: EdgeInsets.all(2),
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
color: Colors.red,
|
|
|
|
borderRadius: BorderRadius.circular(6),
|
|
|
|
),
|
|
|
|
constraints: BoxConstraints(minWidth: 14, minHeight: 14),
|
|
|
|
child: Text(
|
|
|
|
"$number",
|
|
|
|
style: TextStyle(color: Colors.white, fontSize: 12),
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
),
|
|
|
|
))
|
|
|
|
: Container()
|
|
|
|
],
|
|
|
|
),
|
2020-05-06 16:52:35 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|