Skip to content

Instantly share code, notes, and snippets.

@hawkkiller
Created May 20, 2024 14:11
Show Gist options
  • Save hawkkiller/b0f5de60088c764dc331caec895fdd4e to your computer and use it in GitHub Desktop.
Save hawkkiller/b0f5de60088c764dc331caec895fdd4e to your computer and use it in GitHub Desktop.
Dead simple badge using custom single child layout
class RecommendedBadge extends StatelessWidget {
const RecommendedBadge({super.key});
@override
Widget build(BuildContext context) => DecoratedBox(
decoration: BoxDecoration(
color: Colors.of(context).tertiary,
borderRadius: BorderRadius.circular(4),
),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 2),
child: Text.labelSmall(
'RECOMMENDED',
color: Colors.of(context).onTertiary,
),
),
);
}
class _BadgeCustomSingleChildLayout extends SingleChildLayoutDelegate {
_BadgeCustomSingleChildLayout({required this.alignment});
final Alignment alignment;
@override
BoxConstraints getConstraintsForChild(BoxConstraints constraints) => constraints.loosen();
@override
Offset getPositionForChild(Size size, Size childSize) =>
alignment.alongSize(size) - alignment.alongSize(childSize) - Offset(0, childSize.height / 2);
@override
bool shouldRelayout(covariant _BadgeCustomSingleChildLayout oldDelegate) =>
alignment != oldDelegate.alignment;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment