Unverified Commit e8cbd444 authored by Hans Muller's avatar Hans Muller Committed by GitHub

Added Badge.isLabelVisible flag (#115292)

parent 1b23ad63
...@@ -36,6 +36,7 @@ class Badge extends StatelessWidget { ...@@ -36,6 +36,7 @@ class Badge extends StatelessWidget {
this.padding, this.padding,
this.alignment, this.alignment,
this.label, this.label,
this.isLabelVisible = true,
this.child, this.child,
}); });
...@@ -102,6 +103,12 @@ class Badge extends StatelessWidget { ...@@ -102,6 +103,12 @@ class Badge extends StatelessWidget {
/// this is a [StadiumBorder] shaped "large" badge with height [largeSize]. /// this is a [StadiumBorder] shaped "large" badge with height [largeSize].
final Widget? label; final Widget? label;
/// If false, the badge's [label] is not included.
///
/// This flag is true by default. It's intended to make it convenient
/// to create a badge that's only shown under certain conditions.
final bool isLabelVisible;
/// The widget that the badge is stacked on top of. /// The widget that the badge is stacked on top of.
/// ///
/// Typically this is an default sized [Icon] that's part of a /// Typically this is an default sized [Icon] that's part of a
...@@ -110,6 +117,10 @@ class Badge extends StatelessWidget { ...@@ -110,6 +117,10 @@ class Badge extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
if (!isLabelVisible) {
return child ?? const SizedBox();
}
final BadgeThemeData badgeTheme = BadgeTheme.of(context); final BadgeThemeData badgeTheme = BadgeTheme.of(context);
final BadgeThemeData defaults = _BadgeDefaultsM3(context); final BadgeThemeData defaults = _BadgeDefaultsM3(context);
final double effectiveSmallSize = smallSize ?? badgeTheme.smallSize ?? defaults.smallSize!; final double effectiveSmallSize = smallSize ?? badgeTheme.smallSize ?? defaults.smallSize!;
......
...@@ -20,7 +20,7 @@ void main() { ...@@ -20,7 +20,7 @@ void main() {
alignment: Alignment.topLeft, alignment: Alignment.topLeft,
child: Builder( child: Builder(
builder: (BuildContext context) { builder: (BuildContext context) {
// theme.textTtheme is updated when the MaterialApp is built. // theme.textTheme is updated when the MaterialApp is built.
theme = Theme.of(context); theme = Theme.of(context);
return const Badge( return const Badge(
label: Text('0'), label: Text('0'),
...@@ -71,7 +71,7 @@ void main() { ...@@ -71,7 +71,7 @@ void main() {
alignment: Alignment.topLeft, alignment: Alignment.topLeft,
child: Builder( child: Builder(
builder: (BuildContext context) { builder: (BuildContext context) {
// theme.textTtheme is updated when the MaterialApp is built. // theme.textTheme is updated when the MaterialApp is built.
theme = Theme.of(context); theme = Theme.of(context);
return const Badge( return const Badge(
label: Text('0'), label: Text('0'),
...@@ -202,5 +202,27 @@ void main() { ...@@ -202,5 +202,27 @@ void main() {
expect(tester.renderObject(find.byType(Badge)), paints..rrect(color: black)); expect(tester.renderObject(find.byType(Badge)), paints..rrect(color: black));
}); });
testWidgets('isLabelVisible', (WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
theme: ThemeData.light(useMaterial3: true),
home: const Align(
alignment: Alignment.topLeft,
child: Badge(
label: Text('0'),
isLabelVisible: false,
child: Icon(Icons.add),
),
),
),
);
expect(find.text('0'), findsNothing);
expect(find.byType(Icon), findsOneWidget);
expect(tester.getSize(find.byType(Badge)), const Size(24, 24)); // default Icon size
expect(tester.getTopLeft(find.byType(Badge)), Offset.zero);
final RenderBox box = tester.renderObject(find.byType(Badge));
expect(box, isNot(paints..rrect()));
});
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment