/*
 * @Author       : WuWei
 * @LastEditors  : WuWei
 * @Date         : 2021-04-26 10:26:32
 * @LastEditTime : 2023-05-25 15:55:43
 * @FilePath     : /wisdom-huiguanjia-app/wisdom_cli/lib/src/form_widget/components/radio.dart
 * @Description  : Do not edit
 */
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
import '../../assets.gen.dart';

///单项选择
class WFRadioPicker extends StatelessWidget {
  const WFRadioPicker({
    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 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 ?? 'label';
    var _value = value ?? 'value';
    final _axis = axis ?? Axis.horizontal;
    return WisFormLayoutComponent(
      title: title,
      isRequired: isRequired,
      axis: _axis,
      child: StatefulBuilder(
        builder: (BuildContext context, setState) {
          return _axis == Axis.horizontal
              ? Wisdom.wrap(
                  alignment: Alignment.centerLeft,
                  padding: EdgeInsets.only(left: 10.pt),
                  runSpacing: 10,
                  children: [
                    for (int i = 0; i < _dataList!.length; i++)
                      _dataList[i][_label].length > 5
                          ? Wisdom.row(
                              onTap: () => {
                                setState(() {
                                  _initialValue = _dataList[i][_value];
                                  onChanged!(_initialValue);
                                })
                              },
                              margin: i == 0
                                  ? EdgeInsets.only(
                                      top: 10.pt,
                                    )
                                  : i == (_dataList.length - 1)
                                      ? EdgeInsets.only(
                                          bottom: 10.pt,
                                        )
                                      : null,
                              mainAxisAlignment: MainAxisAlignment.start,
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                if (_dataList[i][_value] == _initialValue) ...[
                                  Image(
                                      image: AssetList.$danxuan_png_image,
                                      height: 17.pt),
                                ] else ...[
                                  Image(
                                      image: AssetList.$danxuanyuan_png_image,
                                      height: 17.pt),
                                ],
                                SizedBox(width: 8.pt),
                                WisText(
                                  '${_dataList[i][_label]}',
                                  size: 15.pt,
                                  color: Color(0xff222222),
                                  weight: FontWeight.w300,
                                ).asExpanded(),
                              ],
                            )
                          : Wisdom.row(
                              width: 100.pt,
                              minWidth: 100.pt,
                              margin: i == (_dataList.length - 1)
                                      ? EdgeInsets.only(
                                          bottom: 10.pt,
                                        )
                                      : null,
                              onTap: () => {
                                setState(() {
                                  _initialValue = _dataList[i][_value];
                                  onChanged!(_initialValue);
                                })
                              },
                              mainAxisAlignment: MainAxisAlignment.start,
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                if (_dataList[i][_value] == _initialValue) ...[
                                  Image(
                                      image: AssetList.$danxuan_png_image,
                                      height: 17.pt),
                                ] else ...[
                                  Image(
                                      image: AssetList.$danxuanyuan_png_image,
                                      height: 17.pt),
                                ],
                                SizedBox(width: 8.pt),
                                WisText(
                                  '${_dataList[i][_label]}',
                                  size: 15.pt,
                                  color: Color(0xff222222),
                                  weight: FontWeight.w300,
                                ).asExpanded(),
                              ],
                            ),
                  ],
                )
              : Column(
                  children: [
                    for (num i = 0; i < _dataList!.length; i++)
                      Wisdom.row(
                        onTap: () => {
                          setState(() {
                            _initialValue = _dataList[i as int][_value];
                            onChanged!(_initialValue);
                          })
                        },
                        mainAxisAlignment: MainAxisAlignment.start,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        margin: EdgeInsets.only(
                            bottom:
                                (i + 1) == _dataList.length ? 15.pt : 26.pt),
                        children: [
                          if (_dataList[i as int][_value] == _initialValue) ...[
                            Image(
                                image: AssetList.$danxuan_png_image,
                                height: 17.pt),
                          ] else ...[
                            Image(
                                image: AssetList.$danxuanyuan_png_image,
                                height: 17.pt),
                          ],
                          SizedBox(width: 8.pt),
                          WisText(
                            '${_dataList[i][_label]}',
                            size: 15.pt,
                            color: Color(0xff222222),
                            weight: FontWeight.w300,
                          ).asExpanded(),
                        ],
                      )
                  ],
                );
        },
      ),
    );
  }
}
