115 lines
3.6 KiB
Dart
115 lines
3.6 KiB
Dart
import 'package:flutter/material.dart';
|
||
import 'package:sk_base_mobile/app_theme.dart';
|
||
import 'package:sk_base_mobile/util/screen_adaper_util.dart';
|
||
import 'package:sk_datetime_picker/sk_datetime_picker.dart';
|
||
|
||
class SkDateTimePicker extends StatelessWidget {
|
||
final TextEditingController textController;
|
||
final VoidCallback? onClear;
|
||
final Function(DateTime?)? onSelected;
|
||
final bool isRequired;
|
||
final String labelText;
|
||
final DateTime? initialDate;
|
||
const SkDateTimePicker({
|
||
super.key,
|
||
this.onClear,
|
||
this.onSelected,
|
||
this.initialDate,
|
||
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 {
|
||
DateTime? dateTime = await showSkDateTimePicker(
|
||
theme: theme,
|
||
is24HourMode: true,
|
||
title: Container(
|
||
padding: EdgeInsets.symmetric(vertical: ScreenAdaper.height(10)),
|
||
child: Text(
|
||
'选择日期时间',
|
||
style: TextStyle(fontSize: ScreenAdaper.height(30)),
|
||
),
|
||
),
|
||
context: context,
|
||
initialDate: initialDate ?? DateTime.now(),
|
||
firstDate: DateTime(1600).subtract(const Duration(days: 3652)),
|
||
lastDate: DateTime.now().add(
|
||
const Duration(days: 3652),
|
||
),
|
||
isForce2Digits: true,
|
||
isShowSeconds: false,
|
||
minutesInterval: 1,
|
||
secondsInterval: 1,
|
||
borderRadius: const BorderRadius.all(Radius.circular(16)),
|
||
constraints: const BoxConstraints(
|
||
maxWidth: 350,
|
||
maxHeight: 650,
|
||
),
|
||
transitionBuilder: (context, anim1, anim2, child) {
|
||
return FadeTransition(
|
||
opacity: anim1.drive(
|
||
Tween(
|
||
begin: 0,
|
||
end: 1,
|
||
),
|
||
),
|
||
child: child,
|
||
);
|
||
},
|
||
transitionDuration: const Duration(milliseconds: 200),
|
||
barrierDismissible: true,
|
||
selectableDayPredicate: (dateTime) {
|
||
return true;
|
||
},
|
||
);
|
||
if (onSelected != null) {
|
||
onSelected!(dateTime);
|
||
}
|
||
},
|
||
);
|
||
}
|
||
}
|