From 0525e9d3b6d58b017d06ebefda303e5908673dde Mon Sep 17 00:00:00 2001 From: Freek van de Ven Date: Fri, 26 Jul 2024 14:52:14 +0200 Subject: [PATCH] fix: add pause validation directly in the pause dialog when saving --- .../lib/src/ui/widgets/pause_selection.dart | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/packages/flutter_availability/lib/src/ui/widgets/pause_selection.dart b/packages/flutter_availability/lib/src/ui/widgets/pause_selection.dart index 9148b04..f46f049 100644 --- a/packages/flutter_availability/lib/src/ui/widgets/pause_selection.dart +++ b/packages/flutter_availability/lib/src/ui/widgets/pause_selection.dart @@ -277,18 +277,28 @@ class _AvailabilityBreakSelectionDialogState }); } + Future onSave() async { + AvailabilityError? error; + try { + _breakViewModel.toBreak().validate(); + Navigator.of(context).pop(_breakViewModel); + } on BreakEndBeforeStartException { + error = AvailabilityError.breakEndBeforeStart; + } on BreakSubmittedDurationTooLongException { + error = AvailabilityError.breakSubmittedDurationTooLong; + } + + if (error != null && context.mounted) { + await options.errorDisplayBuilder( + context, + error, + ); + } + } + var canSave = _breakViewModel.canSave; - var onSaveButtonPress = canSave - ? () { - if (_breakViewModel.isValid) { - Navigator.of(context).pop(_breakViewModel); - } else { - debugPrint("Break is not valid"); - // TODO(freek): show error message - } - } - : null; + var onSaveButtonPress = canSave ? onSave : null; var saveButton = options.primaryButtonBuilder( context,