import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:huixiang/generated/l10n.dart';
import 'package:huixiang/retrofit/data/activity.dart';
import 'package:huixiang/retrofit/data/base_data.dart';
import 'package:huixiang/retrofit/data/miNiDetail.dart';
import 'package:huixiang/retrofit/data/shoppingCart.dart';
import 'package:huixiang/retrofit/data/store_info.dart';
import 'package:huixiang/retrofit/min_api.dart';
import 'package:huixiang/retrofit/retrofit_api.dart';
import 'package:huixiang/store/store_view/people_num.dart';
import 'package:huixiang/store/store_view/product_sku.dart';
import 'package:huixiang/store/store_view/shop_car.dart';
import 'package:huixiang/store/store_view/store_activity.dart';
import 'package:huixiang/store/store_view/store_info.dart';
import 'package:huixiang/store/store_view/store_order_list.dart';
import 'package:huixiang/union/union_view/union_coupon.dart';
import 'package:huixiang/union/union_view/vip.dart';
import 'package:huixiang/utils/font_weight.dart';
import 'package:huixiang/view_widget/custom_image.dart';
import 'package:huixiang/view_widget/my_tab.dart';
import 'package:huixiang/view_widget/receive_success.dart';
import 'package:huixiang/view_widget/round_button.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:shared_preferences/shared_preferences.dart';

class StoreOrderPage extends StatefulWidget {
  final Map<String, dynamic> arguments;

  StoreOrderPage({this.arguments});

  @override
  State<StatefulWidget> createState() {
    return _StoreOrderPage();
  }
}

