Skip to content

feat(ui_auth): add a way to customize ErrorText message #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -37,6 +37,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 @@ -70,12 +92,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,
);
});
});
}