TDCheckboxGroupContainer constructor

TDCheckboxGroupContainer({
  1. Key? key,
  2. Widget? child,
  3. Axis? direction,
  4. List<TDCheckbox>? directionalTdCheckboxes,
  5. List<String>? selectIds,
  6. bool? passThrough,
  7. bool cardMode = false,
  8. int? titleMaxLine,
  9. int? maxSelected,
  10. TDCheckboxStyle? style,
  11. TDCheckboxGroupController? controller,
  12. IconBuilder? customIconBuilder,
  13. ContentBuilder? customContentBuilder,
  14. double? spacing,
  15. TDContentDirection? contentDirection,
  16. OnCheckBoxGroupChange? onCheckBoxGroupChange,
  17. VoidCallback? onOverloadChecked,
})

Implementation

TDCheckboxGroupContainer({
  Key? key,
  Widget? child, // 使用child 则请勿设置direction
  Axis? direction, // direction 对 directionalTdRadios 起作用
  List<TDCheckbox>? directionalTdCheckboxes,
  List<String>? selectIds, // 默认选择项的id组
  bool? passThrough, // 非通栏单选样式 用于使用child 或 direction == Axis.vertical 场景
  bool cardMode = false,
  int? titleMaxLine, // item的行数
  int? maxSelected, // 最大勾选数
  TDCheckboxStyle? style,// 勾选样式
  TDCheckboxGroupController? controller,
  IconBuilder? customIconBuilder,
  ContentBuilder? customContentBuilder,
  double? spacing, // icon和文字距离
  TDContentDirection? contentDirection,
  OnCheckBoxGroupChange? onCheckBoxGroupChange,
  VoidCallback? onOverloadChecked,
})  : assert(() {
  // 使用direction属性则必须配合directionalTdCheckboxes,child字段无效
  if (direction != null && directionalTdCheckboxes == null) {
    throw FlutterError(
        '[TDCheckboxGroupContainer] direction and directionalTdCheckboxes must set at the same time');
  }
  // 未使用direction则必须设置child
  if (direction == null && child == null) {
    throw FlutterError(
        '[TDCheckboxGroupContainer] direction means use child as the exact one, but child is null');
  }
  // 横向单选框 每个选项有字数限制
  if (direction == Axis.horizontal && directionalTdCheckboxes != null) {
    directionalTdCheckboxes.forEach((element) {
      if (element.subTitle != null) {
        throw FlutterError(
            'horizontal checkbox style should not have subTilte, '
                'because there left no room for it');
      }
    });
    var maxWordCount = 2;
    var tips =
        '[TDCheckboxGroupContainer] checkbox title please not exceed $maxWordCount words.\n'
        '2tabs: 7words maximum\n'
        '3tabs: 4words maximum\n'
        '4tabs: 2words maximum';
    if (directionalTdCheckboxes.length == 2) {
      maxWordCount = 7;
    }
    if (directionalTdCheckboxes.length == 3) {
      maxWordCount = 4;
    }
    if (directionalTdCheckboxes.length == 4) {
      maxWordCount = 2;
    }
    directionalTdCheckboxes.forEach((checkbox) {
      if ((checkbox.title?.length ?? 0) > maxWordCount) {
        throw FlutterError(tips);
      }
    });
  }
  // 卡片模式要求每个TDRadio必须设置cardMode属性为true,且不能有子标题(空间不够)
  if (cardMode == true) {
    assert(direction != null && directionalTdCheckboxes != null);
    directionalTdCheckboxes!.forEach((element) {
      // if use cardMode at TDRadioGroup, then every TDRadio should
      // set it's own carMode to true.
      if (element.cardMode == false) {
        throw FlutterError(
            'if use cardMode at TDCheckboxGroupContainer, then every '
                'TDCheckbox should set it\'s own carMode to true.');
      }
      if (element.subTitle != null && direction == Axis.horizontal) {
        throw FlutterError(
            'horizontal card style should not have subTilte, '
                'because there left no room for it');
      }
    });
  }
  return true;
}()),
      super(
      child: Container(
        clipBehavior: (passThrough ?? false) && direction != Axis.horizontal
            ? Clip.hardEdge
            : Clip.none,
        decoration: (passThrough ?? false) && direction != Axis.horizontal
            ? BoxDecoration(borderRadius: BorderRadius.circular(10))
            : null,
        margin: (passThrough ?? false) && direction != Axis.horizontal
            ? const EdgeInsets.symmetric(horizontal: 16)
            : null,
        child: direction == null
            ? child!
            : (direction == Axis.vertical
            ? ListView.separated(
          padding: const EdgeInsets.all(0),
          shrinkWrap: true,
          physics: const NeverScrollableScrollPhysics(),
          itemBuilder: (BuildContext context, int index) {
            return Container(
              margin: cardMode
                  ? const EdgeInsets.symmetric(horizontal: 16)
                  : null,
              height: cardMode ? 82 : null,
              child: directionalTdCheckboxes![index],
            );
          },
          itemCount: directionalTdCheckboxes!.length,
          separatorBuilder: (BuildContext context, int index) {
            if (cardMode) {
              return const SizedBox(
                height: 12,
              );
            }
            return const SizedBox.shrink();
          },
        )
            : Container(
          margin: cardMode
              ? EdgeInsets.symmetric(horizontal: 16.scale)
              : null,
          alignment: cardMode ? Alignment.topLeft : null,
          child: cardMode
              ? Wrap(
            spacing: 12.scale,
            runSpacing: 12,
            runAlignment: WrapAlignment.spaceEvenly,
            children: directionalTdCheckboxes!.map((element) {
              return SizedBox(
                width: 106.3.scale,
                height: 56,
                child: element,
              );
            }).toList(),
          )
              : Row(
            mainAxisSize: MainAxisSize.min,
            children: directionalTdCheckboxes!
                .map((e) => Expanded(child: e))
                .toList(),
          ),
        )),
      ),
      key: key,
      onChangeGroup: (ids) {
        selectIds = ids;
        onCheckBoxGroupChange?.call(ids);
      },
      onOverloadChecked: onOverloadChecked,
      controller: controller,
      checkedIds: selectIds,
      maxChecked: maxSelected,
      titleMaxLine: titleMaxLine,
      contentDirection: contentDirection,
      customIconBuilder: customIconBuilder,
      customContentBuilder: customContentBuilder,
      style: style,
      spacing: spacing,
    );