From 44f540338b9d51560c7a7394cc2b7774cdfeecaa Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Thu, 12 Jan 2023 12:18:23 +0200 Subject: [PATCH] Fix `SliverAppBar.large` and `SliverAppBar.medium` do not use `foregroundColor` (#118322) --- .../flutter/lib/src/material/app_bar.dart | 15 ++-- .../test/material/app_bar_theme_test.dart | 72 +++++++++++++++++++ 2 files changed, 81 insertions(+), 6 deletions(-) diff --git a/packages/flutter/lib/src/material/app_bar.dart b/packages/flutter/lib/src/material/app_bar.dart index a3ba8830e5bb5..dcb0597d87459 100644 --- a/packages/flutter/lib/src/material/app_bar.dart +++ b/packages/flutter/lib/src/material/app_bar.dart @@ -1652,6 +1652,7 @@ class SliverAppBar extends StatefulWidget { actions: actions, flexibleSpace: flexibleSpace ?? _ScrollUnderFlexibleSpace( title: title, + foregroundColor: foregroundColor, variant: _ScrollUnderFlexibleVariant.medium, centerCollapsedTitle: centerTitle, primary: primary, @@ -1753,6 +1754,7 @@ class SliverAppBar extends StatefulWidget { actions: actions, flexibleSpace: flexibleSpace ?? _ScrollUnderFlexibleSpace( title: title, + foregroundColor: foregroundColor, variant: _ScrollUnderFlexibleVariant.large, centerCollapsedTitle: centerTitle, primary: primary, @@ -2227,12 +2229,14 @@ 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; @@ -2240,6 +2244,7 @@ class _ScrollUnderFlexibleSpace extends StatelessWidget { @override Widget build(BuildContext context) { late final ThemeData theme = Theme.of(context); + late final AppBarTheme appBarTheme = AppBarTheme.of(context); final FlexibleSpaceBarSettings settings = context.dependOnInheritedWidgetOfExactType()!; final double topPadding = primary ? MediaQuery.viewPaddingOf(context).top : 0; final double collapsedHeight = settings.minExtent - topPadding; @@ -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; @@ -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; @@ -2307,7 +2310,7 @@ class _ScrollUnderFlexibleSpace extends StatelessWidget { alignment: centerTitle ? Alignment.center : AlignmentDirectional.centerStart, - child: collapsedTitle + child: collapsedTitle, ), ), ), diff --git a/packages/flutter/test/material/app_bar_theme_test.dart b/packages/flutter/test/material/app_bar_theme_test.dart index 6c4ba608f8a4e..424dad713ec65 100644 --- a/packages/flutter/test/material/app_bar_theme_test.dart +++ b/packages/flutter/test/material/app_bar_theme_test.dart @@ -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: [ + 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: [ + 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: [ + 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: [ + 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);