From 995bd989dd9ea87f1e5f7f94f281d39b50c5c5c3 Mon Sep 17 00:00:00 2001 From: Nathan Walker Date: Mon, 1 Mar 2021 18:05:03 -0800 Subject: [PATCH] SliverAppBar default backwards compatibility fix (#77022) --- .../flutter/lib/src/material/app_bar.dart | 6 +-- .../test/material/app_bar_theme_test.dart | 45 +++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/packages/flutter/lib/src/material/app_bar.dart b/packages/flutter/lib/src/material/app_bar.dart index 681f1180f9..85542c7813 100644 --- a/packages/flutter/lib/src/material/app_bar.dart +++ b/packages/flutter/lib/src/material/app_bar.dart @@ -1105,7 +1105,7 @@ class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate { final ShapeBorder? shape; final double? toolbarHeight; final double? leadingWidth; - final bool backwardsCompatibility; + final bool? backwardsCompatibility; final TextStyle? toolbarTextStyle; final TextStyle? titleTextStyle; final SystemUiOverlayStyle? systemOverlayStyle; @@ -1450,7 +1450,7 @@ class SliverAppBar extends StatefulWidget { this.shape, this.toolbarHeight = kToolbarHeight, this.leadingWidth, - this.backwardsCompatibility = true, + this.backwardsCompatibility, this.toolbarTextStyle, this.titleTextStyle, this.systemOverlayStyle, @@ -1702,7 +1702,7 @@ class SliverAppBar extends StatefulWidget { /// {@macro flutter.material.appbar.backwardsCompatibility} /// /// This property is used to configure an [AppBar]. - final bool backwardsCompatibility; + final bool? backwardsCompatibility; /// {@macro flutter.material.appbar.toolbarTextStyle} /// diff --git a/packages/flutter/test/material/app_bar_theme_test.dart b/packages/flutter/test/material/app_bar_theme_test.dart index 1248ed4243..6a77705eb5 100644 --- a/packages/flutter/test/material/app_bar_theme_test.dart +++ b/packages/flutter/test/material/app_bar_theme_test.dart @@ -70,6 +70,51 @@ void main() { expect(text.style, appBarTheme.toolbarTextStyle); }); + testWidgets('SliverAppBar allows AppBar to determine backwardsCompatibility', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/77016 + const AppBarTheme appBarTheme = AppBarTheme( + backwardsCompatibility: false, + backgroundColor: Colors.lightBlue, + foregroundColor: Colors.black, + ); + + Widget _buildWithBackwardsCompatibility([bool? enabled]) => MaterialApp( + theme: ThemeData(appBarTheme: appBarTheme), + home: Scaffold(body: CustomScrollView( + slivers: [ + SliverAppBar( + title: const Text('App Bar Title'), + backwardsCompatibility: enabled, + actions: [ + IconButton(icon: const Icon(Icons.share), onPressed: () { }), + ], + ), + ], + )), + ); + + // Backwards compatibility enabled, AppBar should be built with true. + await tester.pumpWidget(_buildWithBackwardsCompatibility(true)); + AppBar appBar = tester.widget(find.byType(AppBar)); + expect(appBar.backwardsCompatibility, true); + + // Backwards compatibility disabled, AppBar should be built with false. + await tester.pumpWidget(_buildWithBackwardsCompatibility(false)); + appBar = tester.widget(find.byType(AppBar)); + expect(appBar.backwardsCompatibility, false); + + // Backwards compatibility unspecified, AppBar should be built with null. + await tester.pumpWidget(_buildWithBackwardsCompatibility()); + appBar = tester.widget(find.byType(AppBar)); + expect(appBar.backwardsCompatibility, null); + + // AppBar should use the backwardsCompatibility of AppBarTheme. + // Since backwardsCompatibility is false, the text color should match the + // foreground color of the AppBarTheme. + final DefaultTextStyle text = _getAppBarText(tester); + expect(text.style.color, appBarTheme.foregroundColor); + }); + testWidgets('AppBar widget properties take priority over theme', (WidgetTester tester) async { const Brightness brightness = Brightness.dark; const SystemUiOverlayStyle systemOverlayStyle = SystemUiOverlayStyle.light;