From 04800868110c38fb5ccb3f9cfd6441cf132b863c Mon Sep 17 00:00:00 2001 From: Andrei Lesnitsky Date: Thu, 2 Nov 2023 14:51:26 +0100 Subject: [PATCH] feat(ui_auth): add a way to customize PlatformException text --- .../lib/src/widgets/error_text.dart | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) 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 d8059d74..1ddbe797 100644 --- a/packages/firebase_ui_auth/lib/src/widgets/error_text.dart +++ b/packages/firebase_ui_auth/lib/src/widgets/error_text.dart @@ -6,6 +6,7 @@ import 'package:firebase_auth/firebase_auth.dart' show FirebaseAuthException; import 'package:firebase_ui_localizations/firebase_ui_localizations.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; +import 'package:flutter/services.dart' show PlatformException; import '../flows/phone_auth_flow.dart'; @@ -38,7 +39,7 @@ 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. + /// A way to customize localized error messages for [FirebaseAuthException]. /// /// Example usage: /// ```dart @@ -54,6 +55,19 @@ class ErrorText extends StatelessWidget { FirebaseAuthException exception, )? localizeError; + /// A way to customize error message for [PlatformException] + /// + /// Example usage: + /// ```dart + /// ErrorText.localizePlatformError = (BuildContext context, PlatformException e) { + /// if (e.code == "network_error") return "Please check your internet connection."; + /// return "Oh no! Something went wrong."; + /// } + static String Function( + BuildContext context, + PlatformException exception, + )? localizePlatformError; + /// 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 @@ -106,6 +120,10 @@ class ErrorText extends StatelessWidget { } } + if (exception is PlatformException && localizePlatformError != null) { + text = localizePlatformError!(context, exception as PlatformException); + } + return Text( text, textAlign: textAlign,