Files
task_hub/lib/widgets/common.dart
2026-04-26 14:23:32 +08:00

107 lines
2.8 KiB
Dart

import 'package:fluent_ui/fluent_ui.dart';
import 'package:intl/intl.dart';
import 'package:omni_datetime_picker/omni_datetime_picker.dart';
Widget buildFormItem({
required String label,
required Widget child,
bool required = false,
}) {
return Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
width: 80,
child: Padding(
padding: const EdgeInsets.only(top: 8),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
if (required)
Padding(
padding: EdgeInsets.only(right: 4),
child: Text(
'*',
style: TextStyle(color: Colors.red, fontSize: 16),
),
),
Text(
label,
style: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.w500,
),
),
const Text(':', style: TextStyle(fontSize: 14)),
],
),
),
),
const SizedBox(width: 12),
// 右侧表单控件
Expanded(child: child),
],
);
}
// 显示日期/日期时间的按钮组件
Widget buildOmniDateButton({
required BuildContext context,
required DateTime? selectedDate,
required String placeholder,
required OmniDateTimePickerType type,
required ValueChanged<DateTime?> onChanged,
}) {
String displayText = '$placeholder';
if (selectedDate != null) {
switch (type) {
case OmniDateTimePickerType.date:
displayText = DateFormat('yyyy-MM-dd').format(selectedDate);
break;
case OmniDateTimePickerType.time:
displayText = DateFormat('HH:mm:ss').format(selectedDate);
break;
case OmniDateTimePickerType.dateAndTime:
displayText = DateFormat('yyyy-MM-dd HH:mm:ss').format(selectedDate);
break;
}
}
return Button(
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(FluentIcons.calendar, size: 16),
SizedBox(width: 8),
Text(
displayText,
style: TextStyle(
color: selectedDate == null ? Colors.grey[130] : null,
),
),
],
),
onPressed: () async {
final DateTime? picked = await showOmniDateTimePicker(
context: context,
initialDate: selectedDate ?? DateTime.now(),
firstDate: DateTime(
DateTime.now().year,
DateTime.now().month,
DateTime.now().day,
),
lastDate: DateTime.now().add(Duration(days: 365 * 2)),
is24HourMode: true,
isShowSeconds: false,
type: type,
barrierColor: Colors.grey,
);
if (picked != null) {
onChanged(picked);
}
},
);
}