import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class BrandList extends StatefulWidget {
  final TabController tabController;

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

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

class _BrandListState extends State<BrandList> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: TabBarView(
          controller: widget.tabController, children: _buildTabView()),
    );
  }

  _buildTabView() {
    List<Widget> listWidget = List();
    for (var index = 0; index < 6; index++) {
      listWidget.add(BrandListView());
    }
    return listWidget;
  }
}

///底部列表
class BrandListView extends StatefulWidget {
  final String type;

  const BrandListView({Key key, this.type}) : super(key: key);

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

class _BrandListViewState extends State<BrandListView> {
  @override
  Widget build(BuildContext context) {
    return Container(
        child: ListView.builder(
            shrinkWrap: false, itemCount: 2, itemBuilder: _buildItem));
  }

  Widget _buildItem(BuildContext context, int index) {
    if (index == 0) {
      return ListView.builder(
          physics: NeverScrollableScrollPhysics(),
          shrinkWrap: true,
          itemCount: 2,
          itemBuilder: _buildListItem1);
    } else {
      return ListView.builder(
          physics: NeverScrollableScrollPhysics(),
          shrinkWrap: true,
          itemCount: 2,
          itemBuilder: _buildListItem2);
    }
  }

  ///构建某个品牌1
  Widget _buildListItem1(BuildContext context, int index) {
    if (index == 0) {
      return Container(
        color: Colors.amber,
        height: 40,
        child: Center(
            child: CachedNetworkImage(
          imageUrl: "",
        )),
      );
    } else {
      return Container(
        margin: EdgeInsets.only(left: 10, right: 10, top: 8),
        padding: EdgeInsets.all(7),
        decoration: BoxDecoration(
            color: HexColor.fromHex("#FFFFFF"),
            borderRadius: BorderRadius.circular(8)),
        child: Column(
          children: <Widget>[
            Row(
              children: <Widget>[
                CachedNetworkImage(
                  imageUrl: "",
                  width: 40,
                  height: 40,
                ),
                Expanded(
                  child: Container(
                    height: 40,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        Text(
                          "百草味",
                          style: TextStyle(
                              color: HexColor.fromHex("#333333"),
                              fontSize: 15,
                              fontWeight: FontWeight.w600),
                        ),
                        Expanded(
                            child: Container(
                                height: 20,
                                margin: EdgeInsets.only(top: 4),
                                child: ListView.builder(
                                    scrollDirection: Axis.horizontal,
                                    padding: EdgeInsets.all(0),
                                    shrinkWrap: true,
                                    itemCount: 4,
                                    itemBuilder: _buildDiscounts)))
                      ],
                    ),
                  ),
                ),
                Row(
                  children: <Widget>[
                    Text(
                      "加载更多",
                      style: TextStyle(
                          color: HexColor.fromHex("#FF333333"), fontSize: 12),
                    ),
                    Icon(Icons.keyboard_arrow_right)
                  ],
                )
              ],
            ),
            Row(
              children: <Widget>[
                Expanded(
                    child: Container(
                  height: 180,
                  child: ListView.builder(
                    itemCount: 4,
                    shrinkWrap: true,
                    itemBuilder: _buildGoodsItem,
                    scrollDirection: Axis.horizontal,
                  ),
                ))
              ],
            )
          ],
        ),
      );
    }
  }

  ///构建某个品牌2
  Widget _buildListItem2(BuildContext context, int index) {
    if (index == 0) {
      return Container(
        color: Colors.amber,
        height: 40,
        child: Center(
            child: CachedNetworkImage(
          imageUrl: "",
        )),
      );
    } else {
      return Container(
        margin: EdgeInsets.only(left: 10, right: 10, top: 8),
        padding: EdgeInsets.all(7),
        decoration: BoxDecoration(
            color: HexColor.fromHex("#FFFFFF"),
            borderRadius: BorderRadius.circular(8)),
        child: Column(
          children: <Widget>[
            Row(
              children: <Widget>[
                CachedNetworkImage(
                  imageUrl: "",
                  width: 40,
                  height: 40,
                ),
                Expanded(
                  child: Container(
                    height: 40,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        Text(
                          "百草味",
                          style: TextStyle(
                              color: HexColor.fromHex("#333333"),
                              fontSize: 15,
                              fontWeight: FontWeight.w600),
                        ),
                        Expanded(
                            child: Container(
                                height: 20,
                                margin: EdgeInsets.only(top: 4),
                                child: ListView.builder(
                                    scrollDirection: Axis.horizontal,
                                    padding: EdgeInsets.all(0),
                                    shrinkWrap: true,
                                    itemCount: 4,
                                    itemBuilder: _buildDiscounts)))
                      ],
                    ),
                  ),
                ),
                Row(
                  children: <Widget>[
                    Text(
                      "已售",
                      style: TextStyle(
                          color: HexColor.fromHex("#FF333333"), fontSize: 12),
                    ),
                    Text(
                      "10.6",
                      style: TextStyle(
                          color: HexColor.fromHex("#FFFF4242"), fontSize: 12),
                    ),
                    Text(
                      "万件",
                      style: TextStyle(
                          color: HexColor.fromHex("#FF333333"), fontSize: 12),
                    ),
                    Icon(Icons.keyboard_arrow_right)
                  ],
                )
              ],
            ),
            Row(
              children: <Widget>[
                Expanded(
                    child: Container(
                  height: 180,
                  child: ListView.builder(
                    itemCount: 4,
                    shrinkWrap: true,
                    itemBuilder: _buildGoodsItem,
                    scrollDirection: Axis.horizontal,
                  ),
                ))
              ],
            )
          ],
        ),
      );
    }
  }

  ///构建优惠项
  Widget _buildDiscounts(BuildContext context, int index) {
    return Container(
      margin: EdgeInsets.only(right: 6),
      child: Padding(
        padding: EdgeInsets.only(left: 4, right: 4),
        child: Center(
          child: Text(
            "ddd",
            style:
                TextStyle(color: HexColor.fromHex("#FFFF6500"), fontSize: 10),
          ),
        ),
      ),
      decoration: BoxDecoration(
          color: HexColor.fromHex("#FFFFF1E8"),
          borderRadius: BorderRadius.circular(4)),
    );
  }

  Widget _buildGoodsItem(BuildContext context, int index) {
    return Container(
      width: 101,
      margin: EdgeInsets.only(right: 10, top: 10),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Container(
            width: 101,
            height: 101,
            color: HexColor.fromHex("#424242"),
          ),

          ///商品名称
          Container(
              width: 101,
              child: Text(
                "商品名称短发的说法发大水方法",
                style: TextStyle(color: HexColor.fromHex("#333333")),
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
              )),
          Row(
            crossAxisAlignment: CrossAxisAlignment.end,
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              Padding(
                padding: EdgeInsets.only(bottom: 1),
                child: Text(
                  "¥",
                  style: TextStyle(
                      color: HexColor.fromHex("#333333"), fontSize: 10),
                ),
              ),
              SizedBox(
                width: 2,
              ),
              Text(
                "118",
                style: TextStyle(
                    color: HexColor.fromHex("#FFFF4242"), fontSize: 15),
              ),
              SizedBox(
                width: 10,
              ),
              Padding(
                padding: EdgeInsets.only(bottom: 1),
                child: Text(
                  "¥200",
                  style: TextStyle(
                      decoration: TextDecoration.lineThrough,
                      color: HexColor.fromHex("#FFACACAC"),
                      fontSize: 10),
                ),
              ),
            ],
          ),
          Row(
            children: buildCoupon(),
          )
        ],
      ),
    );
  }

  ///构建优惠券
  buildCoupon() {
    List<Widget> listWidget = List();
    for (var index = 0; index < 2; index++) {
      listWidget.add(Expanded(
        child: Container(
          decoration: BoxDecoration(
              color: HexColor.fromHex("#FFFFF1F1"),
              borderRadius: BorderRadius.circular(4)),
          child: Padding(
            padding: EdgeInsets.all(4.0),
            child: Text(
              "券¥26.00",
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
              style:
                  TextStyle(color: HexColor.fromHex("#FFFF4242"), fontSize: 10),
            ),
          ),
        ),
      ));
    }
    return listWidget;
  }
}
