Skip to content

Commit

Permalink
fix(ui_auth): fix material 3 button styling (#10253)
Browse files Browse the repository at this point in the history
  • Loading branch information
lesnitsky authored Jan 12, 2023
1 parent f3a4d3a commit a541e9a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
8 changes: 7 additions & 1 deletion packages/firebase_ui_auth/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:firebase_ui_oauth_apple/firebase_ui_oauth_apple.dart';
import 'package:firebase_ui_oauth_facebook/firebase_ui_oauth_facebook.dart';
import 'package:firebase_ui_oauth_google/firebase_ui_oauth_google.dart';
import 'package:firebase_ui_oauth_twitter/firebase_ui_oauth_twitter.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

Expand Down Expand Up @@ -107,6 +108,7 @@ class FirebaseAuthUIExample extends StatelessWidget {
theme: ThemeData(
brightness: Brightness.light,
visualDensity: VisualDensity.standard,
useMaterial3: true,
inputDecorationTheme: const InputDecorationTheme(
border: OutlineInputBorder(),
),
Expand Down Expand Up @@ -260,6 +262,8 @@ class FirebaseAuthUIExample extends StatelessWidget {
);
},
'/profile': (context) {
final platform = Theme.of(context).platform;

return ProfileScreen(
actions: [
SignedOutAction((context) {
Expand All @@ -268,7 +272,9 @@ class FirebaseAuthUIExample extends StatelessWidget {
mfaAction,
],
actionCodeSettings: actionCodeSettings,
showMFATile: true,
showMFATile: kIsWeb ||
platform == TargetPlatform.iOS ||
platform == TargetPlatform.android,
);
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,15 @@ class _DeleteAccountButtonState extends State<DeleteAccountButton> {
final l = FirebaseUILocalizations.labelsOf(context);
bool isCupertino = CupertinoUserInterfaceLevel.maybeOf(context) != null;

final themeData = Theme.of(context);
final colorScheme = themeData.colorScheme;

return LoadingButton(
isLoading: _isLoading,
color: isCupertino ? CupertinoColors.destructiveRed : Colors.red,
color: isCupertino ? CupertinoColors.destructiveRed : colorScheme.error,
icon: isCupertino ? CupertinoIcons.delete : Icons.delete,
label: l.deleteAccount,
labelColor: colorScheme.onError,
onTap: _deleteAccount,
variant: widget.variant,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,17 @@ class _LoadingButtonContent extends StatelessWidget {
@override
Widget build(BuildContext context) {
bool isCupertino = CupertinoUserInterfaceLevel.maybeOf(context) != null;
Widget child;

Widget child = Text(label);
if (color != null) {
final theme = Theme.of(context).textTheme.button;
child = Text(
label,
style: theme?.copyWith(color: color),
);
} else {
child = Text(label);
}

if (isLoading) {
child = LoadingIndicator(
Expand All @@ -42,6 +51,7 @@ class LoadingButton extends StatelessWidget {
final String label;
final IconData? icon;
final Color? color;
final Color? labelColor;
final VoidCallback onTap;
final ButtonVariant? variant;

Expand All @@ -52,22 +62,31 @@ class LoadingButton extends StatelessWidget {
this.isLoading = false,
this.icon,
this.color,
this.labelColor,
this.variant = ButtonVariant.outlined,
}) : super(key: key);

@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final isMaterial3 = theme.useMaterial3;

final resolvedColor = variant == ButtonVariant.filled && !isMaterial3
? theme.colorScheme.onPrimary
: null;

final contentColor = labelColor ?? resolvedColor;

final content = _LoadingButtonContent(
label: label,
isLoading: isLoading,
color: variant == ButtonVariant.filled
? Theme.of(context).colorScheme.onPrimary
: null,
color: contentColor,
);

return UniversalButton(
color: color,
icon: icon,
contentColor: contentColor,
onPressed: onTap,
variant: variant,
child: content,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class UniversalButton extends PlatformWidget {
final TextDirection? direction;
final ButtonVariant? variant;
final Color? color;
final Color? contentColor;

const UniversalButton({
Key? key,
Expand All @@ -39,6 +40,7 @@ class UniversalButton extends PlatformWidget {
this.direction = TextDirection.ltr,
this.variant,
this.color,
this.contentColor,
}) : assert(text != null || child != null),
super(key: key);

Expand All @@ -56,7 +58,7 @@ class UniversalButton extends PlatformWidget {
children: [
if (icon != null) ...[
if (direction == TextDirection.rtl) const SizedBox(width: 8),
Icon(icon, size: 20),
Icon(icon, size: 20, color: contentColor),
if (direction == TextDirection.ltr) const SizedBox(width: 8),
],
this.child ?? Text(text!),
Expand Down Expand Up @@ -99,6 +101,7 @@ class UniversalButton extends PlatformWidget {
if (variant == ButtonVariant.text) {
foregroundColor = MaterialStateColor.resolveWith((_) => color!);
} else {
foregroundColor = MaterialStateColor.resolveWith((_) => contentColor!);
backgroundColor = MaterialStateColor.resolveWith((_) => color!);
}

Expand All @@ -115,22 +118,22 @@ class UniversalButton extends PlatformWidget {
switch (_variant) {
case ButtonVariant.text:
return TextButton.icon(
icon: Icon(icon),
icon: Icon(icon, color: contentColor),
onPressed: onPressed,
label: child,
style: style,
);
case ButtonVariant.filled:
return ElevatedButton.icon(
onPressed: onPressed,
icon: Icon(icon),
icon: Icon(icon, color: contentColor),
label: child,
style: style,
);
case ButtonVariant.outlined:
return OutlinedButton.icon(
onPressed: onPressed,
icon: Icon(icon),
icon: Icon(icon, color: contentColor),
label: child,
style: style,
);
Expand Down

0 comments on commit a541e9a

Please sign in to comment.