class _StoreOrderPage extends State<StoreOrderPage>
    with TickerProviderStateMixin {
  TabController tabcontroller;
  ApiService apiService;
  MinApiService minService;
  StoreInfo storeInfo;
  List<Activity> activitys;
  StoreOrderListPage storeOrderListPage;

  ScrollController controller = ScrollController();
  ShoppingCart shopCarGoods;
  int tableId = 0;
  String parentId = "";
  String parentCode = "";
  String tenant = "";
  String storeId = "";
  int numberOfPeople = 0;
  ///小程序token
  String minToken;
  String pName;
  String pid;
  String cName;
  String cid;

  @override
  void initState() {
    super.initState();

    tableId = widget.arguments["tableId"] ?? 0;
    tenant = widget.arguments["tenant"];
    storeId = widget.arguments["id"];

    debugPrint(
        "store_param  tableId:$tableId  tenant:$tenant  storeId:$storeId");

    if (tabcontroller == null)
      tabcontroller = TabController(
        length: 2,
        vsync: this,
      );

    SharedPreferences.getInstance().then((value) {
      minLogin(value);
      queryStoreInfo();
    });
  }

  /// 小程序登录
  minLogin(SharedPreferences shared) async {
    EasyLoading.show(status: S.of(context).zhengzaijiazai);
    apiService = ApiService(
      Dio(),
      context: context,
      token: shared.getString('token'),
      showLoading: false,
    );
    apiService.minLogin(storeId).catchError((onError) {
      debugPrint(onError);
    }).then((baseData) {
      if (baseData != null && baseData.isSuccess) {
        Map<String, dynamic> minStoreInfo = baseData.data;
        minToken = minStoreInfo["token"];
        queryStoreInfo();
        SharedPreferences.getInstance().then(
          (value) => {
            value.setString('minToken', minToken),
            value.setString('tenant', tenant),
            value.setString('storeId', storeId),
          },
        );
        minService = MinApiService(
          Dio(),
          context: context,
          token: minToken,
          tenant: tenant,
          storeId: storeId,
          showLoading: false,
        );

        queryMemberInfo();

        queryShopCar().then((value) {
          this.shopCarGoods = value;
          setState(() {});
        });
        if (tableId > 0) {
          getParentInfo();
        }
        EasyLoading.dismiss();
      }
    });
  }

  queryMemberInfo() async {
    BaseData baseData = await minService.memberInfo()
        .catchError((error){debugPrint(error);});
    if (baseData != null && baseData.isSuccess) {
      SharedPreferences.getInstance().then(
            (value) => {
          value.setString('minMember', jsonEncode(baseData.data)),
        },
      );
    }
  }

  ///获取父订单(火锅订单加菜前调用)
  getParentInfo() async {
    BaseData baseData = await minService.getParentInfo("$tableId")
        .catchError((error) {debugPrint(error);});
    if (baseData != null && baseData.isSuccess) {
      if (baseData.data != null) {
        parentId = baseData.data["id"];
        parentCode = baseData.data["parentCode"];
      } else {
        ///TODO: 没有父订单,
        queryStoreInfo1();
      }
    }
  }

  ///获取桌子信息
  queryStoreInfo1() async {
    BaseData baseData = await minService.queryStoreInfo1({
      "getCoupon": true,
      "storeId": storeId,
      "tableId": tableId,
    }).catchError((error) {
      debugPrint(error);
    });
    if (baseData != null && baseData.isSuccess) {
      StoreInfo storeInfo = StoreInfo.fromJson(baseData.data);
      if (storeInfo.storeTable != null) {
        showPeopleNum(storeInfo.storeTable.tableName);
      }
    }
  }

  /// 查询店铺信息
  queryStoreInfo() async {
    BaseData baseData = await apiService.queryStoreInfo(storeId).catchError((error) {
      debugPrint(error);
    });
    if (baseData != null && baseData.isSuccess) {
      storeInfo = StoreInfo.fromJson(baseData.data);
      activitys = storeInfo.informationVOPageVO.list
          .map((e) => Activity.fromJson(e))
          .toList();
      if (mounted) {
        setState(() {});
      }
    }
    EasyLoading.dismiss();
  }

  RefreshController refreshController;
  bool dialogShowing = false;

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        if (dialogShowing) {
          debugPrint("ssssasdadsasdadasd");
          SmartDialog.dismiss();
          return false;
        } else {
          return true;
        }
      },
      child: Container(
        color: Colors.white,
        child: Stack(
          children: [
            Positioned(
              left: 0,
              right: 0,
              top: 0,
              bottom: 54.h,
              child: NestedScrollView(
                controller: controller,
                dragStartBehavior: DragStartBehavior.start,
                physics: BouncingScrollPhysics(),
                headerSliverBuilder: (BuildContext context, bool innerScrolled) {
                  return [
                    SliverOverlapAbsorber(
                      handle: NestedScrollView.sliverOverlapAbsorberHandleFor(
                          context),
                      sliver: SliverAppBar(
                        expandedHeight:
                        (storeInfo != null && storeInfo.couponVOList != null)
                            ? 470.h
                            : 365.h,
                        floating: false,
                        snap: false,
                        pinned: true,
                        stretch: false,
                        brightness: Brightness.light,
                        leading: GestureDetector(
                          onTap: () {
                            Navigator.of(context).pop();
                          },
                          child: Container(
                            alignment: Alignment.centerRight,
                            margin: EdgeInsets.only(left: 10),
                            padding: EdgeInsets.all(10),
                            child: Icon(
                              Icons.arrow_back_ios,
                              color: Colors.black,
                              size: 24,
                            ),
                          ),
                        ),
                        flexibleSpace: FlexibleSpaceBar(
                          title: Title(
                            controller,
                            storeInfo != null ? storeInfo.storeName : ''),
                          collapseMode: CollapseMode.pin,
                          stretchModes: [
                            StretchMode.zoomBackground,
                            StretchMode.fadeTitle,
                            StretchMode.blurBackground,
                          ],
                          background: Container(
                            color: Colors.white,
                            child: Stack(
                              children: [
                                Positioned(
                                  child: Column(
                                    children: [
                                      buildSwiper(),
                                      Expanded(
                                        child: Container(
                                          color: Colors.transparent,
                                        ),
                                        flex: 1,
                                      ),
                                    ],
                                  ),
                                  top: 0,
                                  bottom: 0,
                                  left: 0,
                                  right: 0,
                                ),
                                Positioned(
                                  child: Container(
                                    child: Column(
                                      children: [
                                        ///门店信息
                                        StoreInfoView(storeInfo),

                                        ///门店对应优惠券
                                        if (storeInfo != null &&
                                            storeInfo.couponVOList != null)
                                          UnionCoupon(
                                            storeInfo,
                                            _receiveCoupon,
                                            coupon: true,
                                          ),

                                        if (storeInfo == null ||
                                            storeInfo.couponVOList == null)
                                          SizedBox(
                                            height: 8,
                                          ),

                                        ///门店对应VIP信息
                                        Vip(storeInfo, () {}, false),
                                      ],
                                    ),
                                  ),
                                  top: 110.h,
                                  bottom: 0,
                                  left: 0,
                                  right: 0,
                                ),
                              ],
                            ),
                          ),
                        ),
                        backgroundColor: Color(0x33FAFAFA),
                        centerTitle: false,
                        elevation: 0,
                        bottom: PreferredSize(
                          preferredSize: Size(
                            MediaQuery.of(context).size.width,
                            38,
                          ),
                          child: Container(
                            padding: EdgeInsets.symmetric(horizontal: 10.w),
                            width: MediaQuery.of(context).size.width,
                            child: TabBar(
                              controller: tabcontroller,
                              automaticIndicatorColorAdjustment: true,
                              isScrollable: true,
                              indicatorWeight: 1,
                              indicatorColor: Color(0xFFFAFAFA),
                              labelPadding: EdgeInsets.only(
                                left: 8.w,
                                right: 8.w,
                              ),
                              indicatorSize: TabBarIndicatorSize.label,
                              unselectedLabelStyle: TextStyle(
                                fontSize: 15.sp,
                                fontWeight: FontWeight.w400,
                              ),
                              labelStyle: TextStyle(
                                color: Colors.black,
                                fontSize: 18.sp,
                                fontWeight: FontWeight.bold,
                              ),
                              labelColor: Colors.black,
                              tabs: [
                                MyTab(text: S.of(context).diancan),
                                MyTab(text: S.of(context).xindianhuodong),
                              ],
                            ),
                          ),
                        ),
                      ),
                    ),
                  ];
                },
                body: TabBarView(
                  physics: NeverScrollableScrollPhysics(),
                  children: [
                    ///点餐
                    StoreOrderListPage(
                      widget.arguments,
                      activitys,
                      storeInfo,
                      shopCarGoods,
                      controller,
                      minToken,
                      tenant,
                      _queryMiNiDetail,
                    ),

                    ///星店活动,
                    StoreActivity(
                      widget.arguments,
                      activitys,
                    ),
                  ],
                  controller: tabcontroller,
                ),
              ), /*SmartRefresher(
            controller: refreshController =
                RefreshController(initialRefresh: false),
            enablePullDown: true,
            enablePullUp: false,
            header: MyHeader(),
            physics: BouncingScrollPhysics(),
            onRefresh: () {
              queryStoreInfo();
            },
            child:
          ),*/
            ),
            Positioned(
              bottom: 0,
              left: 0,
              right: 0,
              child: Stack(
                alignment: Alignment.bottomLeft,
                children: [
                  Container(
                    height: 54.h,
                    color: Color(0xFFFAFAFA),
                    child: Row(
                      children: [
                        Spacer(),
                        Text(
                          S.of(context).heji,
                          style: TextStyle(
                            fontSize: 12.sp,
                            fontWeight: MyFontWeight.regular,
                            color: Colors.black,
                          ),
                        ),
                        Text(
                          "¥${shopCarGoods != null ? shopCarGoods.cartSum : "0.0"}",
                          style: TextStyle(
                            fontSize: 20.sp,
                            fontWeight: MyFontWeight.medium,
                            color: Color(0xFF32A060),
                          ),
                        ),
                        Spacer(),
                        GestureDetector(
                          onTap: () {
                            toDownOrder();
                          },
                          child: RoundButton(
                            width: 103.w,
                            height: 54.h,
                            text: S.current.jiesuan,
                            textColor: Colors.white,
                            fontWeight: MyFontWeight.regular,
                            backgroup: Color(0xFF32A060),
                            radius: 0,
                            fontSize: 16.sp,
                            padding: EdgeInsets.symmetric(vertical: 5.h),
                          ),
                        ),
                      ],
                    ),
                  ),
                  Stack(
                    children: [
                      InkWell(
                        onTap: () {
                          showShoppingCart();
                        },
                        child: Image.asset(
                          "assets/image/shopp.png",
                          width: 88,
                          height: 88,
                          fit: BoxFit.fitWidth,
                        ),
                      ),
                      Positioned(
                        right: 15,
                        top: 14,
                        child: RoundButton(
                          width: 17,
                          height: 17,
                          text: "${count()}",
                          textColor: Colors.white,
                          fontWeight: MyFontWeight.regular,
                          backgroup: Color(0xFF32A060),
                          fontSize: 12.sp,
                          radius: 100,
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  ///领取优惠券
  _receiveCoupon(couponId) async {
    BaseData baseData = await apiService.receiveCoupon(couponId);
    if (baseData != null && baseData.isSuccess) {
      queryStoreInfo();
      showAlertDialog();
    }
  }

  /// 领取成功弹窗
  showAlertDialog() {
    //显示对话框
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return ReceiveSuccess();
      },
    );
  }

  ///显示选择人数的弹窗
  showPeopleNum(String tableName) async {
    var people = await showDialog(
      context: context,
      barrierDismissible: false,
      builder: (context) {
        return PeopleNumView(tableName);
      },
    );

    if (people != null && people > 0) {
      setState(() {
        this.numberOfPeople = people;
      });
    } else {
      Navigator.of(context).pop();
    }
  }

  ///计算个数
  int count() {
    if (shopCarGoods == null ||
        shopCarGoods.shoppingCartSkuItemList == null ||
        shopCarGoods.shoppingCartSkuItemList.length == 0) return 0;
    int count = 0;
    shopCarGoods.shoppingCartSkuItemList.forEach((element) {
      count += element.buyNum;
    });
    return count;
  }

  ///去下单结算页面
  toDownOrder() async {
    int num = count();

    if (parentId == null || parentId == "") {
      if (num == 0) {
        SmartDialog.showToast("请先选择您要购买的商品!~");
        return;
      }
    }
    await Navigator.of(context).pushNamed(
      '/router/settlement',
      arguments: {
        "storeInfo": storeInfo,
        "tableId": tableId,
        "parentCode": parentCode,
        "parentId": parentId,
        "pName":pName,
        "pid":pid,
        "cName":cName,
        "cid":cid,
        "shoppingCart": shopCarGoods,
        "numberOfPeople": numberOfPeople,
      },
    );
    if(tableId >0){
      getParentInfo();
    }
    queryShopCar().then((value) {
      this.shopCarGoods = value;
      setState(() {});
    });
  }

  Widget buildSwiper() {
    return Container(
      width: double.infinity,
      height: 180.h,
      child: Swiper(
        pagination: SwiperPagination(
          alignment: Alignment.bottomCenter,
          builder: DotSwiperPaginationBuilder(
            size: 8.w,
            activeSize: 8.w,
            space: 5.w,
            activeColor: Colors.white,
            color: Colors.white.withAlpha(76),
          ),
        ),
        itemBuilder: (context, position) {
          return Container(
            child: MImage(
              (storeInfo != null &&
                      storeInfo.bannerList != null &&
                      position < storeInfo.bannerList.length)
                  ? storeInfo.bannerList[position].imgUrl
                  : "",
              fit: BoxFit.cover,
              radius: BorderRadius.zero,
              errorSrc: "assets/image/default_2_1.png",
              fadeSrc: "assets/image/default_2_1.png",
            ),
          );
        },
        itemCount: (storeInfo != null && storeInfo.bannerList != null)
            ? storeInfo.bannerList.length
            : 1,
      ),
    );
  }

  /// 购物车的key,用于刷新UI
  GlobalKey shopCartKey = GlobalKey();

  ///购物车弹窗
  showShoppingCart() {
    queryShopCar().then((value) {
      this.shopCarGoods = value;
      dialogShowing = true;
      SmartDialog.show(
        widget: ShopCar(
          shopCartKey,
          this.shopCarGoods,
          clearShopCar,
          toDownOrder,
          shopCartAdd,
          shopCartReduce,
        ),
        onDismiss: () {
          dialogShowing = false;
        },
        alignmentTemp: Alignment.bottomCenter,
      );
    });
  }

  ///清空购物车
  clearShopCar() async {
    BaseData<bool> baseData = await minService.clearShoppingCart();
    if (baseData.isSuccess) {
      shopCarGoods = null;
      setState(() {});
    }
  }

  ///选规格
  _queryMiNiDetail(String id,int count) async {
    EasyLoading.show(status: S.current.zhengzaijiazai);
    BaseData<MiNiDetail> baseData = await minService.miNiDetail(id).catchError((error){});
    if (baseData != null && baseData.isSuccess) {
      showStoreSelector(baseData.data, id,count);
    }
  }

  ///选规格弹窗
  showStoreSelector(MiNiDetail miNiDetail, String id,int count) async {
    if (miNiDetail.attrList != null && miNiDetail.attrList.length == 1
        && miNiDetail.attrList[0].attrValueList.length == 1) {
      _addShopCar(miNiDetail, [], count);
    } else {
      EasyLoading.dismiss();
      dialogShowing = true;
      SmartDialog.show(
        widget: ProductSku(
          miNiDetail,
          shopCarGoods,
          id,
          _addShopCar,
          add,
          reduce,
        ),
        onDismiss: () {
          dialogShowing = false;
        },
        alignmentTemp: Alignment.bottomCenter,
      );
    }
  }

  ///添加购物车
  Future _addShopCar(MiNiDetail miNiDetail, selectSkus, int count) async {
    ProductSkuVOListBean productSku;
    if (selectSkus!= null && selectSkus.length == 0) {
      productSku = miNiDetail.productSkuVOList.first;
    } else {
      productSku = miNiDetail.productSkuVOList.firstWhere((element) {
        bool gg = true;
        selectSkus.forEach((element1) {
          if (element.skuNameStr.indexOf(element1) < 0) {
            gg = false;
            return gg;
          }
        });
        return gg;
      });
    }
    if (productSku == null) return;
    String skuId = productSku.id;
    String skuValue = selectSkus
        .toString()
        .replaceAll("[", "")
        .replaceAll("]", "")
        .replaceAll(",", "");

    if (miNiDetail != null) {
      BaseData<List<ShoppingCart>> baseDate = await minService.addShoppingCart({
        "storeId": storeInfo.id,
        "storeName": storeInfo.storeName ?? "",
        "numberOfPeople": numberOfPeople,
        "tableId": tableId,
        "parentId": parentId,
        "parentCode": parentCode,
        "shoppingCartSkuItemList": [
          {
            "buyNum": count,
            "id": skuId,
            "productId": miNiDetail.id,
            "productName": miNiDetail.productName,
            "skuName": skuValue,
            "storeId": storeInfo.id,
            "skuPrice": productSku.skuPrice,
            "skuStock": productSku.skuStock,
            "tableId": tableId,
          },
        ],
      });
      EasyLoading.dismiss();
      if (baseDate != null && baseDate.isSuccess) {
        queryShopCar().then((value) {
          this.shopCarGoods = value;
          setState(() {});
        });
      }
    }
  }

  ///查询购物车
  Future<ShoppingCart> queryShopCar() async {
    pName="";//活动
    pid="";//活动
    cName="";//优惠券
    cid="";//优惠券
    BaseData<List<ShoppingCart>> baseDate =
        await minService.getShoppingCart(tableId);
    if (baseDate != null &&
        baseDate.isSuccess &&
        baseDate.data != null &&
        baseDate.data.length > 0) {
      if( baseDate.data[0].selectDiscount ==1){
        baseDate.data[0].couponList.forEach((element) {
          if(element.isMaxCoupon){
            cName = element.promotionName;
            cid = element.id;
          }
        });
      }else if(baseDate.data[0].selectDiscount==2){
        baseDate.data[0].promotionInfoList.forEach((element) {
          if(element.isMaxPromotion){
            pName=element.name;
            pid=element.id;
          }
        });
      }
      return baseDate.data[0];
    } else {
      return null;
    }
  }

  ///购物车➕1
  Future<ShoppingCart> shopCartAdd(
      ShoppingCartSkuItemListBean cartSkuItem) async {
    Map<String, dynamic> shopCarTemp = shopCarGoods.toJson();
    cartSkuItem.buyNum += 1;
    shopCarTemp["shoppingCartSkuItemList"] = [cartSkuItem.toJson()];
    BaseData<List<ShoppingCart>> baseDate =
        await minService.shoppingCartSingle(shopCarTemp);
    if (baseDate.isSuccess) {
      this.shopCarGoods = await queryShopCar();
      if (shopCartKey != null) {
        shopCartKey.currentState.setState(() {});
      }
      setState(() {});
    }
    return this.shopCarGoods;
  }

  ///购物车➖1
  Future<ShoppingCart> shopCartReduce(
      ShoppingCartSkuItemListBean cartSkuItem) async {
    Map<String, dynamic> shopCarTemp = shopCarGoods.toJson();
    cartSkuItem.buyNum -= 1;
    shopCarTemp["shoppingCartSkuItemList"] = [cartSkuItem.toJson()];
    BaseData<List<ShoppingCart>> baseDate =
        await minService.shoppingCartSingle(shopCarTemp);
    if (baseDate.isSuccess) {
      this.shopCarGoods = await queryShopCar();
      if (shopCartKey != null) {
        shopCartKey.currentState.setState(() {});
      }
      setState(() {});
    }
    return this.shopCarGoods;
  }

  ///商品➕1
  add(MiNiDetail miNiDetail, selectSkus) async {
    ProductSkuVOListBean productSku =
        miNiDetail.productSkuVOList.firstWhere((element) {
      return skuY(element, selectSkus);
    });

    if (productSku == null) return;
    String skuId = productSku.id;
    if (shopCarGoods == null) {
      await _addShopCar(miNiDetail, selectSkus, 2);
      return;
    }
    int shopSkuIndex = shopCarGoods.shoppingCartSkuItemList
        .indexWhere((element) => skuId == element.id);
    Map<String, dynamic> shopCarTemp = shopCarGoods.toJson();
    shopCarGoods.tableId = "$tableId";

    if (shopSkuIndex >= 0) {
      shopCarGoods.shoppingCartSkuItemList[shopSkuIndex].buyNum += 1;
      ShoppingCartSkuItemListBean cartSkuItem = shopCarGoods
          .shoppingCartSkuItemList
          .firstWhere((element) => skuId == element.id);
      shopCarTemp["shoppingCartSkuItemList"] = [cartSkuItem.toJson()];
    } else {
      await _addShopCar(miNiDetail, selectSkus, 2);
      return;
    }

    BaseData<List<ShoppingCart>> baseDate =
        await minService.shoppingCartSingle(shopCarTemp);
    if (baseDate.isSuccess) {
      queryShopCar().then((value) {
        this.shopCarGoods = value;
        setState(() {});
      });
    }
  }

  ///商品➖1
  reduce(MiNiDetail miNiDetail, selectSkus) async {
    ProductSkuVOListBean productSku =
        miNiDetail.productSkuVOList.firstWhere((element) {
      return skuY(element, selectSkus);
    });

    if (productSku == null) return;
    String skuId = productSku.id;
    if (shopCarGoods == null) {
      await _addShopCar(miNiDetail, selectSkus, 2);
      return;
    }
    ShoppingCartSkuItemListBean shopSkuItem = shopCarGoods
        .shoppingCartSkuItemList
        .firstWhere((element) => skuId == element.skuId);
    int shopSkuIndex = shopCarGoods.shoppingCartSkuItemList
        .indexWhere((element) => skuId == element.skuId);

    if (shopSkuItem != null) {
      if (shopSkuItem.buyNum > 1) {
        shopCarGoods.shoppingCartSkuItemList[shopSkuIndex].buyNum -= 1;
      }
    } else {
      await _addShopCar(miNiDetail, selectSkus, 2);
      return;
    }
    shopCarGoods.tableId = "$tableId";

    Map<String, dynamic> shopCarTemp = shopCarGoods.toJson();
    ShoppingCartSkuItemListBean cartSkuItem = shopCarGoods
        .shoppingCartSkuItemList
        .firstWhere((element) => skuId == element.id);
    shopCarTemp["shoppingCartSkuItemList"] = [cartSkuItem.toJson()];

    BaseData<List<ShoppingCart>> baseDate =
        await minService.shoppingCartSingle(shopCarTemp);
    if (baseDate.isSuccess) {
      queryShopCar().then((value) {
        this.shopCarGoods = value;
        setState(() {});
      });
    }
  }

  bool skuY(ProductSkuVOListBean productSku, selectSkus) {
    bool gg = true;
    selectSkus.forEach((element1) {
      if (productSku.skuNameStr.indexOf(element1) < 0) {
        gg = false;
        return gg;
      }
    });
    return gg;
  }
}

class Title extends StatefulWidget {
  final ScrollController controller;
  final String title;

  Title(this.controller, this.title);

  @override
  State<StatefulWidget> createState() {
    return _Title();
  }
}

class _Title extends State<Title> {
  double alpha = 0;

  @override
  void initState() {
    super.initState();

    widget.controller.addListener(() {
      double maxScrollExtent = widget.controller.position.maxScrollExtent;
      double alphaHeight = (kToolbarHeight) / 2;
      double alphaProgress = maxScrollExtent - widget.controller.offset;
      double alphatemp = 0;
      if (alphaProgress <= 1) {
        alphatemp = 1;
      } else if (alphaProgress <= alphaHeight) {
        alphatemp =
            (((alphaHeight - alphaProgress) / alphaHeight) > 0.5) ? 1 : 0;
      } else {
        alphatemp = 0;
      }
      if (alpha != alphatemp && mounted) {
        alpha = alphatemp;
        print("object:  $alpha");
        setState(() {});
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(bottom: 38),
      child: Text(
        widget.title ?? "",
        style: TextStyle(
          color: Colors.black.withOpacity(alpha),
          fontWeight: FontWeight.bold,
          fontSize: 18.sp,
        ),
      ),
    );
  }
}