Unverified Commit 852a30b0 authored by Ayush Bherwani's avatar Ayush Bherwani Committed by GitHub

[AppBarTheme] adds centerTitle property (#57736)

parent 02b10801
...@@ -174,8 +174,8 @@ class AppBar extends StatefulWidget implements PreferredSizeWidget { ...@@ -174,8 +174,8 @@ class AppBar extends StatefulWidget implements PreferredSizeWidget {
/// [elevation] is specified, it must be non-negative. /// [elevation] is specified, it must be non-negative.
/// ///
/// If [backgroundColor], [elevation], [brightness], [iconTheme], /// If [backgroundColor], [elevation], [brightness], [iconTheme],
/// [actionsIconTheme], or [textTheme] are null, then their [AppBarTheme] /// [actionsIconTheme], [textTheme] or [centerTitle] are null, then their
/// values will be used. If the corresponding [AppBarTheme] property is null, /// [AppBarTheme] values will be used. If the corresponding [AppBarTheme] property is null,
/// then the default specified in the property's documentation will be used. /// then the default specified in the property's documentation will be used.
/// ///
/// Typically used in the [Scaffold.appBar] property. /// Typically used in the [Scaffold.appBar] property.
...@@ -388,7 +388,8 @@ class AppBar extends StatefulWidget implements PreferredSizeWidget { ...@@ -388,7 +388,8 @@ class AppBar extends StatefulWidget implements PreferredSizeWidget {
/// Whether the title should be centered. /// 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; final bool centerTitle;
/// Whether the title should be wrapped with header [Semantics]. /// Whether the title should be wrapped with header [Semantics].
...@@ -431,6 +432,8 @@ class AppBar extends StatefulWidget implements PreferredSizeWidget { ...@@ -431,6 +432,8 @@ class AppBar extends StatefulWidget implements PreferredSizeWidget {
bool _getEffectiveCenterTitle(ThemeData theme) { bool _getEffectiveCenterTitle(ThemeData theme) {
if (centerTitle != null) if (centerTitle != null)
return centerTitle; return centerTitle;
if (theme.appBarTheme.centerTitle != null)
return theme.appBarTheme.centerTitle;
assert(theme.platform != null); assert(theme.platform != null);
switch (theme.platform) { switch (theme.platform) {
case TargetPlatform.android: case TargetPlatform.android:
......
...@@ -37,6 +37,7 @@ class AppBarTheme with Diagnosticable { ...@@ -37,6 +37,7 @@ class AppBarTheme with Diagnosticable {
this.iconTheme, this.iconTheme,
this.actionsIconTheme, this.actionsIconTheme,
this.textTheme, this.textTheme,
this.centerTitle,
}); });
/// Default value for [AppBar.brightness]. /// Default value for [AppBar.brightness].
...@@ -69,6 +70,11 @@ class AppBarTheme with Diagnosticable { ...@@ -69,6 +70,11 @@ class AppBarTheme with Diagnosticable {
/// If null, [AppBar] uses [ThemeData.primaryTextTheme]. /// If null, [AppBar] uses [ThemeData.primaryTextTheme].
final TextTheme textTheme; 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 /// Creates a copy of this object with the given fields replaced with the
/// new values. /// new values.
AppBarTheme copyWith({ AppBarTheme copyWith({
...@@ -78,6 +84,7 @@ class AppBarTheme with Diagnosticable { ...@@ -78,6 +84,7 @@ class AppBarTheme with Diagnosticable {
double elevation, double elevation,
IconThemeData iconTheme, IconThemeData iconTheme,
TextTheme textTheme, TextTheme textTheme,
bool centerTitle,
}) { }) {
return AppBarTheme( return AppBarTheme(
brightness: brightness ?? this.brightness, brightness: brightness ?? this.brightness,
...@@ -86,6 +93,7 @@ class AppBarTheme with Diagnosticable { ...@@ -86,6 +93,7 @@ class AppBarTheme with Diagnosticable {
iconTheme: iconTheme ?? this.iconTheme, iconTheme: iconTheme ?? this.iconTheme,
actionsIconTheme: actionsIconTheme ?? this.actionsIconTheme, actionsIconTheme: actionsIconTheme ?? this.actionsIconTheme,
textTheme: textTheme ?? this.textTheme, textTheme: textTheme ?? this.textTheme,
centerTitle: centerTitle ?? this.centerTitle,
); );
} }
...@@ -108,6 +116,7 @@ class AppBarTheme with Diagnosticable { ...@@ -108,6 +116,7 @@ class AppBarTheme with Diagnosticable {
iconTheme: IconThemeData.lerp(a?.iconTheme, b?.iconTheme, t), iconTheme: IconThemeData.lerp(a?.iconTheme, b?.iconTheme, t),
actionsIconTheme: IconThemeData.lerp(a?.actionsIconTheme, b?.actionsIconTheme, t), actionsIconTheme: IconThemeData.lerp(a?.actionsIconTheme, b?.actionsIconTheme, t),
textTheme: TextTheme.lerp(a?.textTheme, b?.textTheme, 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 { ...@@ -120,6 +129,7 @@ class AppBarTheme with Diagnosticable {
iconTheme, iconTheme,
actionsIconTheme, actionsIconTheme,
textTheme, textTheme,
centerTitle,
); );
} }
...@@ -135,7 +145,8 @@ class AppBarTheme with Diagnosticable { ...@@ -135,7 +145,8 @@ class AppBarTheme with Diagnosticable {
&& other.elevation == elevation && other.elevation == elevation
&& other.iconTheme == iconTheme && other.iconTheme == iconTheme
&& other.actionsIconTheme == actionsIconTheme && other.actionsIconTheme == actionsIconTheme
&& other.textTheme == textTheme; && other.textTheme == textTheme
&& other.centerTitle == centerTitle;
} }
@override @override
...@@ -147,5 +158,6 @@ class AppBarTheme with Diagnosticable { ...@@ -147,5 +158,6 @@ class AppBarTheme with Diagnosticable {
properties.add(DiagnosticsProperty<IconThemeData>('iconTheme', iconTheme, defaultValue: null)); properties.add(DiagnosticsProperty<IconThemeData>('iconTheme', iconTheme, defaultValue: null));
properties.add(DiagnosticsProperty<IconThemeData>('actionsIconTheme', actionsIconTheme, defaultValue: null)); properties.add(DiagnosticsProperty<IconThemeData>('actionsIconTheme', actionsIconTheme, defaultValue: null));
properties.add(DiagnosticsProperty<TextTheme>('textTheme', textTheme, defaultValue: null)); properties.add(DiagnosticsProperty<TextTheme>('textTheme', textTheme, defaultValue: null));
properties.add(DiagnosticsProperty<bool>('centerTitle', centerTitle, defaultValue: null));
} }
} }
...@@ -182,6 +182,43 @@ void main() { ...@@ -182,6 +182,43 @@ void main() {
// Default value for ThemeData.typography is Typography.material2014() // Default value for ThemeData.typography is Typography.material2014()
expect(text.style, Typography.material2014().englishLike.bodyText2.merge(Typography.material2014().white.bodyText2).merge(themeData.primaryTextTheme.bodyText2)); 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() { AppBarTheme _appBarTheme() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment