Skip to content

Commit 74f5368

Browse files
committed
Fix error handling to not set errorText if error is set.
1 parent abd7e68 commit 74f5368

File tree

2 files changed

+91
-2
lines changed

2 files changed

+91
-2
lines changed

lib/vsc_datetime_field.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,11 @@ class VscDatetimeFieldState extends State<VscDatetimeField> {
269269
focusNode: _effectiveFocusNode,
270270
controller: _effectiveController,
271271
decoration: widget.textFieldConfiguration.decoration.copyWith(
272-
errorText: _internalErrorText ??
273-
widget.textFieldConfiguration.decoration.errorText,
272+
// Only set errorText if there's no error widget already provided
273+
errorText: widget.textFieldConfiguration.decoration.error == null
274+
? (_internalErrorText ??
275+
widget.textFieldConfiguration.decoration.errorText)
276+
: null,
274277
suffixIcon: Semantics(
275278
identifier: 'id_datetime_field_suffix_icon',
276279
child: InkResponse(
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
import 'package:vsc_datetime_field/vsc_datetime_field.dart';
4+
5+
void main() {
6+
group('VscDatetimeField error handling', () {
7+
testWidgets('handles error widget in decoration without assertion error',
8+
(WidgetTester tester) async {
9+
// Build a VscDatetimeField with an error widget in the decoration
10+
await tester.pumpWidget(
11+
MaterialApp(
12+
home: Scaffold(
13+
body: VscDatetimeField(
14+
textFieldConfiguration: const TextFieldConfiguration(
15+
decoration: InputDecoration(
16+
labelText: 'Test Field',
17+
// This simulates what UiEditorDatetimeField does
18+
error: Text('External validation error'),
19+
errorMaxLines: 3,
20+
),
21+
),
22+
),
23+
),
24+
),
25+
);
26+
27+
// The widget should build without throwing an assertion error
28+
expect(find.byType(VscDatetimeField), findsOneWidget);
29+
expect(find.text('External validation error'), findsOneWidget);
30+
31+
// Now trigger an internal error by typing invalid date
32+
await tester.enterText(find.byType(TextField), '5/3/');
33+
await tester.pump();
34+
35+
// Should still show only the external error widget, not internal errorText
36+
expect(find.text('External validation error'), findsOneWidget);
37+
expect(find.text('Invalid value'), findsNothing);
38+
});
39+
40+
testWidgets('shows internal error when no external error widget exists',
41+
(WidgetTester tester) async {
42+
await tester.pumpWidget(
43+
MaterialApp(
44+
home: Scaffold(
45+
body: VscDatetimeField(
46+
textFieldConfiguration: const TextFieldConfiguration(
47+
decoration: InputDecoration(
48+
labelText: 'Test Field',
49+
// No error widget provided
50+
),
51+
),
52+
),
53+
),
54+
),
55+
);
56+
57+
// Enter invalid date
58+
await tester.enterText(find.byType(TextField), '5/3/');
59+
await tester.pump();
60+
61+
// Should show the internal error text
62+
expect(find.text('Invalid value'), findsOneWidget);
63+
});
64+
65+
testWidgets('respects existing errorText when no internal error',
66+
(WidgetTester tester) async {
67+
await tester.pumpWidget(
68+
MaterialApp(
69+
home: Scaffold(
70+
body: VscDatetimeField(
71+
textFieldConfiguration: const TextFieldConfiguration(
72+
decoration: InputDecoration(
73+
labelText: 'Test Field',
74+
errorText: 'Pre-existing error text',
75+
),
76+
),
77+
),
78+
),
79+
),
80+
);
81+
82+
// Should show the pre-existing error text
83+
expect(find.text('Pre-existing error text'), findsOneWidget);
84+
});
85+
});
86+
}

0 commit comments

Comments
 (0)