From cc98d3f3ea0539c1eeb8c013dc425d2443283ed7 Mon Sep 17 00:00:00 2001 From: Jonas Wanke Date: Sat, 30 Dec 2023 11:03:24 +0100 Subject: [PATCH] Honor themeData.useMaterialTheme with different defaults --- lib/src/app_bar/app_bar.dart | 12 +++++-- lib/src/app_bar/state.dart | 66 +++++++++++++++++++++++++++++------- 2 files changed, 63 insertions(+), 15 deletions(-) diff --git a/lib/src/app_bar/app_bar.dart b/lib/src/app_bar/app_bar.dart index c5e0d7e..0a347a2 100644 --- a/lib/src/app_bar/app_bar.dart +++ b/lib/src/app_bar/app_bar.dart @@ -22,6 +22,7 @@ class MorphingAppBar extends StatelessWidget implements PreferredSizeWidget { this.bottom, this.elevation, this.shadowColor, + this.surfaceTintColor, this.shape, this.backgroundColor, this.foregroundColor, @@ -71,6 +72,9 @@ class MorphingAppBar extends StatelessWidget implements PreferredSizeWidget { /// See [AppBar.shadowColor] final Color? shadowColor; + /// See [AppBar.surfaceTintColor] + final Color? surfaceTintColor; + /// See [AppBar.shape] final ShapeBorder? shape; @@ -128,9 +132,11 @@ class MorphingAppBar extends StatelessWidget implements PreferredSizeWidget { final actualBackgroundColor = backgroundColor ?? theme.appBarTheme.backgroundColor ?? - (theme.colorScheme.brightness == Brightness.dark + (theme.useMaterial3 ? theme.colorScheme.surface - : theme.colorScheme.primary); + : theme.colorScheme.brightness == Brightness.dark + ? theme.colorScheme.surface + : theme.colorScheme.primary); final actualSystemOverlayStyle = systemOverlayStyle ?? context.theme.appBarTheme.systemOverlayStyle ?? actualBackgroundColor.contrastSystemUiOverlayStyle; @@ -148,6 +154,7 @@ class MorphingAppBar extends StatelessWidget implements PreferredSizeWidget { bottom: bottom, elevation: elevation, shadowColor: shadowColor, + surfaceTintColor: surfaceTintColor, shape: shape, backgroundColor: actualBackgroundColor, foregroundColor: foregroundColor, @@ -224,6 +231,7 @@ class _AnimatedAppBar extends AnimatedWidget { bottom: AnimatedBottom(state), elevation: state.elevation, shadowColor: state.shadowColor, + surfaceTintColor: state.surfaceTintColor, shape: state.shape, backgroundColor: state.backgroundColor, foregroundColor: state.foregroundColor, diff --git a/lib/src/app_bar/state.dart b/lib/src/app_bar/state.dart index cd6083f..34aae93 100644 --- a/lib/src/app_bar/state.dart +++ b/lib/src/app_bar/state.dart @@ -21,6 +21,8 @@ class MorphingState { double get elevation => lerpDouble(parent.elevation, child.elevation, t)!; Color get shadowColor => _lerpColor(parent.shadowColor, child.shadowColor, t); + Color get surfaceTintColor => + _lerpColor(parent.surfaceTintColor, child.surfaceTintColor, t); ShapeBorder? get shape => ShapeBorder.lerp(parent.shape, child.shape, t); Color get backgroundColor => @@ -56,10 +58,15 @@ class MorphingState { SystemUiOverlayStyle get systemOverlayStyle => t < 0.5 ? parent.systemOverlayStyle : child.systemOverlayStyle; - /// Interpolate between colors in Oklab space. - static Color _lerpColor(Color a, Color b, double t) { - return OklabColor.fromColor(a) - .interpolate(OklabColor.fromColor(b), t) + /// Interpolate between colors in Oklab space, where `null` values are treated + /// as transparent. + static Color _lerpColor(Color? a, Color? b, double t) { + if (a == null && b == null) return Colors.transparent; + + final aAsOklab = a == null ? null : OklabColor.fromColor(a); + final bAsOklab = b == null ? null : OklabColor.fromColor(b); + return (aAsOklab ?? bAsOklab!.withAlpha(0)) + .interpolate(bAsOklab ?? aAsOklab!.withAlpha(0), t) .toColor(); } } @@ -77,36 +84,68 @@ class EndState { final Widget? leading; - double get elevation => appBar.elevation ?? appBarTheme.elevation ?? 4; - Color get shadowColor => - appBar.shadowColor ?? appBarTheme.shadowColor ?? Colors.black; + double get elevation => + appBar.elevation ?? appBarTheme.elevation ?? (theme.useMaterial3 ? 0 : 4); + double? get scrolledUnderElevation { + return appBar.elevation ?? + appBarTheme.elevation ?? + (theme.useMaterial3 ? 3 : null); + } + + Color get shadowColor { + return appBar.shadowColor ?? + appBarTheme.shadowColor ?? + (theme.useMaterial3 ? Colors.transparent : Colors.black); + } + + Color? get surfaceTintColor { + return appBar.surfaceTintColor ?? + appBarTheme.surfaceTintColor ?? + (theme.useMaterial3 ? theme.colorScheme.surfaceTint : null); + } + ShapeBorder? get shape => appBar.shape; Color get backgroundColor { return appBar.backgroundColor ?? appBarTheme.backgroundColor ?? - (theme.colorScheme.brightness == Brightness.dark + (theme.useMaterial3 ? theme.colorScheme.surface - : theme.colorScheme.primary); + : theme.colorScheme.brightness == Brightness.dark + ? theme.colorScheme.surface + : theme.colorScheme.primary); } Color get foregroundColor { return appBar.foregroundColor ?? appBarTheme.foregroundColor ?? - (theme.colorScheme.brightness == Brightness.dark + (theme.useMaterial3 ? theme.colorScheme.onSurface - : theme.colorScheme.onPrimary); + : theme.colorScheme.brightness == Brightness.dark + ? theme.colorScheme.onSurface + : theme.colorScheme.onPrimary); } IconThemeData get overallIconTheme { return appBar.iconTheme ?? appBarTheme.iconTheme ?? - theme.iconTheme.copyWith(color: foregroundColor); + (theme.useMaterial3 ? const IconThemeData(size: 24) : theme.iconTheme) + .copyWith(color: foregroundColor); } IconThemeData get actionsIconTheme { return appBar.actionsIconTheme ?? appBarTheme.actionsIconTheme ?? + appBar.iconTheme ?? + appBarTheme.iconTheme ?? + (theme.useMaterial3 + ? IconThemeData( + color: appBar.foregroundColor ?? + appBarTheme.foregroundColor ?? + theme.colorScheme.onSurfaceVariant, + size: 24, + ) + : null) ?? overallIconTheme; } @@ -136,7 +175,8 @@ class EndState { double get opacity => appBar.toolbarOpacity; double get bottomOpacity => appBar.bottomOpacity; - double get toolbarHeight => appBar.toolbarHeight ?? kToolbarHeight; + double get toolbarHeight => + appBar.toolbarHeight ?? (theme.useMaterial3 ? 64 : kToolbarHeight); double get leadingWidth => appBar.leadingWidth ?? kToolbarHeight; TextStyle? get toolbarTextStyle {