import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:zhiying_base_widget/widgets/custom/goods/category_goods/category_goods_callback.dart';
import 'package:zhiying_base_widget/widgets/home/home_goods/bloc/home_goods_bloc.dart';
import 'package:zhiying_base_widget/widgets/home/home_goods/bloc/home_goods_header_bloc.dart';
import 'package:zhiying_comm/zhiying_comm.dart';
import 'package:zhiying_comm/util/base_bloc.dart';
import 'custom_goods.dart';
import 'custom_goods_header.dart';

class CategoryGoodsListCreater extends WidgetCreater {
  @override
  List<Widget> createWidgets(Map<String, dynamic> model) {
    print('创建商品列表');
    Map<String, dynamic> json = {};
    try {
      json = jsonDecode(model['data']);
    } catch (e) {}

    // 上边距
    Widget padding = SliverToBoxAdapter(child: Container(margin: EdgeInsets.only(top: ParseUtil.stringParseDouble(null != json ? json['top_margin'] : '0'))));

    // 商品列表
    HomeGoodsBloc goodsBloc = HomeGoodsBloc();
    Widget goodsList = CustomGoods(model, bloc: goodsBloc);
    // Widget goodsList = BlocProvider<HomeGoodsBloc>(
    //   bloc: goodsBloc,
    //   child: CustomGoodsContainer(model),
    // );

    // 商品头部
    // Widget _widget = CustomGoodsHeader(model, goodsBloc);
    Widget _widget = BlocProvider<HomeGoodsHeaderBloc>(
      bloc: HomeGoodsHeaderBloc(),
      child: CustomGoodsHeaderContainer(model, goodsBloc),
    );
    Widget header = SliverPersistentHeader(pinned: true, floating: false, delegate: CustomGoodsHeaderDelegate(_widget));

    return [
      // 边距
      padding,
      // 头部
      header,
      // 列表
      goodsList
    ];
  }

  @override
  bool isSliverChild() {
    return true;
  }
}
