diff --git a/packages/firebase_ui_oauth/lib/src/oauth_provider_button_base.dart b/packages/firebase_ui_oauth/lib/src/oauth_provider_button_base.dart index d6fa7460..cc3d1958 100644 --- a/packages/firebase_ui_oauth/lib/src/oauth_provider_button_base.dart +++ b/packages/firebase_ui_oauth/lib/src/oauth_provider_button_base.dart @@ -5,7 +5,6 @@ import 'package:firebase_auth/firebase_auth.dart' as fba; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; -import 'package:flutter_svg/flutter_svg.dart'; import 'package:firebase_ui_auth/firebase_ui_auth.dart'; import 'package:firebase_ui_oauth/firebase_ui_oauth.dart'; @@ -361,7 +360,7 @@ class _ButtonContent extends StatelessWidget { final double height; final String iconSrc; final double iconPadding; - final String assetsPackage; + final String? assetsPackage; final String label; final bool isLoading; final Color textColor; @@ -392,7 +391,12 @@ class _ButtonContent extends StatelessWidget { Widget build(BuildContext context) { final icon = Padding( padding: EdgeInsets.all(iconPadding), - child: SvgPicture.string(iconSrc, width: height, height: height), + child: OAuthProviderIcon( + src: iconSrc, + package: assetsPackage, + width: height, + height: height, + ), ); if (label.isEmpty) { diff --git a/packages/firebase_ui_oauth/lib/src/oauth_provider_button_style.dart b/packages/firebase_ui_oauth/lib/src/oauth_provider_button_style.dart index 5ad02f16..82eb47b5 100644 --- a/packages/firebase_ui_oauth/lib/src/oauth_provider_button_style.dart +++ b/packages/firebase_ui_oauth/lib/src/oauth_provider_button_style.dart @@ -4,19 +4,42 @@ import 'package:flutter/material.dart'; import 'package:firebase_ui_shared/firebase_ui_shared.dart'; -import 'package:flutter_svg/svg.dart'; +import 'package:vector_graphics/vector_graphics.dart'; /// {@template ui.oauth.themed_oauth_provider_button_style} /// An object that is being used to resolve a style of the button. /// {@endtemplate} abstract class ThemedOAuthProviderButtonStyle { + /// Asset paths (relative to [assetsPackage]) of the light and dark icons. + /// + /// Icons must be compiled into the `vector_graphics` binary format. Declare + /// the SVG assets with the `vector_graphics_compiler` asset transformer: + /// + /// ```yaml + /// flutter: + /// assets: + /// - path: assets/icon.svg + /// transformers: + /// - package: vector_graphics_compiler + /// ``` + /// + /// When the assets are declared by a package (see [assetsPackage]), that + /// package must list `vector_graphics_compiler` under `dependencies` (not + /// `dev_dependencies`) so that the transformer can be resolved when + /// building the consuming app. When the assets are declared by the app + /// itself, a `dev_dependencies` entry in the app is enough. ThemedIconSrc get iconSrc; ThemedColor get backgroundColor; ThemedColor get color; ThemedColor get iconBackgroundColor => backgroundColor; ThemedColor get borderColor => backgroundColor; double get iconPadding => 0; - String get assetsPackage; + + /// The name of the package that contains the [iconSrc] assets. + /// + /// Return `null` when the assets are bundled with the app itself, for + /// example for a custom provider style defined in the app. + String? get assetsPackage; /// A custom label string. /// @@ -27,9 +50,9 @@ abstract class ThemedOAuthProviderButtonStyle { /// Profile Screen. ThemedValue get iconWidget => ThemedValue( // Display the light icon on a dark background. - SvgPicture.string(iconSrc.light), + OAuthProviderIcon(src: iconSrc.light, package: assetsPackage), // Display the dark icon on a light background. - SvgPicture.string(iconSrc.dark), + OAuthProviderIcon(src: iconSrc.dark, package: assetsPackage), ); /// {@macro ui.oauth.themed_oauth_provider_button_style} @@ -54,7 +77,7 @@ class OAuthProviderButtonStyle { final Color backgroundColor; final Color color; final Color borderColor; - final String assetsPackage; + final String? assetsPackage; final Color iconBackgroundColor; OAuthProviderButtonStyle({ @@ -67,3 +90,36 @@ class OAuthProviderButtonStyle { required this.iconBackgroundColor, }); } + +/// {@template ui.oauth.oauth_provider_icon} +/// Renders an SVG icon asset compiled by the `vector_graphics_compiler` +/// asset transformer. +/// {@endtemplate} +class OAuthProviderIcon extends StatelessWidget { + /// The asset path of the precompiled icon. + final String src; + + /// The package that contains the [src] asset. + final String? package; + + final double? width; + final double? height; + + /// {@macro ui.oauth.oauth_provider_icon} + const OAuthProviderIcon({ + super.key, + required this.src, + this.package, + this.width, + this.height, + }); + + @override + Widget build(BuildContext context) { + return VectorGraphic( + loader: AssetBytesLoader(src, packageName: package), + width: width, + height: height, + ); + } +} diff --git a/packages/firebase_ui_oauth/pubspec.yaml b/packages/firebase_ui_oauth/pubspec.yaml index cccd0c93..b3af3b9d 100644 --- a/packages/firebase_ui_oauth/pubspec.yaml +++ b/packages/firebase_ui_oauth/pubspec.yaml @@ -13,9 +13,9 @@ dependencies: firebase_auth: ^7.0.0 firebase_ui_auth: ^4.0.0 firebase_ui_shared: ^1.5.0 - flutter_svg: ^2.0.9 flutter_web_auth_2: ^5.1.0 http: ^1.6.0 + vector_graphics: ^1.1.19 flutter: sdk: flutter @@ -23,6 +23,7 @@ dev_dependencies: flutter_test: sdk: flutter flutter_lints: ^6.0.0 + vector_graphics_compiler: ^1.1.19 false_secrets: - "/example/**/google-services.json" diff --git a/packages/firebase_ui_oauth/test/flutterfire_ui_oauth_test.dart b/packages/firebase_ui_oauth/test/flutterfire_ui_oauth_test.dart index 55ec8d78..dc4838db 100644 --- a/packages/firebase_ui_oauth/test/flutterfire_ui_oauth_test.dart +++ b/packages/firebase_ui_oauth/test/flutterfire_ui_oauth_test.dart @@ -6,14 +6,22 @@ import 'package:firebase_auth/firebase_auth.dart' as fba; import 'package:firebase_ui_auth/firebase_ui_auth.dart'; import 'package:firebase_ui_oauth/firebase_ui_oauth.dart'; import 'package:flutter/material.dart'; -import 'package:flutter_svg/flutter_svg.dart'; +import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'package:vector_graphics/vector_graphics.dart'; +import 'package:vector_graphics_compiler/vector_graphics_compiler.dart' + show encodeSvg; class FakeAssetBundle extends Fake implements AssetBundle { final String svgStr = ''''''; + final List loadedKeys = []; @override - Future loadString(String key, {bool cache = true}) async => svgStr; + Future load(String key) async { + loadedKeys.add(key); + final bytes = encodeSvg(xml: svgStr, debugName: key); + return bytes.buffer.asByteData(); + } } // Duplicated from packages/firebase_ui_oauth_google/lib/src/theme.dart @@ -26,24 +34,9 @@ const _backgroundColor = ThemedColor(_googleBlue, _googleWhite); const _color = ThemedColor(_googleWhite, _googleDark); const _iconBackgroundColor = ThemedColor(_googleWhite, _googleWhite); -const _iconSvg = ''' - - - - - - - - - - - - - - -'''; - -const _iconSrc = ThemedIconSrc(_iconSvg, _iconSvg); +const _iconAsset = 'assets/icons/google.svg'; + +const _iconSrc = ThemedIconSrc(_iconAsset, _iconAsset); class GoogleProviderButtonStyle extends ThemedOAuthProviderButtonStyle { const GoogleProviderButtonStyle(); @@ -67,6 +60,24 @@ class GoogleProviderButtonStyle extends ThemedOAuthProviderButtonStyle { String get assetsPackage => 'firebase_ui_oauth_google'; } +/// A custom provider style whose icon is bundled with the app itself. +class AppBundledButtonStyle extends ThemedOAuthProviderButtonStyle { + const AppBundledButtonStyle(); + + @override + ThemedColor get backgroundColor => _backgroundColor; + + @override + ThemedColor get color => _color; + + @override + ThemedIconSrc get iconSrc => + const ThemedIconSrc('assets/app_icon_dark.svg', 'assets/app_icon.svg'); + + @override + String? get assetsPackage => null; +} + class FakeOAuthProvider extends OAuthProvider { @override ProviderArgs get desktopSignInArgs => throw UnimplementedError(); @@ -193,7 +204,7 @@ void main() { await tester.pumpWidget(renderMaterialButton()); final iconFinder = find.byWidgetPredicate( - (widget) => widget is SvgPicture, + (widget) => widget is VectorGraphic, ); expect(iconFinder, findsOneWidget); @@ -225,4 +236,33 @@ void main() { expect(find.byType(LayoutFlowAwarePadding), findsOneWidget); }); }); + + group('ThemedOAuthProviderButtonStyle.iconWidget', () { + Future> pumpIcon(WidgetTester tester, Widget icon) async { + final bundle = FakeAssetBundle(); + await tester.pumpWidget(DefaultAssetBundle(bundle: bundle, child: icon)); + return bundle.loadedKeys; + } + + testWidgets('loads icons from assetsPackage', (tester) async { + final keys = await pumpIcon( + tester, + style.iconWidget.getValue(Brightness.light), + ); + + expect(keys, ['packages/firebase_ui_oauth_google/$_iconAsset']); + }); + + testWidgets('loads icons from the app bundle when assetsPackage is null', ( + tester, + ) async { + const appStyle = AppBundledButtonStyle(); + final keys = await pumpIcon( + tester, + appStyle.iconWidget.getValue(Brightness.light), + ); + + expect(keys, ['assets/app_icon_dark.svg']); + }); + }); } diff --git a/packages/firebase_ui_oauth_apple/assets/icons/apple_dark.svg b/packages/firebase_ui_oauth_apple/assets/icons/apple_dark.svg new file mode 100644 index 00000000..b563dada --- /dev/null +++ b/packages/firebase_ui_oauth_apple/assets/icons/apple_dark.svg @@ -0,0 +1,7 @@ + + + + + + + diff --git a/packages/firebase_ui_oauth_apple/assets/icons/apple_light.svg b/packages/firebase_ui_oauth_apple/assets/icons/apple_light.svg new file mode 100644 index 00000000..051f05ce --- /dev/null +++ b/packages/firebase_ui_oauth_apple/assets/icons/apple_light.svg @@ -0,0 +1,7 @@ + + + + + + + diff --git a/packages/firebase_ui_oauth_apple/lib/src/theme.dart b/packages/firebase_ui_oauth_apple/lib/src/theme.dart index 3fcd9f2f..55219478 100644 --- a/packages/firebase_ui_oauth_apple/lib/src/theme.dart +++ b/packages/firebase_ui_oauth_apple/lib/src/theme.dart @@ -11,29 +11,10 @@ const _appleWhite = Color(0xffffffff); const _backgroundColor = ThemedColor(_appleWhite, _appleBlack); const _color = ThemedColor(_appleBlack, _appleWhite); -const _iconSvgLight = ''' - - - - - - - - -'''; - -const _iconSvgDark = ''' - - - - - - - - -'''; - -const _iconSrc = ThemedIconSrc(_iconSvgLight, _iconSvgDark); +const _iconSrc = ThemedIconSrc( + 'assets/icons/apple_light.svg', + 'assets/icons/apple_dark.svg', +); class AppleProviderButtonStyle extends ThemedOAuthProviderButtonStyle { const AppleProviderButtonStyle(); diff --git a/packages/firebase_ui_oauth_apple/pubspec.yaml b/packages/firebase_ui_oauth_apple/pubspec.yaml index f69ecf1d..96dc2fd3 100644 --- a/packages/firebase_ui_oauth_apple/pubspec.yaml +++ b/packages/firebase_ui_oauth_apple/pubspec.yaml @@ -11,6 +11,7 @@ environment: dependencies: firebase_auth: ^7.0.0 firebase_ui_oauth: ^3.0.0 + vector_graphics_compiler: ^1.1.19 flutter: sdk: flutter @@ -29,34 +30,12 @@ false_secrets: # The following section is specific to Flutter packages. flutter: - - # To add assets to your package, add an assets section, like this: - # assets: - # - images/a_dot_burr.jpeg - # - images/a_dot_ham.jpeg - # - # For details regarding assets in packages, see - # https://flutter.dev/assets-and-images/#from-packages - # - # An image asset can refer to one or more resolution-specific "variants", see - # https://flutter.dev/assets-and-images/#resolution-aware - - # To add custom fonts to your package, add a fonts section here, - # in this "flutter" section. Each entry in this list should have a - # "family" key with the font family name, and a "fonts" key with a - # list giving the asset and other descriptors for the font. For - # example: - # fonts: - # - family: Schyler - # fonts: - # - asset: fonts/Schyler-Regular.ttf - # - asset: fonts/Schyler-Italic.ttf - # style: italic - # - family: Trajan Pro - # fonts: - # - asset: fonts/TrajanPro.ttf - # - asset: fonts/TrajanPro_Bold.ttf - # weight: 700 - # - # For details regarding fonts in packages, see - # https://flutter.dev/custom-fonts/#from-packages + # SVG icons are compiled to the vector_graphics binary format at build + # time, so the output always matches the app's resolved codec version. + assets: + - path: assets/icons/apple_light.svg + transformers: + - package: vector_graphics_compiler + - path: assets/icons/apple_dark.svg + transformers: + - package: vector_graphics_compiler diff --git a/packages/firebase_ui_oauth_facebook/assets/icons/facebook.svg b/packages/firebase_ui_oauth_facebook/assets/icons/facebook.svg new file mode 100644 index 00000000..2faee6f3 --- /dev/null +++ b/packages/firebase_ui_oauth_facebook/assets/icons/facebook.svg @@ -0,0 +1,7 @@ + + + f_logo_RGB-White_1024 + + + + diff --git a/packages/firebase_ui_oauth_facebook/lib/src/theme.dart b/packages/firebase_ui_oauth_facebook/lib/src/theme.dart index a7106c63..4381a82d 100644 --- a/packages/firebase_ui_oauth_facebook/lib/src/theme.dart +++ b/packages/firebase_ui_oauth_facebook/lib/src/theme.dart @@ -11,16 +11,8 @@ const _facebookWhite = Color(0xffffffff); const _backgroundColor = ThemedColor(_facebookBlue, _facebookBlue); const _color = ThemedColor(_facebookWhite, _facebookWhite); -const _iconSvg = ''' - - f_logo_RGB-White_1024 - - - - -'''; - -const _iconSrc = ThemedIconSrc(_iconSvg, _iconSvg); +const _iconAsset = 'assets/icons/facebook.svg'; +const _iconSrc = ThemedIconSrc(_iconAsset, _iconAsset); class FacebookProviderButtonStyle extends ThemedOAuthProviderButtonStyle { const FacebookProviderButtonStyle(); diff --git a/packages/firebase_ui_oauth_facebook/pubspec.yaml b/packages/firebase_ui_oauth_facebook/pubspec.yaml index 04f8dc87..6daa870d 100644 --- a/packages/firebase_ui_oauth_facebook/pubspec.yaml +++ b/packages/firebase_ui_oauth_facebook/pubspec.yaml @@ -11,6 +11,7 @@ environment: dependencies: firebase_auth: ^7.0.0 firebase_ui_oauth: ^3.0.0 + vector_graphics_compiler: ^1.1.19 flutter: sdk: flutter flutter_facebook_auth: ^7.1.2 @@ -32,34 +33,9 @@ false_secrets: # The following section is specific to Flutter packages. flutter: - - # To add assets to your package, add an assets section, like this: - # assets: - # - images/a_dot_burr.jpeg - # - images/a_dot_ham.jpeg - # - # For details regarding assets in packages, see - # https://flutter.dev/assets-and-images/#from-packages - # - # An image asset can refer to one or more resolution-specific "variants", see - # https://flutter.dev/assets-and-images/#resolution-aware - - # To add custom fonts to your package, add a fonts section here, - # in this "flutter" section. Each entry in this list should have a - # "family" key with the font family name, and a "fonts" key with a - # list giving the asset and other descriptors for the font. For - # example: - # fonts: - # - family: Schyler - # fonts: - # - asset: fonts/Schyler-Regular.ttf - # - asset: fonts/Schyler-Italic.ttf - # style: italic - # - family: Trajan Pro - # fonts: - # - asset: fonts/TrajanPro.ttf - # - asset: fonts/TrajanPro_Bold.ttf - # weight: 700 - # - # For details regarding fonts in packages, see - # https://flutter.dev/custom-fonts/#from-packages + # SVG icons are compiled to the vector_graphics binary format at build + # time, so the output always matches the app's resolved codec version. + assets: + - path: assets/icons/facebook.svg + transformers: + - package: vector_graphics_compiler diff --git a/packages/firebase_ui_oauth_google/assets/google_icon.svg b/packages/firebase_ui_oauth_google/assets/icons/google.svg similarity index 99% rename from packages/firebase_ui_oauth_google/assets/google_icon.svg rename to packages/firebase_ui_oauth_google/assets/icons/google.svg index 50d9b896..1033f528 100644 --- a/packages/firebase_ui_oauth_google/assets/google_icon.svg +++ b/packages/firebase_ui_oauth_google/assets/icons/google.svg @@ -1,4 +1,3 @@ - diff --git a/packages/firebase_ui_oauth_google/lib/src/theme.dart b/packages/firebase_ui_oauth_google/lib/src/theme.dart index 70be751f..31e7ffe5 100644 --- a/packages/firebase_ui_oauth_google/lib/src/theme.dart +++ b/packages/firebase_ui_oauth_google/lib/src/theme.dart @@ -13,24 +13,8 @@ const _backgroundColor = ThemedColor(_googleBlue, _googleWhite); const _color = ThemedColor(_googleWhite, _googleDark); const _iconBackgroundColor = ThemedColor(_googleWhite, _googleWhite); -const _iconSvg = ''' - - - - - - - - - - - - - - -'''; - -const _iconSrc = ThemedIconSrc(_iconSvg, _iconSvg); +const _iconAsset = 'assets/icons/google.svg'; +const _iconSrc = ThemedIconSrc(_iconAsset, _iconAsset); class GoogleProviderButtonStyle extends ThemedOAuthProviderButtonStyle { const GoogleProviderButtonStyle(); diff --git a/packages/firebase_ui_oauth_google/pubspec.yaml b/packages/firebase_ui_oauth_google/pubspec.yaml index 015a2bba..f52b1ece 100644 --- a/packages/firebase_ui_oauth_google/pubspec.yaml +++ b/packages/firebase_ui_oauth_google/pubspec.yaml @@ -11,6 +11,7 @@ environment: dependencies: firebase_auth: ^7.0.0 firebase_ui_oauth: ^3.0.0 + vector_graphics_compiler: ^1.1.19 flutter: sdk: flutter google_sign_in: ^7.1.0 @@ -26,3 +27,9 @@ false_secrets: - "/example/**/GoogleService-Info.plist" flutter: + # SVG icons are compiled to the vector_graphics binary format at build + # time, so the output always matches the app's resolved codec version. + assets: + - path: assets/icons/google.svg + transformers: + - package: vector_graphics_compiler diff --git a/packages/firebase_ui_oauth_twitter/assets/icons/twitter.svg b/packages/firebase_ui_oauth_twitter/assets/icons/twitter.svg new file mode 100644 index 00000000..72d0f680 --- /dev/null +++ b/packages/firebase_ui_oauth_twitter/assets/icons/twitter.svg @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/packages/firebase_ui_oauth_twitter/lib/src/theme.dart b/packages/firebase_ui_oauth_twitter/lib/src/theme.dart index 08c42879..cb686148 100644 --- a/packages/firebase_ui_oauth_twitter/lib/src/theme.dart +++ b/packages/firebase_ui_oauth_twitter/lib/src/theme.dart @@ -11,17 +11,8 @@ const _twitterWhite = Color(0xffffffff); const _backgroundColor = ThemedColor(_twitterBlue, _twitterBlue); const _color = ThemedColor(_twitterWhite, _twitterWhite); -const _iconSvg = ''' - - - - - - - -'''; - -const _iconSrc = ThemedIconSrc(_iconSvg, _iconSvg); +const _iconAsset = 'assets/icons/twitter.svg'; +const _iconSrc = ThemedIconSrc(_iconAsset, _iconAsset); class TwitterProviderButtonStyle extends ThemedOAuthProviderButtonStyle { const TwitterProviderButtonStyle(); diff --git a/packages/firebase_ui_oauth_twitter/pubspec.yaml b/packages/firebase_ui_oauth_twitter/pubspec.yaml index d4d93ce7..bf9dca07 100644 --- a/packages/firebase_ui_oauth_twitter/pubspec.yaml +++ b/packages/firebase_ui_oauth_twitter/pubspec.yaml @@ -13,6 +13,7 @@ dependencies: sdk: flutter firebase_auth: ^7.0.0 firebase_ui_oauth: ^3.0.0 + vector_graphics_compiler: ^1.1.19 dev_dependencies: flutter_test: @@ -29,34 +30,9 @@ false_secrets: # The following section is specific to Flutter packages. flutter: - - # To add assets to your package, add an assets section, like this: - # assets: - # - images/a_dot_burr.jpeg - # - images/a_dot_ham.jpeg - # - # For details regarding assets in packages, see - # https://flutter.dev/assets-and-images/#from-packages - # - # An image asset can refer to one or more resolution-specific "variants", see - # https://flutter.dev/assets-and-images/#resolution-aware - - # To add custom fonts to your package, add a fonts section here, - # in this "flutter" section. Each entry in this list should have a - # "family" key with the font family name, and a "fonts" key with a - # list giving the asset and other descriptors for the font. For - # example: - # fonts: - # - family: Schyler - # fonts: - # - asset: fonts/Schyler-Regular.ttf - # - asset: fonts/Schyler-Italic.ttf - # style: italic - # - family: Trajan Pro - # fonts: - # - asset: fonts/TrajanPro.ttf - # - asset: fonts/TrajanPro_Bold.ttf - # weight: 700 - # - # For details regarding fonts in packages, see - # https://flutter.dev/custom-fonts/#from-packages + # SVG icons are compiled to the vector_graphics binary format at build + # time, so the output always matches the app's resolved codec version. + assets: + - path: assets/icons/twitter.svg + transformers: + - package: vector_graphics_compiler