Skip to content

Commit

Permalink
Fix SliverAppBar.large and SliverAppBar.medium do not use `foregr…
Browse files Browse the repository at this point in the history
…oundColor` (#118322)
  • Loading branch information
TahaTesser authored Jan 12, 2023
1 parent f8628b5 commit 44f5403
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 6 deletions.
15 changes: 9 additions & 6 deletions packages/flutter/lib/src/material/app_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1652,6 +1652,7 @@ class SliverAppBar extends StatefulWidget {
actions: actions,
flexibleSpace: flexibleSpace ?? _ScrollUnderFlexibleSpace(
title: title,
foregroundColor: foregroundColor,
variant: _ScrollUnderFlexibleVariant.medium,
centerCollapsedTitle: centerTitle,
primary: primary,
Expand Down Expand Up @@ -1753,6 +1754,7 @@ class SliverAppBar extends StatefulWidget {
actions: actions,
flexibleSpace: flexibleSpace ?? _ScrollUnderFlexibleSpace(
title: title,
foregroundColor: foregroundColor,
variant: _ScrollUnderFlexibleVariant.large,
centerCollapsedTitle: centerTitle,
primary: primary,
Expand Down Expand Up @@ -2227,19 +2229,22 @@ enum _ScrollUnderFlexibleVariant { medium, large }
class _ScrollUnderFlexibleSpace extends StatelessWidget {
const _ScrollUnderFlexibleSpace({
this.title,
this.foregroundColor,
required this.variant,
this.centerCollapsedTitle,
this.primary = true,
});

final Widget? title;
final Color? foregroundColor;
final _ScrollUnderFlexibleVariant variant;
final bool? centerCollapsedTitle;
final bool primary;

@override
Widget build(BuildContext context) {
late final ThemeData theme = Theme.of(context);
late final AppBarTheme appBarTheme = AppBarTheme.of(context);
final FlexibleSpaceBarSettings settings = context.dependOnInheritedWidgetOfExactType<FlexibleSpaceBarSettings>()!;
final double topPadding = primary ? MediaQuery.viewPaddingOf(context).top : 0;
final double collapsedHeight = settings.minExtent - topPadding;
Expand All @@ -2259,13 +2264,13 @@ class _ScrollUnderFlexibleSpace extends StatelessWidget {
if (title != null) {
collapsedTitle = config.collapsedTextStyle != null
? DefaultTextStyle(
style: config.collapsedTextStyle!,
style: config.collapsedTextStyle!.copyWith(color: foregroundColor ?? appBarTheme.foregroundColor),
child: title!,
)
: title;
expandedTitle = config.expandedTextStyle != null
? DefaultTextStyle(
style: config.expandedTextStyle!,
style: config.expandedTextStyle!.copyWith(color: foregroundColor ?? appBarTheme.foregroundColor),
child: title!,
)
: title;
Expand All @@ -2286,9 +2291,7 @@ class _ScrollUnderFlexibleSpace extends StatelessWidget {
return true;
}
}
centerTitle = centerCollapsedTitle
?? theme.appBarTheme.centerTitle
?? platformCenter();
centerTitle = centerCollapsedTitle ?? appBarTheme.centerTitle ?? platformCenter();
}

final bool isCollapsed = settings.isScrolledUnder ?? false;
Expand All @@ -2307,7 +2310,7 @@ class _ScrollUnderFlexibleSpace extends StatelessWidget {
alignment: centerTitle
? Alignment.center
: AlignmentDirectional.centerStart,
child: collapsedTitle
child: collapsedTitle,
),
),
),
Expand Down
72 changes: 72 additions & 0 deletions packages/flutter/test/material/app_bar_theme_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,78 @@ void main() {
expect(navToolbar.middleSpacing, 40);
});

testWidgets("SliverAppBar.medium's title uses AppBarTheme.foregroundColor", (WidgetTester tester) async {
const Color foregroundColor = Color(0xff00ff00);
await tester.pumpWidget(MaterialApp(
theme: ThemeData(appBarTheme: const AppBarTheme(foregroundColor: foregroundColor)),
home: CustomScrollView(
slivers: <Widget>[
SliverAppBar.medium(
title: const Text('Medium Title'),
),
],
),
));

final RichText text = tester.firstWidget(find.byType(RichText));
expect(text.text.style!.color, foregroundColor);
});

testWidgets(
"SliverAppBar.medium's foregroundColor takes priority over AppBarTheme.foregroundColor", (WidgetTester tester) async {
const Color foregroundColor = Color(0xff00ff00);
await tester.pumpWidget(MaterialApp(
theme: ThemeData(appBarTheme: const AppBarTheme(foregroundColor: Color(0xffff0000))),
home: CustomScrollView(
slivers: <Widget>[
SliverAppBar.medium(
foregroundColor: foregroundColor,
title: const Text('Medium Title'),
),
],
),
));

final RichText text = tester.firstWidget(find.byType(RichText));
expect(text.text.style!.color, foregroundColor);
});

testWidgets("SliverAppBar.large's title uses AppBarTheme.foregroundColor", (WidgetTester tester) async {
const Color foregroundColor = Color(0xff00ff00);
await tester.pumpWidget(MaterialApp(
theme: ThemeData(appBarTheme: const AppBarTheme(foregroundColor: foregroundColor)),
home: CustomScrollView(
slivers: <Widget>[
SliverAppBar.large(
title: const Text('Large Title'),
),
],
),
));

final RichText text = tester.firstWidget(find.byType(RichText));
expect(text.text.style!.color, foregroundColor);
});

testWidgets(
"SliverAppBar.large's foregroundColor takes priority over AppBarTheme.foregroundColor", (WidgetTester tester) async {
const Color foregroundColor = Color(0xff00ff00);
await tester.pumpWidget(MaterialApp(
theme: ThemeData(appBarTheme: const AppBarTheme(foregroundColor: Color(0xffff0000))),
home: CustomScrollView(
slivers: <Widget>[
SliverAppBar.large(
foregroundColor: foregroundColor,
title: const Text('Large Title'),
),
],
),
));

final RichText text = tester.firstWidget(find.byType(RichText));
expect(text.text.style!.color, foregroundColor);
});

testWidgets('Default AppBarTheme debugFillProperties', (WidgetTester tester) async {
final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder();
const AppBarTheme().debugFillProperties(builder);
Expand Down

0 comments on commit 44f5403

Please sign in to comment.