feat(client): 录单表单优化 + 可搜索下拉支持新建 + 日期选择器组件

新增 DatePickerField 日期选择器与 date_util;可搜索下拉 SearchableOptionField
支持内联新建选项;入库/出库录单表单、商品页、财务页、app_shell 导航相应调整。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
wangjia
2026-06-20 07:05:22 +08:00
parent e892ab45b2
commit a76724b385
12 changed files with 2868 additions and 1624 deletions
+179
View File
@@ -0,0 +1,179 @@
import 'package:flutter/material.dart';
import '../core/theme/app_theme.dart';
import '../core/utils/date_util.dart';
/// 年/月/日 三个可编辑下拉框的日期选择组件,替代原生 showDatePicker。
///
/// - 每个下拉是 Material 3 `DropdownMenu`:点开可选,键入数字即过滤定位
/// (年/月/日均为有界域,条目覆盖全范围,等价于「手动输入数字」)。
/// - 改月/年时把超出当月的「日」自动 clamp 回当月最大值。
/// - 值以 `yyyy-MM-dd` 字符串进出([value] / [onChanged]);三者未填齐时回调 null。
class DatePickerField extends StatefulWidget {
final String? value; // yyyy-MM-dd
final ValueChanged<String?> onChanged;
final bool isRequired;
final String? label;
const DatePickerField({
super.key,
this.value,
required this.onChanged,
this.isRequired = false,
this.label,
});
@override
State<DatePickerField> createState() => _DatePickerFieldState();
}
class _DatePickerFieldState extends State<DatePickerField> {
int? _year;
int? _month;
int? _day;
@override
void initState() {
super.initState();
_parse(widget.value);
}
@override
void didUpdateWidget(DatePickerField old) {
super.didUpdateWidget(old);
if (old.value != widget.value) {
_parse(widget.value);
}
}
void _parse(String? v) {
final d = parseYmd(v);
_year = d?.year;
_month = d?.month;
_day = d?.day;
}
int get _daysInMonth {
final y = _year ?? DateTime.now().year;
final m = _month ?? 1;
return DateTime(y, m + 1, 0).day; // 下月第 0 天 = 当月最后一天
}
String? get _composed => composeYmd(_year, _month, _day);
void _emit(FormFieldState<String> field) {
// 改月/年后把超界的日 clamp 回当月最大值(与 composeYmd 一致,State 同步显示)
if (_day != null && _day! > _daysInMonth) {
_day = _daysInMonth;
}
final v = _composed;
field.didChange(v);
widget.onChanged(v);
setState(() {});
}
@override
Widget build(BuildContext context) {
final now = DateTime.now();
final years = [for (var y = now.year - 20; y <= now.year + 5; y++) y];
final months = [for (var m = 1; m <= 12; m++) m];
final days = [for (var d = 1; d <= _daysInMonth; d++) d];
// 每个下拉用 Expanded 撑满分得的宽度(expandedInsets:zero 让 DropdownMenu 填满父级),
// trailingIcon 用紧凑小箭头,避免默认大图标按钮挤掉数字(曾导致「2026」被裁成「007」)。
Widget menu({
required String label,
required int? value,
required List<int> items,
required ValueChanged<int?> onSel,
required FormFieldState<String> field,
}) {
return DropdownMenu<int>(
initialSelection: value,
label: Text(label, style: const TextStyle(fontSize: 11)),
enableFilter: true,
requestFocusOnTap: true,
textStyle: const TextStyle(fontSize: 13),
menuHeight: 280,
expandedInsets: EdgeInsets.zero,
trailingIcon: const Icon(Icons.arrow_drop_down, size: 18),
selectedTrailingIcon: const Icon(Icons.arrow_drop_up, size: 18),
inputDecorationTheme: const InputDecorationTheme(
isDense: true,
contentPadding: EdgeInsets.symmetric(horizontal: 6, vertical: 8),
),
dropdownMenuEntries: [
for (final i in items) DropdownMenuEntry<int>(value: i, label: '$i'),
],
onSelected: onSel,
);
}
return FormField<String>(
initialValue: widget.value,
validator: widget.isRequired
? (v) => (_composed == null ? '请选择日期' : null)
: null,
builder: (field) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Row(
children: [
Expanded(
flex: 5,
child: menu(
label: '',
value: _year,
items: years,
field: field,
onSel: (v) {
_year = v;
_emit(field);
},
),
),
const SizedBox(width: 6),
Expanded(
flex: 4,
child: menu(
label: '',
value: _month,
items: months,
field: field,
onSel: (v) {
_month = v;
_emit(field);
},
),
),
const SizedBox(width: 6),
Expanded(
flex: 4,
child: menu(
label: '',
value: _day,
items: days,
field: field,
onSel: (v) {
_day = v;
_emit(field);
},
),
),
],
),
if (field.errorText != null)
Padding(
padding: const EdgeInsets.only(left: 12, top: 4),
child: Text(
field.errorText!,
style: const TextStyle(fontSize: 11, color: AppTheme.danger),
),
),
],
);
},
);
}
}
+108 -11
View File
@@ -13,8 +13,9 @@ class OptionItem {
late final String _initials;
OptionItem({required this.id, required this.name, this.code}) {
_fullPinyin = PinyinHelper.getPinyinE(name, separator: '', defPinyin: '').toLowerCase();
_initials = PinyinHelper.getShortPinyin(name).toLowerCase();
_fullPinyin = PinyinHelper.getPinyinE(name, separator: '', defPinyin: '')
.toLowerCase();
_initials = PinyinHelper.getShortPinyin(name).toLowerCase();
}
bool matches(String kw) {
@@ -36,6 +37,10 @@ class SearchableOptionField extends StatelessWidget {
final bool isRequired;
final bool isDense;
/// 可选:搜索无匹配时「新增到基础数据」。回调收到当前关键字(用户可在确认框里改),
/// 创建成功返回新选项 id(自动选中),失败/取消返回 null。为空则不显示新增入口。
final Future<int?> Function(String keyword)? onCreate;
const SearchableOptionField({
super.key,
required this.options,
@@ -45,6 +50,7 @@ class SearchableOptionField extends StatelessWidget {
required this.onChanged,
this.isRequired = false,
this.isDense = true,
this.onCreate,
});
String get _displayText {
@@ -59,6 +65,7 @@ class SearchableOptionField extends StatelessWidget {
title: dialogTitle,
options: options,
selectedId: selectedId,
onCreate: onCreate,
),
);
// result == -1 means "clear selection"
@@ -86,9 +93,11 @@ class SearchableOptionField extends StatelessWidget {
suffixIcon: selected
? GestureDetector(
onTap: () => onChanged(null),
child: const Icon(Icons.close, size: 14, color: AppTheme.textSecondary),
child: const Icon(Icons.close,
size: 14, color: AppTheme.textSecondary),
)
: const Icon(Icons.arrow_drop_down, size: 16, color: AppTheme.textSecondary),
: const Icon(Icons.arrow_drop_down,
size: 16, color: AppTheme.textSecondary),
),
isEmpty: !selected,
child: Text(
@@ -110,7 +119,13 @@ class _SearchDialog extends StatefulWidget {
final String title;
final List<OptionItem> options;
final int? selectedId;
const _SearchDialog({required this.title, required this.options, this.selectedId});
final Future<int?> Function(String keyword)? onCreate;
const _SearchDialog({
required this.title,
required this.options,
this.selectedId,
this.onCreate,
});
@override
State<_SearchDialog> createState() => _SearchDialogState();
@@ -119,6 +134,7 @@ class _SearchDialog extends StatefulWidget {
class _SearchDialogState extends State<_SearchDialog> {
final _ctrl = TextEditingController();
String _keyword = '';
bool _creating = false;
@override
void dispose() {
@@ -126,11 +142,68 @@ class _SearchDialogState extends State<_SearchDialog> {
super.dispose();
}
/// 「新增到基础数据」流程:确认(名称可改)→ onCreate → 成功则带新 id 关闭搜索框。
Future<void> _createFlow() async {
final kw = _keyword.trim();
if (widget.onCreate == null || kw.isEmpty) return;
final nameCtrl = TextEditingController(text: kw);
final confirmed = await showAppDialog<bool>(
context: context,
builder: (_) => AlertDialog(
title: const Text('新增到基础数据', style: TextStyle(fontSize: 16)),
content: SizedBox(
width: context.dialogWidth(320),
child: TextField(
controller: nameCtrl,
autofocus: true,
decoration: const InputDecoration(labelText: '名称', isDense: true),
),
),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(false),
child: const Text('取消'),
),
TextButton(
onPressed: () => Navigator.of(context).pop(true),
child: const Text('确认新增'),
),
],
),
);
if (confirmed != true) return;
final name = nameCtrl.text.trim();
if (name.isEmpty) return;
setState(() => _creating = true);
try {
final newId = await widget.onCreate!(name);
if (!mounted) return;
if (newId != null) {
Navigator.of(context).pop(newId); // 回填并自动选中
} else {
setState(() => _creating = false);
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('新增失败,请重试')),
);
}
} catch (e) {
if (!mounted) return;
setState(() => _creating = false);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('新增失败:$e')),
);
}
}
@override
Widget build(BuildContext context) {
final filtered = _keyword.isEmpty
? widget.options
: widget.options.where((o) => o.matches(_keyword)).toList();
final kw = _keyword.trim();
final hasExact =
widget.options.any((o) => o.name.toLowerCase() == kw.toLowerCase());
final canCreate = widget.onCreate != null && kw.isNotEmpty && !hasExact;
return AlertDialog(
title: Text(widget.title, style: const TextStyle(fontSize: 16)),
@@ -153,8 +226,12 @@ class _SearchDialogState extends State<_SearchDialog> {
const SizedBox(height: 8),
Expanded(
child: filtered.isEmpty
? const Center(
child: Text('无匹配结果', style: TextStyle(color: AppTheme.textSecondary)))
? Center(
child: Text(
canCreate ? '无匹配结果,可新增到基础数据' : '无匹配结果',
style: const TextStyle(color: AppTheme.textSecondary),
),
)
: ListView.builder(
itemCount: filtered.length,
itemBuilder: (_, i) {
@@ -162,17 +239,36 @@ class _SearchDialogState extends State<_SearchDialog> {
final isSelected = opt.id == widget.selectedId;
return ListTile(
dense: true,
title: Text(opt.name, style: const TextStyle(fontSize: 13)),
title: Text(opt.name,
style: const TextStyle(fontSize: 13)),
subtitle: opt.code != null && opt.code!.isNotEmpty
? Text(opt.code!, style: const TextStyle(fontSize: 11))
? Text(opt.code!,
style: const TextStyle(fontSize: 11))
: null,
selected: isSelected,
selectedTileColor: AppTheme.primary.withValues(alpha: 0.08),
selectedTileColor:
AppTheme.primary.withValues(alpha: 0.08),
onTap: () => Navigator.of(context).pop(opt.id),
);
},
),
),
if (canCreate)
Align(
alignment: Alignment.centerLeft,
child: TextButton.icon(
onPressed: _creating ? null : _createFlow,
icon: _creating
? const SizedBox(
width: 14,
height: 14,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Icon(Icons.add, size: 16),
label: Text('新增「$kw」到基础数据',
style: const TextStyle(fontSize: 12)),
),
),
],
),
),
@@ -180,7 +276,8 @@ class _SearchDialogState extends State<_SearchDialog> {
if (widget.selectedId != null)
TextButton(
onPressed: () => Navigator.of(context).pop(-1),
child: const Text('清除选择', style: TextStyle(color: AppTheme.textSecondary)),
child: const Text('清除选择',
style: TextStyle(color: AppTheme.textSecondary)),
),
TextButton(
onPressed: () => Navigator.of(context).pop(null),