Created
May 20, 2024 14:11
-
-
Save hawkkiller/b0f5de60088c764dc331caec895fdd4e to your computer and use it in GitHub Desktop.
Dead simple badge using custom single child layout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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