caculateBadgeParams method
Implementation
void caculateBadgeParams(BadgeTab badgeTab, BoxConstraints constraints) {
_paddingTop = -5.0;
if (badgeTab.badgeNum != null) {
if (badgeTab.badgeNum! < 10) {
_badgePadding = const EdgeInsets.only(left: 5.0, right: 5.0);
_largeSize = 16.0;
_badgeText = badgeTab.badgeNum?.toString() ?? "";
} else if (badgeTab.badgeNum! > 99) {
_badgePadding = const EdgeInsets.fromLTRB(4, 3, 4, 2);
_largeSize = 16.0;
_badgeText = "99+";
} else {
_badgePadding = const EdgeInsets.fromLTRB(4, 3, 4, 2);
_largeSize = 16.0;
_badgeText = badgeTab.badgeNum?.toString() ?? "";
}
} else {
if (badgeTab.badgeText != null && badgeTab.badgeText!.isNotEmpty) {
_badgePadding = const EdgeInsets.fromLTRB(6, 3, 6, 3);
_largeSize = 16.0;
_badgeText = badgeTab.badgeText?.toString() ?? "";
} else {
_badgePadding = const EdgeInsets.only(left: 4.0, right: 4.0);
_largeSize = 8.0;
_badgeText = "";
_paddingTop = 1.0;
}
}
// 获取 tabTextWidth
TextStyle tabTextStyle =
const TextStyle(overflow: TextOverflow.ellipsis, fontSize: 16);
TextPainter tabTextPainter = TextPainter(
locale: Localizations.localeOf(context), textAlign: TextAlign.center);
tabTextPainter.textDirection = TextDirection.ltr;
tabTextPainter.maxLines = 1;
tabTextPainter.text = TextSpan(text: badgeTab.text, style: tabTextStyle);
tabTextPainter.layout(maxWidth: constraints.maxWidth);
double tabTextWidth = tabTextPainter.width;
// 获取 badgeTextWidth
TextStyle badgeTextStyle = const TextStyle(height: 1, fontSize: 10);
TextPainter badgeTextPainter =
TextPainter(textScaleFactor: MediaQuery.of(context).textScaleFactor);
badgeTextPainter.textDirection = TextDirection.ltr;
badgeTextPainter.maxLines = 1;
badgeTextPainter.text = TextSpan(text: _badgeText, style: badgeTextStyle);
badgeTextPainter.layout(maxWidth: constraints.maxWidth);
// 红点内 text 的宽度
double badgeTextWidth = badgeTextPainter.width;
double badgeWidth = badgeTextWidth + _badgePadding.horizontal;
// 获取外部传入的tab padding值
EdgeInsets labelPadding = widget.labelPadding.resolve(TextDirection.ltr);
if ((tabTextWidth + badgeWidth) >
(constraints.maxWidth + labelPadding.right)) {
// 如果tab文字宽度 + 红点宽度 > 约束宽度(父容器宽度)+ 设置tab 右padding 则将红点左移 红点宽度偏移量
// if(_badgeWidth > (constraints.maxWidth + _labelPadding.right)){
// _paddingRight = 0.0;
// }else{
_paddingRight = constraints.maxWidth + labelPadding.right - badgeWidth;
// }
} else {
_paddingRight = tabTextWidth;
}
}