/*
 * @Author       : WuWei
 * @LastEditors  : WuWei
 * @Date         : 2021-09-22 17:15:29
 * @LastEditTime : 2023-05-25 15:50:49
 * @Description  : Do not edit
 */
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
import '../../assets.gen.dart';

///多项选择
class WFCheckBoxPicker extends StatelessWidget {
  const WFCheckBoxPicker({
    Key? key,
    this.isRequired,
    this.hintText,
    this.title,
    this.initialValue,
    this.onChanged,
    this.dataList,
    this.label,
    this.value,
    this.axis,
  }) : super(key: key);

  ///布局
  final Axis? axis;

  ///是否必须
  final bool? isRequired;
  final String? hintText;
  final Widget? title;
  final List? initialValue;

  /// 选择数据列表
  final dataList;

  /// 单选项对应的名称 { 'name': '标题名称', id: '999999'} 这里的[name]就是对应[label] 默认是'label'
  final String? label;

  /// 单选项对应的值 { 'name': '标题名称', id: '999999'} 这里的[id]就是对应[value] 默认是'value'
  final String? value;

  ///选择
  final void Function(dynamic value)? onChanged;

  @override
  Widget build(BuildContext context) {
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;

    var _initialValue = initialValue ?? [];
    List? _dataList = dataList;
    var _label = label;
    var _value = value;
    return WisFormLayoutComponent(
      title: title,
      isRequired: isRequired,
      axis: Axis.vertical,
      child: StatefulBuilder(
        builder: (BuildContext context, setState) {
          return Column(
            children: [
              for (num i = 0; i < _dataList!.length; i++)
                Wisdom.row(
                  onTap: () => {
                    setState(() {
                      if (_initialValue.contains(_dataList[i as int][_value])) {
                        _initialValue.remove(_dataList[i][_value]);
                      } else {
                        _initialValue.add(_dataList[i][_value]);
                      }
                      onChanged!(_initialValue);
                    })
                  },
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  margin: EdgeInsets.only(
                      bottom: (i + 1) == _dataList.length ? 15.pt : 26.pt),
                  children: [
                    if (_initialValue
                        .contains(_dataList[i as int][_value])) ...[
                      Image(image: AssetList.$fxxz_png_image, height: 16.pt),
                    ] else ...[
                      Image(image: AssetList.$dx_png_image, height: 16.pt),
                    ],
                    SizedBox(width: 8.pt),
                    WisText(
                      '${_dataList[i][_label]}',
                      size: 15.pt,
                      color: colorScheme.mainTitle,
                      weight: FontWeight.w300,
                    ).asExpanded(),
                  ],
                )
            ],
          );
        },
      ),
    );
  }
}
