import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:sk_base_mobile/util/screen_adaper_util.dart'; class SkDatePicker extends StatelessWidget { final TextEditingController textController; final VoidCallback? onClear; final Function? onDateSelected; final bool isRequired; final String labelText; const SkDatePicker({ super.key, this.onClear, this.onDateSelected, this.labelText = '日期', this.isRequired = false, required this.textController, }); @override Widget build(BuildContext context) { return TextFormField( controller: textController, onTapOutside: (event) { FocusScope.of(context).unfocus(); }, decoration: InputDecoration( floatingLabelBehavior: FloatingLabelBehavior.always, prefixIcon: Icon( Icons.date_range_outlined, size: ScreenAdaper.height(40), ), suffixIcon: textController.text.isNotEmpty ? IconButton( icon: const Icon(Icons.clear), // 当点击这个按钮时,清除TextFormField的值 onPressed: () { textController.clear(); if (onClear != null) { onClear!(); } }, ) : const SizedBox(), hintText: '请选择', label: Row( crossAxisAlignment: CrossAxisAlignment.center, mainAxisSize: MainAxisSize.min, children: [ if (isRequired) Text( "*", style: TextStyle( color: Colors.red, fontSize: ScreenAdaper.height(30)), ), Text( labelText, style: TextStyle(fontSize: ScreenAdaper.height(30)), ), ])), keyboardType: TextInputType.none, onTap: () async { final picker = await showDatePicker( context: Get.overlayContext!, initialDate: DateTime.now(), helpText: '选择的日期', confirmText: '确定', cancelText: '取消', firstDate: DateTime(2000, 1, 1), lastDate: DateTime.now().add(const Duration(days: 7))); if (onDateSelected != null) { onDateSelected!(picker); } }, ); } }