/*
 * @Author: XianKaiQun
 * @Date: 2020-07-10 11:40:00
 * @LastEditors  : WuWei
 * @LastEditTime : 2022-01-19 11:25:57
 * @Description: 
 */

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

class WisForm extends StatelessWidget {
  const WisForm({
    Key? key,
    this.formKey,
    this.child,
    this.autoLostFocus = true,
    this.autovalidateMode,
    this.onWillPop,
    this.onChanged,
  }) : super(key: key);
  final GlobalKey<FormState>? formKey;
  final Widget? child;
  final bool autoLostFocus;
  final AutovalidateMode? autovalidateMode;
  final WillPopCallback? onWillPop;
  final VoidCallback? onChanged;

  static FormState? of(BuildContext context) =>
      context.findAncestorStateOfType<FormState>();

  ///失去焦点
  static void lostFocus(BuildContext context) =>
      FocusScope.of(context).requestFocus(FocusNode());

  @override
  Widget build(BuildContext context) {
    Widget current = Form(
      key: formKey,
      child: child!,
      onWillPop: onWillPop,
      onChanged: onChanged,
      autovalidateMode: autovalidateMode,
    );
    if (autoLostFocus) {
      current = GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: () => FocusScope.of(context).requestFocus(FocusNode()),
        child: current,
      );
    }
    return current;
  }
}
