Skip to content

Commit 44f5403

Browse files
authored
Fix SliverAppBar.large and SliverAppBar.medium do not use foregroundColor (flutter#118322)
1 parent f8628b5 commit 44f5403

File tree

2 files changed

+81
-6
lines changed

2 files changed

+81
-6
lines changed

Diff for: packages/flutter/lib/src/material/app_bar.dart

+9-6
Original file line numberDiff line numberDiff line change
@@ -1652,6 +1652,7 @@ class SliverAppBar extends StatefulWidget {
16521652
actions: actions,
16531653
flexibleSpace: flexibleSpace ?? _ScrollUnderFlexibleSpace(
16541654
title: title,
1655+
foregroundColor: foregroundColor,
16551656
variant: _ScrollUnderFlexibleVariant.medium,
16561657
centerCollapsedTitle: centerTitle,
16571658
primary: primary,
@@ -1753,6 +1754,7 @@ class SliverAppBar extends StatefulWidget {
17531754
actions: actions,
17541755
flexibleSpace: flexibleSpace ?? _ScrollUnderFlexibleSpace(
17551756
title: title,
1757+
foregroundColor: foregroundColor,
17561758
variant: _ScrollUnderFlexibleVariant.large,
17571759
centerCollapsedTitle: centerTitle,
17581760
primary: primary,
@@ -2227,19 +2229,22 @@ enum _ScrollUnderFlexibleVariant { medium, large }
22272229
class _ScrollUnderFlexibleSpace extends StatelessWidget {
22282230
const _ScrollUnderFlexibleSpace({
22292231
this.title,
2232+
this.foregroundColor,
22302233
required this.variant,
22312234
this.centerCollapsedTitle,
22322235
this.primary = true,
22332236
});
22342237

22352238
final Widget? title;
2239+
final Color? foregroundColor;
22362240
final _ScrollUnderFlexibleVariant variant;
22372241
final bool? centerCollapsedTitle;
22382242
final bool primary;
22392243

22402244
@override
22412245
Widget build(BuildContext context) {
22422246
late final ThemeData theme = Theme.of(context);
2247+
late final AppBarTheme appBarTheme = AppBarTheme.of(context);
22432248
final FlexibleSpaceBarSettings settings = context.dependOnInheritedWidgetOfExactType<FlexibleSpaceBarSettings>()!;
22442249
final double topPadding = primary ? MediaQuery.viewPaddingOf(context).top : 0;
22452250
final double collapsedHeight = settings.minExtent - topPadding;
@@ -2259,13 +2264,13 @@ class _ScrollUnderFlexibleSpace extends StatelessWidget {
22592264
if (title != null) {
22602265
collapsedTitle = config.collapsedTextStyle != null
22612266
? DefaultTextStyle(
2262-
style: config.collapsedTextStyle!,
2267+
style: config.collapsedTextStyle!.copyWith(color: foregroundColor ?? appBarTheme.foregroundColor),
22632268
child: title!,
22642269
)
22652270
: title;
22662271
expandedTitle = config.expandedTextStyle != null
22672272
? DefaultTextStyle(
2268-
style: config.expandedTextStyle!,
2273+
style: config.expandedTextStyle!.copyWith(color: foregroundColor ?? appBarTheme.foregroundColor),
22692274
child: title!,
22702275
)
22712276
: title;
@@ -2286,9 +2291,7 @@ class _ScrollUnderFlexibleSpace extends StatelessWidget {
22862291
return true;
22872292
}
22882293
}
2289-
centerTitle = centerCollapsedTitle
2290-
?? theme.appBarTheme.centerTitle
2291-
?? platformCenter();
2294+
centerTitle = centerCollapsedTitle ?? appBarTheme.centerTitle ?? platformCenter();
22922295
}
22932296

22942297
final bool isCollapsed = settings.isScrolledUnder ?? false;
@@ -2307,7 +2310,7 @@ class _ScrollUnderFlexibleSpace extends StatelessWidget {
23072310
alignment: centerTitle
23082311
? Alignment.center
23092312
: AlignmentDirectional.centerStart,
2310-
child: collapsedTitle
2313+
child: collapsedTitle,
23112314
),
23122315
),
23132316
),

Diff for: packages/flutter/test/material/app_bar_theme_test.dart

+72
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,78 @@ void main() {
574574
expect(navToolbar.middleSpacing, 40);
575575
});
576576

577+
testWidgets("SliverAppBar.medium's title uses AppBarTheme.foregroundColor", (WidgetTester tester) async {
578+
const Color foregroundColor = Color(0xff00ff00);
579+
await tester.pumpWidget(MaterialApp(
580+
theme: ThemeData(appBarTheme: const AppBarTheme(foregroundColor: foregroundColor)),
581+
home: CustomScrollView(
582+
slivers: <Widget>[
583+
SliverAppBar.medium(
584+
title: const Text('Medium Title'),
585+
),
586+
],
587+
),
588+
));
589+
590+
final RichText text = tester.firstWidget(find.byType(RichText));
591+
expect(text.text.style!.color, foregroundColor);
592+
});
593+
594+
testWidgets(
595+
"SliverAppBar.medium's foregroundColor takes priority over AppBarTheme.foregroundColor", (WidgetTester tester) async {
596+
const Color foregroundColor = Color(0xff00ff00);
597+
await tester.pumpWidget(MaterialApp(
598+
theme: ThemeData(appBarTheme: const AppBarTheme(foregroundColor: Color(0xffff0000))),
599+
home: CustomScrollView(
600+
slivers: <Widget>[
601+
SliverAppBar.medium(
602+
foregroundColor: foregroundColor,
603+
title: const Text('Medium Title'),
604+
),
605+
],
606+
),
607+
));
608+
609+
final RichText text = tester.firstWidget(find.byType(RichText));
610+
expect(text.text.style!.color, foregroundColor);
611+
});
612+
613+
testWidgets("SliverAppBar.large's title uses AppBarTheme.foregroundColor", (WidgetTester tester) async {
614+
const Color foregroundColor = Color(0xff00ff00);
615+
await tester.pumpWidget(MaterialApp(
616+
theme: ThemeData(appBarTheme: const AppBarTheme(foregroundColor: foregroundColor)),
617+
home: CustomScrollView(
618+
slivers: <Widget>[
619+
SliverAppBar.large(
620+
title: const Text('Large Title'),
621+
),
622+
],
623+
),
624+
));
625+
626+
final RichText text = tester.firstWidget(find.byType(RichText));
627+
expect(text.text.style!.color, foregroundColor);
628+
});
629+
630+
testWidgets(
631+
"SliverAppBar.large's foregroundColor takes priority over AppBarTheme.foregroundColor", (WidgetTester tester) async {
632+
const Color foregroundColor = Color(0xff00ff00);
633+
await tester.pumpWidget(MaterialApp(
634+
theme: ThemeData(appBarTheme: const AppBarTheme(foregroundColor: Color(0xffff0000))),
635+
home: CustomScrollView(
636+
slivers: <Widget>[
637+
SliverAppBar.large(
638+
foregroundColor: foregroundColor,
639+
title: const Text('Large Title'),
640+
),
641+
],
642+
),
643+
));
644+
645+
final RichText text = tester.firstWidget(find.byType(RichText));
646+
expect(text.text.style!.color, foregroundColor);
647+
});
648+
577649
testWidgets('Default AppBarTheme debugFillProperties', (WidgetTester tester) async {
578650
final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder();
579651
const AppBarTheme().debugFillProperties(builder);

0 commit comments

Comments
 (0)