/*
 * @Author: XianKaiQun
 * @LastEditors: wjc
 * @Date: 2020-10-27 15:59:44
 * @LastEditTime: 2022-12-07 17:24:06
 */
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

class WisFormLayoutComponent extends StatelessWidget {
  const WisFormLayoutComponent({
    Key? key,
    this.isRequired = false,
    this.isShowRequired = true,
    this.title,
    this.padding = EdgeInsets.zero,
    required this.child,
    this.axis = Axis.horizontal,
    this.validator,
    this.titleWidth,
  }) : super(key: key);

  ///是否必须
  final bool? isRequired;

  ///是否显示必填星号
  final bool isShowRequired;

  ///标题文字
  final Widget? title;
  final EdgeInsetsGeometry? padding;
  final Widget child;

  ///布局
  final Axis? axis;
  final bool Function()? validator;
  final double? titleWidth;

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

    final children = [
      (axis == Axis.horizontal)
          ? title != null
              ? Container(
                  width: titleWidth ?? 100.pt,
                  height: 44.pt,
                  alignment: Alignment.centerLeft,
                  child: Text.rich(
                    TextSpan(
                      children: [
                        if (isRequired == true && isShowRequired) ...[
                          WidgetSpan(
                            child: SizedBox(
                              width: 10.pt,
                              child: Text(
                                '*',
                                style: TextStyle(
                                  color: colorScheme.danger,
                                  fontSize: 16.pt,
                                  fontWeight: FontWeight.w300,
                                ),
                              ),
                            ),
                          ),
                        ] else ...[
                          WidgetSpan(
                            child: Text(''),
                          ),
                        ],
                        WidgetSpan(
                          child: title!,
                        ),
                      ],
                    ),
                    style: TextStyle(
                      color: colorScheme.mainTitle,
                      fontSize: 15.pt,
                      fontWeight: FontWeight.w300,
                    ),
                    textAlign: TextAlign.center,
                  ),
                )
              : Container(height: 0)
          : title != null
              ? Wisdom.row(
                  margin: EdgeInsets.symmetric(vertical: 10.pt),
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    if (isRequired == true && isShowRequired) ...[
                      SizedBox(
                        height: 10.pt,
                        child: Text(
                          '*',
                          style: TextStyle(
                            color: colorScheme.danger,
                            fontSize: 16.pt,
                            fontWeight: FontWeight.w300,
                          ),
                        ),
                      ),
                    ] else ...[
                      Text(''),
                    ],
                    title!.asExpanded(),
                  ],
                )
              : Container(height: 0),
      () {
        Widget current = Container(
          alignment: axis == Axis.horizontal
              ? Alignment.centerRight
              : Alignment.centerLeft,
          constraints: BoxConstraints(minHeight: 44.pt),
          child: child,
        );

        if (axis == Axis.horizontal) {
          current = current.asExpanded();
        }
        return current;
      }()
    ];
    Widget current = (axis == Axis.vertical)
        ? Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: children,
          )
        : Row(
            // mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: children,
          );
    if ((padding ?? EdgeInsets.zero) != EdgeInsets.zero) {
      current = Padding(
        padding: padding!,
        child: current,
      );
    }
    return current;
  }
}
