Skip to content

Commit

Permalink
Honor themeData.useMaterialTheme with different defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasWanke committed Dec 30, 2023
1 parent 65f2a5d commit cc98d3f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 15 deletions.
12 changes: 10 additions & 2 deletions lib/src/app_bar/app_bar.dart
Expand Up @@ -22,6 +22,7 @@ class MorphingAppBar extends StatelessWidget implements PreferredSizeWidget {
this.bottom,
this.elevation,
this.shadowColor,
this.surfaceTintColor,
this.shape,
this.backgroundColor,
this.foregroundColor,
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -148,6 +154,7 @@ class MorphingAppBar extends StatelessWidget implements PreferredSizeWidget {
bottom: bottom,
elevation: elevation,
shadowColor: shadowColor,
surfaceTintColor: surfaceTintColor,
shape: shape,
backgroundColor: actualBackgroundColor,
foregroundColor: foregroundColor,
Expand Down Expand Up @@ -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,
Expand Down
66 changes: 53 additions & 13 deletions lib/src/app_bar/state.dart
Expand Up @@ -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 =>
Expand Down Expand Up @@ -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();
}
}
Expand All @@ -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;
}

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit cc98d3f

Please sign in to comment.