[AppBarTheme] adds centerTitle property (#57736)

This commit is contained in:
Ayush Bherwani 2020-05-22 03:27:03 +05:30 committed by GitHub
parent 02b1080101
commit 852a30b003
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 4 deletions

View file

@ -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:

View file

@ -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<IconThemeData>('iconTheme', iconTheme, defaultValue: null));
properties.add(DiagnosticsProperty<IconThemeData>('actionsIconTheme', actionsIconTheme, defaultValue: null));
properties.add(DiagnosticsProperty<TextTheme>('textTheme', textTheme, defaultValue: null));
properties.add(DiagnosticsProperty<bool>('centerTitle', centerTitle, defaultValue: null));
}
}

View file

@ -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() {