diff --git a/packages/firebase_ui_auth/lib/src/widgets/error_text.dart b/packages/firebase_ui_auth/lib/src/widgets/error_text.dart index ee1d7917..a8c87751 100644 --- a/packages/firebase_ui_auth/lib/src/widgets/error_text.dart +++ b/packages/firebase_ui_auth/lib/src/widgets/error_text.dart @@ -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; @@ -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; + } } } diff --git a/packages/firebase_ui_auth/test/widgets/error_text_test.dart b/packages/firebase_ui_auth/test/widgets/error_text_test.dart new file mode 100644 index 00000000..04cc5152 --- /dev/null +++ b/packages/firebase_ui_auth/test/widgets/error_text_test.dart @@ -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, + ); + }); + }); +}