/*
 * @Author       : WuWei
 * @LastEditors  : WuWei
 * @Date         : 2022-08-16 17:20:44
 * @LastEditTime : 2023-05-25 15:48:53
 * @Description  : Do not edit
 */
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

class WFYearPicker extends StatelessWidget {
  const WFYearPicker({
    Key? key,
    required this.title,
    this.isRequired = false,
    this.hintText,
    required this.onChanged,
    this.initialYear,
    this.type,
    this.minuteInterval,
  }) : super(key: key);
  final Widget title;

  ///是否必须
  final bool? isRequired;
  final String? hintText;
  final dynamic initialYear;
  final void Function(dynamic datetime) onChanged;
  final String? type;
  final int? minuteInterval;

  @override
  Widget build(BuildContext context) {
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    String? _initialDate = initialYear;
    return WisFormLayoutComponent(
      title: title,
      isRequired: isRequired,
      axis: Axis.horizontal,
      child: StatefulBuilder(
        builder: (BuildContext context, setState) {
          return Row(
            children: [
              TextField(
                controller: TextEditingController(
                  text: _initialDate ?? '',
                ),
                readOnly: true,
                onTap: () async {
                  final res = await WPickerUtil.year(
                    context,
                    title: title,
                  );

                  if (res != null) {
                    _initialDate = res.toFormat('{YYYY}');
                    setState(() {});
                    onChanged(_initialDate);
                  }
                },
                style: TextStyle(
                  fontSize: 14.pt,
                  color: colorScheme.minor,
                ),
                decoration: InputDecoration(
                  fillColor: Colors.transparent,
                  filled: true,
                  hintText: hintText,
                  hintStyle: TextStyle(
                    fontSize: 14.pt,
                    color: colorScheme.placeholder,
                    fontWeight: FontWeight.w300,
                  ),
                ),
              ).asExpanded(),
              _initialDate != null
                  ? WFClose(
                      onTap: () {
                        _initialDate = null;
                        setState(() {});
                        onChanged(null);
                      },
                    )
                  : Icon(
                      FaIcons.angle_right,
                      size: 18.pt,
                      color: colorScheme.placeholder,
                    )
            ],
          );
        },
      ),
    );
  }
}
