Skip to content

Commit a36884d

Browse files
authored
feat(ui_auth): add a way to customize ErrorText message (#119)
* feat(ui_auth): add a way to customize ErrorText * add tests * add license header
1 parent 8757037 commit a36884d

File tree

2 files changed

+94
-5
lines changed

2 files changed

+94
-5
lines changed

Diff for: packages/firebase_ui_auth/lib/src/widgets/error_text.dart

+31-5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,28 @@ String? localizedErrorText(
3636
/// A widget which displays error text for a given Firebase error code.
3737
/// {@endtemplate}
3838
class ErrorText extends StatelessWidget {
39+
/// A way to customize localized error messages.
40+
///
41+
/// Example usage:
42+
/// ```dart
43+
/// ErrorText.localizeError = (BuildContext context, FirebaseAuthException e) {
44+
/// return switch (e.code) {
45+
/// 'user-not-found' => 'Please create an account first.',
46+
/// 'credential-already-in-use' => 'This email is already in use.',
47+
/// _ => 'Oh no! Something went wrong.'
48+
/// }
49+
/// }
50+
static String Function(
51+
BuildContext context,
52+
FirebaseAuthException exception,
53+
)? localizeError;
54+
55+
/// A way to customize the widget that is used across the library to show
56+
/// error hints. By default a localized text is used with a color set to
57+
/// [ColorScheme.error] under [MaterialApp] and
58+
/// [CupertinoColors.destructiveRed] under [CupertinoApp].
59+
static Widget Function(BuildContext context, String message)? builder;
60+
3961
/// An exception that contains error details.
4062
/// Often this is a [FirebaseAuthException].
4163
final Exception exception;
@@ -69,12 +91,16 @@ class ErrorText extends StatelessWidget {
6991
}
7092

7193
if (exception is FirebaseAuthException) {
72-
final e = exception as FirebaseAuthException;
73-
final code = e.code;
74-
final newText = localizedErrorText(code, l) ?? e.message;
94+
if (localizeError != null) {
95+
text = localizeError!(context, exception as FirebaseAuthException);
96+
} else {
97+
final e = exception as FirebaseAuthException;
98+
final code = e.code;
99+
final newText = localizedErrorText(code, l) ?? e.message;
75100

76-
if (newText != null) {
77-
text = newText;
101+
if (newText != null) {
102+
text = newText;
103+
}
78104
}
79105
}
80106

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2023, the Chromium project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:firebase_auth/firebase_auth.dart';
6+
import 'package:firebase_ui_auth/firebase_ui_auth.dart';
7+
import 'package:firebase_ui_localizations/firebase_ui_localizations.dart';
8+
import 'package:flutter/material.dart';
9+
import 'package:flutter_test/flutter_test.dart';
10+
11+
void main() {
12+
final exception = FirebaseAuthException(
13+
code: 'invalid-email',
14+
message: 'The email address is badly formatted.',
15+
);
16+
17+
group('$ErrorText', () {
18+
tearDown(() {
19+
ErrorText.localizeError = null;
20+
});
21+
22+
testWidgets('uses localizations', (tester) async {
23+
await tester.pumpWidget(
24+
MaterialApp(
25+
home: ErrorText(exception: exception),
26+
localizationsDelegates: [
27+
FirebaseUILocalizations.delegate,
28+
],
29+
),
30+
);
31+
expect(
32+
find.text('The email address is badly formatted.'),
33+
findsOneWidget,
34+
);
35+
});
36+
37+
testWidgets('allows to override error text', (tester) async {
38+
String localizeError(
39+
BuildContext context,
40+
FirebaseAuthException exception,
41+
) {
42+
expect(exception.code, 'invalid-email');
43+
return 'Custom error text';
44+
}
45+
46+
ErrorText.localizeError = localizeError;
47+
48+
await tester.pumpWidget(
49+
MaterialApp(
50+
home: ErrorText(exception: exception),
51+
localizationsDelegates: [
52+
FirebaseUILocalizations.delegate,
53+
],
54+
),
55+
);
56+
57+
expect(
58+
find.text('Custom error text'),
59+
findsOneWidget,
60+
);
61+
});
62+
});
63+
}

0 commit comments

Comments
 (0)