diff --git a/packages/flutter/lib/src/material/app_bar.dart b/packages/flutter/lib/src/material/app_bar.dart index 874f258d3c..a46911d02b 100644 --- a/packages/flutter/lib/src/material/app_bar.dart +++ b/packages/flutter/lib/src/material/app_bar.dart @@ -174,8 +174,8 @@ class AppBar extends StatefulWidget implements PreferredSizeWidget { /// [elevation] is specified, it must be non-negative. /// /// If [backgroundColor], [elevation], [brightness], [iconTheme], - /// [actionsIconTheme], or [textTheme] are null, then their [AppBarTheme] - /// values will be used. If the corresponding [AppBarTheme] property is null, + /// [actionsIconTheme], [textTheme] or [centerTitle] are null, then their + /// [AppBarTheme] values will be used. If the corresponding [AppBarTheme] property is null, /// then the default specified in the property's documentation will be used. /// /// Typically used in the [Scaffold.appBar] property. @@ -388,7 +388,8 @@ class AppBar extends StatefulWidget implements PreferredSizeWidget { /// Whether the title should be centered. /// - /// Defaults to being adapted to the current [TargetPlatform]. + /// If this property is null, then [ThemeData.appBarTheme.centerTitle] is used, + /// if that is also null, then value is adapted to the current [TargetPlatform]. final bool centerTitle; /// Whether the title should be wrapped with header [Semantics]. @@ -431,6 +432,8 @@ class AppBar extends StatefulWidget implements PreferredSizeWidget { bool _getEffectiveCenterTitle(ThemeData theme) { if (centerTitle != null) return centerTitle; + if (theme.appBarTheme.centerTitle != null) + return theme.appBarTheme.centerTitle; assert(theme.platform != null); switch (theme.platform) { case TargetPlatform.android: diff --git a/packages/flutter/lib/src/material/app_bar_theme.dart b/packages/flutter/lib/src/material/app_bar_theme.dart index 308e394973..5bda6e1af4 100644 --- a/packages/flutter/lib/src/material/app_bar_theme.dart +++ b/packages/flutter/lib/src/material/app_bar_theme.dart @@ -37,6 +37,7 @@ class AppBarTheme with Diagnosticable { this.iconTheme, this.actionsIconTheme, this.textTheme, + this.centerTitle, }); /// Default value for [AppBar.brightness]. @@ -69,6 +70,11 @@ class AppBarTheme with Diagnosticable { /// If null, [AppBar] uses [ThemeData.primaryTextTheme]. final TextTheme textTheme; + /// Default value for [AppBar.centerTitle]. + /// + /// If null, the value is adapted to current [TargetPlatform]. + final bool centerTitle; + /// Creates a copy of this object with the given fields replaced with the /// new values. AppBarTheme copyWith({ @@ -78,6 +84,7 @@ class AppBarTheme with Diagnosticable { double elevation, IconThemeData iconTheme, TextTheme textTheme, + bool centerTitle, }) { return AppBarTheme( brightness: brightness ?? this.brightness, @@ -86,6 +93,7 @@ class AppBarTheme with Diagnosticable { iconTheme: iconTheme ?? this.iconTheme, actionsIconTheme: actionsIconTheme ?? this.actionsIconTheme, textTheme: textTheme ?? this.textTheme, + centerTitle: centerTitle ?? this.centerTitle, ); } @@ -108,6 +116,7 @@ class AppBarTheme with Diagnosticable { iconTheme: IconThemeData.lerp(a?.iconTheme, b?.iconTheme, t), actionsIconTheme: IconThemeData.lerp(a?.actionsIconTheme, b?.actionsIconTheme, t), textTheme: TextTheme.lerp(a?.textTheme, b?.textTheme, t), + centerTitle: t < 0.5 ? a?.centerTitle : b?.centerTitle, ); } @@ -120,6 +129,7 @@ class AppBarTheme with Diagnosticable { iconTheme, actionsIconTheme, textTheme, + centerTitle, ); } @@ -135,7 +145,8 @@ class AppBarTheme with Diagnosticable { && other.elevation == elevation && other.iconTheme == iconTheme && other.actionsIconTheme == actionsIconTheme - && other.textTheme == textTheme; + && other.textTheme == textTheme + && other.centerTitle == centerTitle; } @override @@ -147,5 +158,6 @@ class AppBarTheme with Diagnosticable { properties.add(DiagnosticsProperty('iconTheme', iconTheme, defaultValue: null)); properties.add(DiagnosticsProperty('actionsIconTheme', actionsIconTheme, defaultValue: null)); properties.add(DiagnosticsProperty('textTheme', textTheme, defaultValue: null)); + properties.add(DiagnosticsProperty('centerTitle', centerTitle, defaultValue: null)); } } diff --git a/packages/flutter/test/material/app_bar_theme_test.dart b/packages/flutter/test/material/app_bar_theme_test.dart index c04e937c42..51ef94b327 100644 --- a/packages/flutter/test/material/app_bar_theme_test.dart +++ b/packages/flutter/test/material/app_bar_theme_test.dart @@ -182,6 +182,43 @@ void main() { // Default value for ThemeData.typography is Typography.material2014() expect(text.style, Typography.material2014().englishLike.bodyText2.merge(Typography.material2014().white.bodyText2).merge(themeData.primaryTextTheme.bodyText2)); }); + + testWidgets('AppBar uses AppBarTheme.centerTitle when centerTitle is null', (WidgetTester tester) async { + await tester.pumpWidget(MaterialApp( + theme: ThemeData(appBarTheme: const AppBarTheme(centerTitle: true)), + home: Scaffold(appBar: AppBar(title: const Text('Title'))), + )); + + final NavigationToolbar navToolBar = tester.widget(find.byType(NavigationToolbar)); + expect(navToolBar.centerMiddle, true); + }); + + testWidgets('AppBar.centerTitle takes priority over AppBarTheme.centerTitle', (WidgetTester tester) async { + await tester.pumpWidget(MaterialApp( + theme: ThemeData(appBarTheme: const AppBarTheme(centerTitle: true)), + home: Scaffold( + appBar: AppBar( + title: const Text('Title'), + centerTitle: false, + )), + )); + + final NavigationToolbar navToolBar = tester.widget(find.byType(NavigationToolbar)); + // The AppBar.centerTitle should be used instead of AppBarTheme.centerTitle. + expect(navToolBar.centerMiddle, false); + }); + + testWidgets('AppBar.centerTitle adapts to TargetPlatform when AppBarTheme.centerTitle is null', (WidgetTester tester) async{ + await tester.pumpWidget(MaterialApp( + theme: ThemeData(platform: TargetPlatform.iOS), + home: Scaffold(appBar: AppBar(title: const Text('Title'))), + )); + + final NavigationToolbar navToolBar = tester.widget(find.byType(NavigationToolbar)); + // When ThemeData.platform is TargetPlatform.iOS, and AppBarTheme is null, + // the value of NavigationToolBar.centerMiddle should be true. + expect(navToolBar.centerMiddle, true); + }); } AppBarTheme _appBarTheme() {