build method

  1. @override
Widget build(
  1. BuildContext context,
  2. int index,
  3. bool active
)

Called when the tab item is build.

  • context BuildContext instance;
  • index tab index;
  • active tab state;

Implementation

@override
Widget build(BuildContext context, int index, bool active) {
  Color bgColor = active
      ? activeBackgroundColor ?? Theme.of(context).colorScheme.primary
      : inactiveBackgroundColor ?? Colors.transparent;

  Color iconColor = active
      ? activeColor ?? Theme.of(context).colorScheme.onPrimary
      : inactiveColor ?? Theme.of(context).colorScheme.inversePrimary;

  return Stack(
    alignment: Alignment.center,
    children: [
      // circle shape background
      Container(
        width: active ? 60 : 30,
        height: active ? 60 : 30,
        decoration: BoxDecoration(
          gradient: active ? activeGradient : null,
          color: bgColor,
          borderRadius: BorderRadius.circular(90),
        ),
        alignment: Alignment.center,
      ),
      // icon
      items[index].itemBuilder != null
          ? items[index].itemBuilder!(context, active)
          : Container(
              alignment: Alignment.center,
              child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Icon(items[index].icon,
                        color: iconColor, size: active ? 30 : 20),
                  ]),
            ),
      // badge
      items[index].badge != null && items[index].badge! > 0
          ? Positioned(
              top: 5,
              right: 15,
              child: Container(
                width: 25,
                height: 25,
                decoration: BoxDecoration(
                  color: Colors.red,
                  borderRadius: BorderRadius.circular(90),
                ),
                alignment: Alignment.center,
                child: Text(
                  items[index].badge! > 99
                      ? '99+'
                      : items[index].badge.toString(),
                  style: const TextStyle(color: Colors.white, fontSize: 10),
                ),
              ),
            )
          : Container(),
    ],
  );
}