import 'package:flutter/material.dart';
import 'package:zhiying_comm/util/extension/color.dart';

class BrandTabBar extends StatefulWidget {
  final TabController tabController;

  const BrandTabBar(this.tabController, {Key key}) : super(key: key);

  @override
  _BrandTabBarState createState() => _BrandTabBarState();
}

class _BrandTabBarState extends State<BrandTabBar> {
  var currentIndex = 0;

  @override
  void initState() {
    widget.tabController.addListener(() {
      currentIndex = widget.tabController.index;
      setState(() {});
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return TabBar(
      indicatorWeight: 0.01,
      isScrollable: true,
      controller: widget.tabController,
      tabs: _buildTabs(),
      // onTap: (index) {
      //   currentIndex = index;
      //   setState(() {});
      // },
    );
  }

  ///构建
  _buildTabs() {
    List<Widget> listWidget = List();
    for (var index = 0; index < 6; index++) {
      listWidget.add(Container(
        height: 48,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          mainAxisSize: MainAxisSize.max,
          children: <Widget>[
            Text(
              index.toString(),
              style: TextStyle(
                  color: index == currentIndex
                      ? Colors.red
                      : HexColor.fromHex("#000000")),
            ),
            Container(
              width: 13.67,
              height: 4.6,
              color: index == currentIndex
                  ? Colors.red
                  : HexColor.fromHex("#000000"),
            )
          ],
        ),
      ));
    }
    return listWidget;
  }
}
