2024-03-20 18:31:37 +08:00
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:get/get.dart';
|
|
|
|
|
import 'package:sk_base_mobile/util/screen_adaper_util.dart';
|
|
|
|
|
|
2024-04-01 08:41:52 +08:00
|
|
|
|
class SkDatePicker extends StatelessWidget {
|
2024-03-20 18:31:37 +08:00
|
|
|
|
final TextEditingController textController;
|
|
|
|
|
final VoidCallback? onClear;
|
|
|
|
|
final Function? onDateSelected;
|
2024-03-21 14:09:49 +08:00
|
|
|
|
final bool isRequired;
|
|
|
|
|
final String labelText;
|
|
|
|
|
|
2024-04-01 08:41:52 +08:00
|
|
|
|
const SkDatePicker({
|
2024-03-20 18:31:37 +08:00
|
|
|
|
super.key,
|
|
|
|
|
this.onClear,
|
|
|
|
|
this.onDateSelected,
|
2024-03-21 14:09:49 +08:00
|
|
|
|
this.labelText = '日期',
|
|
|
|
|
this.isRequired = false,
|
2024-03-20 18:31:37 +08:00
|
|
|
|
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,
|
2024-03-28 17:18:46 +08:00
|
|
|
|
size: ScreenAdaper.height(40),
|
2024-03-20 18:31:37 +08:00
|
|
|
|
),
|
|
|
|
|
suffixIcon: textController.text.isNotEmpty
|
|
|
|
|
? IconButton(
|
|
|
|
|
icon: const Icon(Icons.clear),
|
|
|
|
|
// 当点击这个按钮时,清除TextFormField的值
|
|
|
|
|
onPressed: () {
|
|
|
|
|
textController.clear();
|
|
|
|
|
if (onClear != null) {
|
|
|
|
|
onClear!();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
: const SizedBox(),
|
|
|
|
|
hintText: '请选择',
|
2024-03-21 14:09:49 +08:00
|
|
|
|
label: Row(
|
2024-03-20 18:31:37 +08:00
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
|
children: [
|
2024-03-21 14:09:49 +08:00
|
|
|
|
if (isRequired)
|
|
|
|
|
Text(
|
|
|
|
|
"*",
|
|
|
|
|
style: TextStyle(
|
2024-03-28 17:18:46 +08:00
|
|
|
|
color: Colors.red, fontSize: ScreenAdaper.height(30)),
|
2024-03-21 14:09:49 +08:00
|
|
|
|
),
|
2024-03-20 18:31:37 +08:00
|
|
|
|
Text(
|
2024-03-21 14:09:49 +08:00
|
|
|
|
labelText,
|
2024-03-28 17:18:46 +08:00
|
|
|
|
style: TextStyle(fontSize: ScreenAdaper.height(30)),
|
2024-03-20 18:31:37 +08:00
|
|
|
|
),
|
|
|
|
|
])),
|
|
|
|
|
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)));
|
2024-03-21 14:09:49 +08:00
|
|
|
|
|
2024-03-20 18:31:37 +08:00
|
|
|
|
if (onDateSelected != null) {
|
|
|
|
|
onDateSelected!(picker);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|