/*
 * @Author: XianKaiQun
 * @Date: 2020-04-21 09:44:16
 * @LastEditors: XianKaiQun
 * @LastEditTime: 2020-04-21 09:48:22
 * @Descripttion: 
 */
import 'package:flutter/widgets.dart';

class WisSliverGrid extends StatelessWidget {
  const WisSliverGrid({
    Key? key,
    required this.count,
    required this.builder,
    this.crossAxisCount = 2,
    this.childAspectRatio = 1.0,
    this.mainAxisSpacing = 0.0,
    this.crossAxisSpacing = 0.0,
    this.padding,
  }) : super(key: key);

  final Widget Function(BuildContext context, int index) builder;

  /// 多少个
  final int count;

  ///横轴多少个
  final int crossAxisCount;

  ///孩子比例
  final double childAspectRatio;

  ///横向间隔
  final double mainAxisSpacing;

  ///竖向间隔
  final double crossAxisSpacing;

  ///padding
  final EdgeInsetsGeometry? padding;

  @override
  Widget build(BuildContext context) {
    var gridDelegate = SliverGridDelegateWithFixedCrossAxisCount(
      crossAxisCount: crossAxisCount,
      mainAxisSpacing: mainAxisSpacing,
      crossAxisSpacing: crossAxisSpacing,
      childAspectRatio: childAspectRatio,
    );

    Widget cur = SliverGrid(
      delegate: SliverChildBuilderDelegate(
        builder,
        childCount: count,
      ),
      gridDelegate: gridDelegate,
    );

    ///
    if (padding != null) {
      cur = SliverPadding(
        padding: padding!,
        sliver: cur,
      );
    }
    return cur;
  }
}
