feat: add Flutter schema-driven form cards
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import 'package:aioa_mobile/core/forms/schema/form_schema.dart';
|
||||
import 'package:aioa_mobile/core/forms/schema/form_validator.dart';
|
||||
import 'package:aioa_mobile/features/form/domain/leave_form_definition.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
final leaveDraftProvider =
|
||||
NotifierProvider<LeaveDraftController, DynamicFormState>(
|
||||
LeaveDraftController.new,
|
||||
);
|
||||
|
||||
class LeaveDraftController extends Notifier<DynamicFormState> {
|
||||
@override
|
||||
DynamicFormState build() => const DynamicFormState();
|
||||
|
||||
void setValue(String field, Object? value) {
|
||||
final values = {...state.values, field: value};
|
||||
final errors = {...state.errors}..remove(field);
|
||||
state = state.copyWith(values: values, errors: errors);
|
||||
}
|
||||
|
||||
void applyAiSuggestion() {
|
||||
final start = DateTime.now().add(const Duration(days: 1));
|
||||
final normalizedStart = DateTime(
|
||||
start.year,
|
||||
start.month,
|
||||
start.day,
|
||||
13,
|
||||
30,
|
||||
);
|
||||
final end = normalizedStart.add(const Duration(hours: 4));
|
||||
state = DynamicFormState(
|
||||
values: {
|
||||
'type': 'PERSONAL',
|
||||
'startsAt': normalizedStart.toUtc().toIso8601String(),
|
||||
'endsAt': end.toUtc().toIso8601String(),
|
||||
'reason': '办理个人事务,已提前完成工作交接。',
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
bool validate() {
|
||||
final errors = validateDynamicForm(
|
||||
leaveFormDefinition.dataSchema,
|
||||
state.values,
|
||||
);
|
||||
final startsAt = DateTime.tryParse(
|
||||
state.values['startsAt'] as String? ?? '',
|
||||
);
|
||||
final endsAt = DateTime.tryParse(state.values['endsAt'] as String? ?? '');
|
||||
if (startsAt != null && endsAt != null && !endsAt.isAfter(startsAt)) {
|
||||
errors['endsAt'] = '结束时间必须晚于开始时间';
|
||||
}
|
||||
state = state.copyWith(errors: errors);
|
||||
return errors.isEmpty;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user