Skip to content

Commit

Permalink
Added Badge.isLabelVisible flag (#115292)
Browse files Browse the repository at this point in the history
  • Loading branch information
HansMuller authored Nov 17, 2022
1 parent 1b23ad6 commit e8cbd44
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
11 changes: 11 additions & 0 deletions packages/flutter/lib/src/material/badge.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Badge extends StatelessWidget {
this.padding,
this.alignment,
this.label,
this.isLabelVisible = true,
this.child,
});

Expand Down Expand Up @@ -102,6 +103,12 @@ class Badge extends StatelessWidget {
/// this is a [StadiumBorder] shaped "large" badge with height [largeSize].
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.
///
/// Typically this is an default sized [Icon] that's part of a
Expand All @@ -110,6 +117,10 @@ class Badge extends StatelessWidget {

@override
Widget build(BuildContext context) {
if (!isLabelVisible) {
return child ?? const SizedBox();
}

final BadgeThemeData badgeTheme = BadgeTheme.of(context);
final BadgeThemeData defaults = _BadgeDefaultsM3(context);
final double effectiveSmallSize = smallSize ?? badgeTheme.smallSize ?? defaults.smallSize!;
Expand Down
26 changes: 24 additions & 2 deletions packages/flutter/test/material/badge_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void main() {
alignment: Alignment.topLeft,
child: Builder(
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);
return const Badge(
label: Text('0'),
Expand Down Expand Up @@ -71,7 +71,7 @@ void main() {
alignment: Alignment.topLeft,
child: Builder(
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);
return const Badge(
label: Text('0'),
Expand Down Expand Up @@ -202,5 +202,27 @@ void main() {
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()));
});
}

0 comments on commit e8cbd44

Please sign in to comment.