Skip to content

Commit

Permalink
feat(ui_auth): add a way to customize ErrorText message (#119)
Browse files Browse the repository at this point in the history
* feat(ui_auth): add a way to customize ErrorText

* add tests

* add license header
  • Loading branch information
lesnitsky authored Oct 3, 2023
1 parent 8757037 commit a36884d
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 5 deletions.
36 changes: 31 additions & 5 deletions packages/firebase_ui_auth/lib/src/widgets/error_text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,28 @@ String? localizedErrorText(
/// A widget which displays error text for a given Firebase error code.
/// {@endtemplate}
class ErrorText extends StatelessWidget {
/// A way to customize localized error messages.
///
/// Example usage:
/// ```dart
/// ErrorText.localizeError = (BuildContext context, FirebaseAuthException e) {
/// return switch (e.code) {
/// 'user-not-found' => 'Please create an account first.',
/// 'credential-already-in-use' => 'This email is already in use.',
/// _ => 'Oh no! Something went wrong.'
/// }
/// }
static String Function(
BuildContext context,
FirebaseAuthException exception,
)? localizeError;
/// A way to customize the widget that is used across the library to show
/// error hints. By default a localized text is used with a color set to
/// [ColorScheme.error] under [MaterialApp] and
/// [CupertinoColors.destructiveRed] under [CupertinoApp].
static Widget Function(BuildContext context, String message)? builder;
/// An exception that contains error details.
/// Often this is a [FirebaseAuthException].
final Exception exception;
Expand Down Expand Up @@ -69,12 +91,16 @@ class ErrorText extends StatelessWidget {
}
if (exception is FirebaseAuthException) {
final e = exception as FirebaseAuthException;
final code = e.code;
final newText = localizedErrorText(code, l) ?? e.message;
if (localizeError != null) {
text = localizeError!(context, exception as FirebaseAuthException);
} else {
final e = exception as FirebaseAuthException;
final code = e.code;
final newText = localizedErrorText(code, l) ?? e.message;
if (newText != null) {
text = newText;
if (newText != null) {
text = newText;
}
}
}
Expand Down
63 changes: 63 additions & 0 deletions packages/firebase_ui_auth/test/widgets/error_text_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2023, the Chromium project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_ui_auth/firebase_ui_auth.dart';
import 'package:firebase_ui_localizations/firebase_ui_localizations.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
final exception = FirebaseAuthException(
code: 'invalid-email',
message: 'The email address is badly formatted.',
);

group('$ErrorText', () {
tearDown(() {
ErrorText.localizeError = null;
});

testWidgets('uses localizations', (tester) async {
await tester.pumpWidget(
MaterialApp(
home: ErrorText(exception: exception),
localizationsDelegates: [
FirebaseUILocalizations.delegate,
],
),
);
expect(
find.text('The email address is badly formatted.'),
findsOneWidget,
);
});

testWidgets('allows to override error text', (tester) async {
String localizeError(
BuildContext context,
FirebaseAuthException exception,
) {
expect(exception.code, 'invalid-email');
return 'Custom error text';
}

ErrorText.localizeError = localizeError;

await tester.pumpWidget(
MaterialApp(
home: ErrorText(exception: exception),
localizationsDelegates: [
FirebaseUILocalizations.delegate,
],
),
);

expect(
find.text('Custom error text'),
findsOneWidget,
);
});
});
}

0 comments on commit a36884d

Please sign in to comment.