/*
 * @Author: XianKaiQun
 * @Date: 2020-08-25 14:53:57
 * @LastEditors: XianKaiQun
 * @LastEditTime: 2020-09-09 11:27:16
 * @Description: 
 */
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

///表单的结构样式构造
class WFormTile extends StatelessWidget {
  const WFormTile({
    Key? key,
    this.child,
    this.leading,
    this.direction = Axis.horizontal,
    this.required = false,
    this.minWidth,
  }) : super(key: key);

  final Widget? child;
  final Widget? leading;
  final double? minWidth;

  ///布局方向,
  ///[Axis.horizontal][leading]在左，
  ///[Axis.vertical][leading]在上，
  final Axis direction;

  ///显示必填的星星
  final bool required;
  @override
  Widget build(BuildContext context) {
    Widget _leading = DefaultTextStyle(
      style: DefaultTextStyle.of(context).style.copyWith(fontSize: 15.pt),
      child: Wisdom.rich(
        textAlign: TextAlign.start,
        children: [
          if (required) ...[
            WidgetSpan(
              child: SizedBox(
                width: 10.pt,
                child: Text(
                  '*',
                  style: TextStyle(
                    color: Color(0XFFFF495E),
                  ),
                ),
              ),
            ),
          ],
          if (leading != null)
            WidgetSpan(
              child: leading!,
            ),
        ],
      ),
    );

    if (direction == Axis.horizontal) {
      _leading = ConstrainedBox(
        constraints: BoxConstraints(
          minWidth: minWidth ?? 85.pt,
        ),
        child: _leading,
      );
    }

    Widget? _child = child;
    if (_child != null) {
      _child = DefaultTextStyle(
        style: DefaultTextStyle.of(context).style.copyWith(
              fontSize: 15.pt,
            ),
        child: _child,
      );
      if (direction == Axis.horizontal) {
        _child = Expanded(
          child: _child,
        );
      }
    }

    return ConstrainedBox(
      constraints: BoxConstraints(minHeight: 45.pt),
      child: direction == Axis.vertical
          ? Wisdom.column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                SizedBox(height: 15.pt),
                _leading,
                if (_child != null) _child,
                SizedBox(height: 10.pt),
              ],
            )
          : Wisdom.row(
              // margin: EdgeInsets.only(bottom: 10.pt),
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                _leading,
                if (_child != null) _child,
              ],
            ),
    );
  }
}
