Update AppBar and AppBar Theme to new Theme conventions and latest Material spec (#71184)

This commit is contained in:
Hans Muller
2020-12-04 09:12:31 -08:00
committed by GitHub
parent 8f3a12f144
commit d375a83910
4 changed files with 817 additions and 324 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -5,14 +5,16 @@
import 'dart:ui' show lerpDouble;
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'text_theme.dart';
import 'theme.dart';
/// Defines default property values for descendant [AppBar] widgets.
/// Overrides the default values of visual properties for descendant
/// [AppBar] widgets.
///
/// Descendant widgets obtain the current [AppBarTheme] object using
/// Descendant widgets obtain the current [AppBarTheme] object with
/// `AppBarTheme.of(context)`. Instances of [AppBarTheme] can be customized
/// with [AppBarTheme.copyWith].
///
@@ -20,19 +22,16 @@ import 'theme.dart';
/// [ThemeData.appBarTheme].
///
/// All [AppBarTheme] properties are `null` by default. When null, the [AppBar]
/// will use the values from [ThemeData] if they exist, otherwise it will
/// provide its own defaults.
///
/// See also:
///
/// * [ThemeData], which describes the overall theme information for the
/// application.
/// compute its own default values, typically based on the overall theme's
/// [ThemeData.colorScheme], [ThemeData.textTheme], and [ThemeData.iconTheme].
@immutable
class AppBarTheme with Diagnosticable {
/// Creates a theme that can be used for [ThemeData.appBarTheme].
const AppBarTheme({
this.brightness,
this.color,
this.backgroundColor,
this.foregroundColor,
this.elevation,
this.shadowColor,
this.iconTheme,
@@ -40,69 +39,151 @@ class AppBarTheme with Diagnosticable {
this.textTheme,
this.centerTitle,
this.titleSpacing,
this.toolbarTextStyle,
this.titleTextStyle,
this.systemOverlayStyle,
});
/// Default value for [AppBar.brightness].
/// This property is obsolete, please use [systemOverlayStyle] instead.
///
/// If null, [AppBar] uses [ThemeData.primaryColorBrightness].
/// Overrides the default value of the obsolete [AppBar.brightness]
/// property which implicitly defines [AppBar.systemOverlayStyle] in
/// all descendant [AppBar] widgets.
///
/// See also:
///
/// * [systemOverlayStyle], which overrides the default value of
/// [AppBar.systemOverlayStyle] in all descendant [AppBar] widgets.
/// * [AppBar.backwardsCompatibility], which forces [AppBar] to depend
/// on this obsolete property.
final Brightness? brightness;
/// Default value for [AppBar.backgroundColor].
/// Obsolete property that overrides the default value of
/// [AppBar.backgroundColor] in all descendant [AppBar] widgets.
///
/// If null, [AppBar] uses [ThemeData.primaryColor].
/// See also:
///
/// * [backgroundColor], which serves this same purpose
/// as this property, but has a consistent name.
/// * [AppBar.backwardsCompatibility], which forces [AppBar] to depend
/// on this obsolete property.
final Color? color;
/// Default value for [AppBar.elevation].
/// Overrides the default value of [AppBar.backgroundColor] in all
/// descendant [AppBar] widgets.
///
/// If null, [AppBar] uses a default value of 4.0.
/// See also:
///
/// * [foregroundColor], which overrides the default value for
/// [AppBar.foregroundColor] in all descendant widgets.
final Color? backgroundColor;
/// Overrides the default value of [AppBar.foregroundColor] in all
/// descendant widgets.
///
/// See also:
///
/// * [backgroundColor], which overrides the default value for
/// [AppBar.backgroundColor] in all descendant [AppBar] widgets.
final Color? foregroundColor;
/// Overrides the default value of [AppBar.elevation] in all
/// descendant [AppBar] widgets.
final double? elevation;
/// Default value for [AppBar.shadowColor].
///
/// If null, [AppBar] uses a default value of fully opaque black.
/// Overrides the default value for [AppBar.shadowColor] in all
/// descendant widgets.
final Color? shadowColor;
/// Default value for [AppBar.iconTheme].
/// Overrides the default value of [AppBar.iconTheme] in all
/// descendant [AppBar] widgets.
///
/// If null, [AppBar] uses [ThemeData.primaryIconTheme].
/// See also:
///
/// * [actionsIconTheme], which overrides the default value for
/// [AppBar.actionsIconTheme] in all descendant [AppBar] widgets.
/// * [foregroundColor], which overrides the default value
/// [AppBar.foregroundColor] in all descendant widgets.
final IconThemeData? iconTheme;
/// Default value for [AppBar.actionsIconTheme].
/// Overrides the default value of [AppBar.actionsIconTheme] in all
/// descendant widgets.
///
/// If null, [AppBar] uses [ThemeData.primaryIconTheme].
/// See also:
///
/// * [iconTheme], which overrides the default value for
/// [AppBar.iconTheme] in all descendant widgets.
/// * [foregroundColor], which overrides the default value
/// [AppBar.foregroundColor] in all descendant widgets.
final IconThemeData? actionsIconTheme;
/// Default value for [AppBar.textTheme].
/// Overrides the default value of the obsolete [AppBar.textTheme]
/// property in all descendant [AppBar] widgets.
///
/// If null, [AppBar] uses [ThemeData.primaryTextTheme].
/// See also:
///
/// * [toolbarTextStyle], which overrides the default value for
/// [AppBar.toolbarTextStyle in all descendant [AppBar] widgets.
/// * [titleTextStyle], which overrides the default value for
/// [AppBar.titleTextStyle in all descendant [AppBar] widgets.
final TextTheme? textTheme;
/// Default value for [AppBar.centerTitle].
///
/// If null, the value is adapted to current [TargetPlatform].
/// Overrides the default value for [AppBar.centerTitle].
/// property in all descendant widgets.
final bool? centerTitle;
/// Default value for [AppBar.titleSpacing].
/// Overrides the default value for the obsolete [AppBar.titleSpacing]
/// property in all descendant [AppBar] widgets.
///
/// If null, [AppBar] uses default value of [NavigationToolbar.kMiddleSpacing].
final double? titleSpacing;
/// Overrides the default value for the obsolete [AppBar.toolbarTextStyle]
/// property in all descendant [AppBar] widgets.
///
/// See also:
///
/// * [titleTextStyle], which overrides the default of [AppBar.titleTextStyle]
/// in all descendant [AppBar] widgets.
final TextStyle? toolbarTextStyle;
/// Overrides the default value of [AppBar.titleTextStyle]
/// property in all descendant [AppBar] widgets.
///
/// See also:
///
/// * [toolbarTextStyle], which overrides the default of [AppBar.toolbarTextStyle]
/// in all descendant [AppBar] widgets.
final TextStyle? titleTextStyle;
/// Overrides the default value of [AppBar.systemOverlayStyle]
/// property in all descendant [AppBar] widgets.
final SystemUiOverlayStyle? systemOverlayStyle;
/// Creates a copy of this object with the given fields replaced with the
/// new values.
AppBarTheme copyWith({
IconThemeData? actionsIconTheme,
Brightness? brightness,
Color? color,
Color? backgroundColor,
Color? foregroundColor,
double? elevation,
Color? shadowColor,
IconThemeData? iconTheme,
TextTheme? textTheme,
bool? centerTitle,
double? titleSpacing,
TextStyle? toolbarTextStyle,
TextStyle? titleTextStyle,
SystemUiOverlayStyle? systemOverlayStyle,
}) {
return AppBarTheme(
brightness: brightness ?? this.brightness,
color: color ?? this.color,
backgroundColor: backgroundColor ?? this.backgroundColor,
foregroundColor: foregroundColor ?? this.foregroundColor,
elevation: elevation ?? this.elevation,
shadowColor: shadowColor ?? this.shadowColor,
iconTheme: iconTheme ?? this.iconTheme,
@@ -110,6 +191,9 @@ class AppBarTheme with Diagnosticable {
textTheme: textTheme ?? this.textTheme,
centerTitle: centerTitle ?? this.centerTitle,
titleSpacing: titleSpacing ?? this.titleSpacing,
toolbarTextStyle: toolbarTextStyle ?? this.toolbarTextStyle,
titleTextStyle: titleTextStyle ?? this.titleTextStyle,
systemOverlayStyle: systemOverlayStyle ?? this.systemOverlayStyle,
);
}
@@ -128,6 +212,8 @@ class AppBarTheme with Diagnosticable {
return AppBarTheme(
brightness: t < 0.5 ? a?.brightness : b?.brightness,
color: Color.lerp(a?.color, b?.color, t),
backgroundColor: Color.lerp(a?.backgroundColor, b?.backgroundColor, t),
foregroundColor: Color.lerp(a?.foregroundColor, b?.foregroundColor, t),
elevation: lerpDouble(a?.elevation, b?.elevation, t),
shadowColor: Color.lerp(a?.shadowColor, b?.shadowColor, t),
iconTheme: IconThemeData.lerp(a?.iconTheme, b?.iconTheme, t),
@@ -135,6 +221,9 @@ class AppBarTheme with Diagnosticable {
textTheme: TextTheme.lerp(a?.textTheme, b?.textTheme, t),
centerTitle: t < 0.5 ? a?.centerTitle : b?.centerTitle,
titleSpacing: lerpDouble(a?.titleSpacing, b?.titleSpacing, t),
toolbarTextStyle: TextStyle.lerp(a?.toolbarTextStyle, b?.toolbarTextStyle, t),
titleTextStyle: TextStyle.lerp(a?.titleTextStyle, b?.titleTextStyle, t),
systemOverlayStyle: t < 0.5 ? a?.systemOverlayStyle : b?.systemOverlayStyle,
);
}
@@ -143,6 +232,8 @@ class AppBarTheme with Diagnosticable {
return hashValues(
brightness,
color,
backgroundColor,
foregroundColor,
elevation,
shadowColor,
iconTheme,
@@ -150,6 +241,9 @@ class AppBarTheme with Diagnosticable {
textTheme,
centerTitle,
titleSpacing,
toolbarTextStyle,
titleTextStyle,
systemOverlayStyle,
);
}
@@ -162,13 +256,18 @@ class AppBarTheme with Diagnosticable {
return other is AppBarTheme
&& other.brightness == brightness
&& other.color == color
&& other.backgroundColor == backgroundColor
&& other.foregroundColor == foregroundColor
&& other.elevation == elevation
&& other.shadowColor == shadowColor
&& other.iconTheme == iconTheme
&& other.actionsIconTheme == actionsIconTheme
&& other.textTheme == textTheme
&& other.centerTitle == centerTitle
&& other.titleSpacing == titleSpacing;
&& other.titleSpacing == titleSpacing
&& other.toolbarTextStyle == toolbarTextStyle
&& other.titleTextStyle == titleTextStyle
&& other.systemOverlayStyle == systemOverlayStyle;
}
@override
@@ -176,6 +275,8 @@ class AppBarTheme with Diagnosticable {
super.debugFillProperties(properties);
properties.add(DiagnosticsProperty<Brightness>('brightness', brightness, defaultValue: null));
properties.add(ColorProperty('color', color, defaultValue: null));
properties.add(ColorProperty('backgroundColor', backgroundColor, defaultValue: null));
properties.add(ColorProperty('foregroundColor', foregroundColor, defaultValue: null));
properties.add(DiagnosticsProperty<double>('elevation', elevation, defaultValue: null));
properties.add(ColorProperty('shadowColor', shadowColor, defaultValue: null));
properties.add(DiagnosticsProperty<IconThemeData>('iconTheme', iconTheme, defaultValue: null));
@@ -183,5 +284,7 @@ class AppBarTheme with Diagnosticable {
properties.add(DiagnosticsProperty<TextTheme>('textTheme', textTheme, defaultValue: null));
properties.add(DiagnosticsProperty<bool>('centerTitle', centerTitle, defaultValue: null));
properties.add(DiagnosticsProperty<double>('titleSpacing', titleSpacing, defaultValue: null));
properties.add(DiagnosticsProperty<TextStyle>('toolbarTextStyle', toolbarTextStyle, defaultValue: null));
properties.add(DiagnosticsProperty<TextStyle>('titleTextStyle', titleTextStyle, defaultValue: null));
}
}

View File

@@ -27,6 +27,7 @@ Widget buildSliverAppBarApp({
primary: true,
slivers: <Widget>[
SliverAppBar(
backwardsCompatibility: false,
title: const Text('AppBar Title'),
floating: floating,
pinned: pinned,
@@ -72,6 +73,7 @@ void main() {
theme: ThemeData(platform: TargetPlatform.android),
home: Scaffold(
appBar: AppBar(
backwardsCompatibility: false,
title: const Text('X'),
),
),
@@ -92,6 +94,7 @@ void main() {
theme: ThemeData(platform: platform),
home: Scaffold(
appBar: AppBar(
backwardsCompatibility: false,
title: const Text('X'),
),
),
@@ -110,6 +113,7 @@ void main() {
theme: ThemeData(platform: platform),
home: Scaffold(
appBar: AppBar(
backwardsCompatibility: false,
title: const Text('X'),
actions: const <Widget>[
Icon(Icons.thumb_up),
@@ -131,6 +135,7 @@ void main() {
theme: ThemeData(platform: platform),
home: Scaffold(
appBar: AppBar(
backwardsCompatibility: false,
title: const Text('X'),
actions: const <Widget>[
Icon(Icons.thumb_up),
@@ -153,6 +158,7 @@ void main() {
theme: ThemeData(platform: TargetPlatform.android),
home: Scaffold(
appBar: AppBar(
backwardsCompatibility: false,
centerTitle: true,
title: const Text('X'),
),
@@ -172,6 +178,7 @@ void main() {
MaterialApp(
home: Scaffold(
appBar: AppBar(
backwardsCompatibility: false,
centerTitle: false,
title: const Placeholder(key: Key('X')),
),
@@ -191,6 +198,7 @@ void main() {
textDirection: TextDirection.rtl,
child: Scaffold(
appBar: AppBar(
backwardsCompatibility: false,
centerTitle: false,
title: const Placeholder(key: Key('X')),
),
@@ -209,6 +217,7 @@ void main() {
MaterialApp(
home: Scaffold(
appBar: AppBar(
backwardsCompatibility: false,
centerTitle: false,
titleSpacing: 32.0,
title: const Placeholder(key: Key('X')),
@@ -229,6 +238,7 @@ void main() {
textDirection: TextDirection.rtl,
child: Scaffold(
appBar: AppBar(
backwardsCompatibility: false,
centerTitle: false,
titleSpacing: 32.0,
title: const Placeholder(key: Key('X')),
@@ -250,6 +260,7 @@ void main() {
MaterialApp(
home: Scaffold(
appBar: AppBar(
backwardsCompatibility: false,
centerTitle: false,
title: const Text('X'),
),
@@ -271,6 +282,7 @@ void main() {
textDirection: TextDirection.rtl,
child: Scaffold(
appBar: AppBar(
backwardsCompatibility: false,
centerTitle: false,
title: const Text('X'),
),
@@ -296,6 +308,7 @@ void main() {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
backwardsCompatibility: false,
leading: leading,
centerTitle: false,
title: Container(
@@ -354,6 +367,7 @@ void main() {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
backwardsCompatibility: false,
leading: leading,
centerTitle: true,
title: Container(
@@ -408,6 +422,7 @@ void main() {
textDirection: TextDirection.rtl,
child: Scaffold(
appBar: AppBar(
backwardsCompatibility: false,
leading: leading,
centerTitle: true,
title: Container(
@@ -453,6 +468,7 @@ void main() {
home: SizedBox(
height: kToolbarHeight,
child: AppBar(
backwardsCompatibility: false,
leading: const Text('L'),
title: const Text('No Scaffold'),
actions: const <Widget>[Text('A1'), Text('A2')],
@@ -476,6 +492,7 @@ void main() {
width: 0.0,
child: Scaffold(
appBar: AppBar(
backwardsCompatibility: false,
title: const Text('X'),
),
),
@@ -499,6 +516,7 @@ void main() {
MaterialApp(
home: Scaffold(
appBar: AppBar(
backwardsCompatibility: false,
key: appBarKey,
leading: SizedBox(key: leadingKey, height: 50.0),
title: SizedBox(key: titleKey, height: 40.0),
@@ -526,6 +544,7 @@ void main() {
theme: ThemeData(platform: TargetPlatform.android),
home: Scaffold(
appBar: AppBar(
backwardsCompatibility: false,
title: const Text('X'),
),
drawer: Column(), // Doesn't really matter. Triggers a hamburger regardless.
@@ -544,6 +563,7 @@ void main() {
theme: ThemeData(platform: TargetPlatform.android),
home: Scaffold(
appBar: AppBar(
backwardsCompatibility: false,
title: const Text('X'),
actions: const <Widget> [
IconButton(
@@ -890,6 +910,7 @@ void main() {
home: CustomScrollView(
slivers: <Widget>[
SliverAppBar(
backwardsCompatibility: false,
title: const Text('Title'),
forceElevated: forceElevated,
),
@@ -945,7 +966,10 @@ void main() {
data: topPadding100,
child: Scaffold(
primary: true,
appBar: AppBar(title: const Text('title')),
appBar: AppBar(
backwardsCompatibility: false,
title: const Text('title')
),
),
),
),
@@ -968,6 +992,7 @@ void main() {
child: Scaffold(
primary: false,
appBar: AppBar(
backwardsCompatibility: false,
bottom: PreferredSize(
preferredSize: const Size.fromHeight(200.0),
child: Container(),
@@ -994,6 +1019,7 @@ void main() {
child: Scaffold(
primary: true,
appBar: AppBar(
backwardsCompatibility: false,
bottom: PreferredSize(
preferredSize: const Size.fromHeight(200.0),
child: Container(),
@@ -1018,6 +1044,7 @@ void main() {
child: MediaQuery(
data: topPadding100,
child: AppBar(
backwardsCompatibility: false,
primary: false,
title: const Text('title'),
),
@@ -1047,6 +1074,7 @@ void main() {
body: Column(
children: <Widget>[
AppBar(
backwardsCompatibility: false,
title: const Text('title'),
),
],
@@ -1084,7 +1112,10 @@ void main() {
MaterialApp(
home: Scaffold(
drawer: const Drawer(),
appBar: AppBar(automaticallyImplyLeading: false),
appBar: AppBar(
backwardsCompatibility: false,
automaticallyImplyLeading: false,
),
),
),
);
@@ -1098,6 +1129,7 @@ void main() {
MaterialApp(
home: Center(
child: AppBar(
backwardsCompatibility: false,
title: const Text('Abc'),
actions: <Widget>[
IconButton(
@@ -1139,6 +1171,7 @@ void main() {
MaterialApp(
home: Center(
child: AppBar(
backwardsCompatibility: false,
leading: Placeholder(key: key),
title: const Text('Abc'),
actions: const <Widget>[
@@ -1160,6 +1193,7 @@ void main() {
MaterialApp(
home: Center(
child: AppBar(
backwardsCompatibility: false,
leading: Placeholder(key: key),
title: const Text('Abc'),
actions: const <Widget>[
@@ -1190,6 +1224,7 @@ void main() {
MaterialApp(
home: Center(
child: AppBar(
backwardsCompatibility: false,
leading: Placeholder(key: key),
title: const Text('Abc'),
actions: const <Widget>[
@@ -1231,6 +1266,7 @@ void main() {
MaterialApp(
home: Center(
child: AppBar(
backwardsCompatibility: false,
leading: Placeholder(key: key),
title: const Text('Abc'),
actions: const <Widget>[
@@ -1278,6 +1314,7 @@ void main() {
child: Scaffold(
primary: false,
appBar: AppBar(
backwardsCompatibility: false,
leading: Placeholder(key: leadingKey), // Forced to 56x56, see _kLeadingWidth in app_bar.dart.
title: Placeholder(key: titleKey, fallbackHeight: kToolbarHeight),
actions: <Widget>[ Placeholder(key: trailingKey, fallbackWidth: 10) ],
@@ -1322,6 +1359,7 @@ void main() {
primary: true,
slivers: <Widget>[
SliverAppBar(
backwardsCompatibility: false,
leading: Placeholder(key: leadingKey),
title: Placeholder(key: titleKey, fallbackHeight: kToolbarHeight),
actions: <Widget>[ Placeholder(key: trailingKey) ],
@@ -1359,6 +1397,7 @@ void main() {
primary: true,
slivers: <Widget>[
SliverAppBar(
backwardsCompatibility: false,
leading: Placeholder(key: leadingKey),
title: Placeholder(key: titleKey),
actions: <Widget>[ Placeholder(key: trailingKey) ],
@@ -1380,6 +1419,7 @@ void main() {
MaterialApp(
home: Center(
child: AppBar(
backwardsCompatibility: false,
leading: const Text('Leading'),
title: const Text('Title'),
actions: const <Widget>[
@@ -1465,6 +1505,7 @@ void main() {
textDirection: TextDirection.rtl,
child: Center(
child: AppBar(
backwardsCompatibility: false,
leading: const Text('Leading'),
title: const Text('Title'),
actions: const <Widget>[
@@ -1553,6 +1594,7 @@ void main() {
MaterialApp(
home: Center(
child: AppBar(
backwardsCompatibility: false,
leading: const Text('Leading'),
title: const ExcludeSemantics(child: Text('Title')),
excludeHeaderSemantics: true,
@@ -1610,6 +1652,7 @@ void main() {
home: CustomScrollView(
slivers: <Widget>[
SliverAppBar(
backwardsCompatibility: false,
leading: Text('Leading'),
flexibleSpace: ExcludeSemantics(child: Text('Title')),
actions: <Widget>[Text('Action 1')],
@@ -1680,6 +1723,7 @@ void main() {
home: CustomScrollView(
slivers: <Widget>[
SliverAppBar(
backwardsCompatibility: false,
leading: Text('Leading'),
flexibleSpace: Text('Flexible space'),
actions: <Widget>[Text('Action 1')],
@@ -1753,11 +1797,15 @@ void main() {
await tester.pumpWidget(MaterialApp(
theme: darkTheme,
home: Scaffold(
appBar: AppBar(title: const Text('test')),
appBar: AppBar(
backwardsCompatibility: false,
title: const Text('test')
),
),
));
expect(darkTheme.primaryColorBrightness, Brightness.dark);
expect(darkTheme.colorScheme.brightness, Brightness.dark);
expect(SystemChrome.latestStyle, const SystemUiOverlayStyle(
statusBarBrightness: Brightness.dark,
statusBarIconBrightness: Brightness.light,
@@ -1766,14 +1814,20 @@ void main() {
testWidgets('AppBar draws a dark system bar for a light background', (WidgetTester tester) async {
final ThemeData lightTheme = ThemeData(primaryColor: Colors.white);
await tester.pumpWidget(MaterialApp(
theme: lightTheme,
home: Scaffold(
appBar: AppBar(title: const Text('test')),
await tester.pumpWidget(
MaterialApp(
theme: lightTheme,
home: Scaffold(
appBar: AppBar(
backwardsCompatibility: false,
title: const Text('test')
),
),
),
));
);
expect(lightTheme.primaryColorBrightness, Brightness.light);
expect(lightTheme.colorScheme.brightness, Brightness.light);
expect(SystemChrome.latestStyle, const SystemUiOverlayStyle(
statusBarBrightness: Brightness.light,
statusBarIconBrightness: Brightness.dark,
@@ -1793,6 +1847,7 @@ void main() {
body: CustomScrollView(
slivers: <Widget>[
SliverAppBar(
backwardsCompatibility: false,
expandedHeight: appBarHeight,
pinned: false,
floating: true,
@@ -1846,6 +1901,7 @@ void main() {
await tester.pumpWidget(
MaterialApp(
home: AppBar(
backwardsCompatibility: false,
leading: const Text('L'),
title: const Text('No Scaffold'),
actions: const <Widget>[Text('A1'), Text('A2')],
@@ -1869,6 +1925,7 @@ void main() {
await tester.pumpWidget(
MaterialApp(
home: AppBar(
backwardsCompatibility: false,
leading: const Text('L'),
title: const Text('No Scaffold'),
actions: const <Widget>[Text('A1'), Text('A2')],
@@ -1892,6 +1949,7 @@ void main() {
home: CustomScrollView(
slivers: <Widget>[
SliverAppBar(
backwardsCompatibility: false,
leading: Text('L'),
title: Text('No Scaffold'),
actions: <Widget>[Text('A1'), Text('A2')],
@@ -1919,6 +1977,7 @@ void main() {
home: CustomScrollView(
slivers: <Widget>[
SliverAppBar(
backwardsCompatibility: false,
leading: Text('L'),
title: Text('No Scaffold'),
actions: <Widget>[Text('A1'), Text('A2')],
@@ -1949,6 +2008,7 @@ void main() {
data: MediaQuery.of(context).copyWith(textScaleFactor: textScaleFactor),
child: Scaffold(
appBar: AppBar(
backwardsCompatibility: false,
centerTitle: false,
title: const Text('Jumbo', style: TextStyle(fontSize: 18)),
),
@@ -1989,6 +2049,7 @@ void main() {
builder: (BuildContext context) {
return Scaffold(
appBar: AppBar(
backwardsCompatibility: false,
centerTitle: centerTitle,
title: MediaQuery(
data: MediaQuery.of(context).copyWith(textScaleFactor: textScaleFactor),
@@ -2049,6 +2110,7 @@ void main() {
home: CustomScrollView(
slivers: <Widget>[
SliverAppBar(
backwardsCompatibility: false,
title: const Text('Jumbo'),
pinned: pinned,
floating: floating,
@@ -2082,6 +2144,7 @@ void main() {
MaterialApp(
home: Scaffold(
appBar: AppBar(
backwardsCompatibility: false,
title: const Text('Title'),
toolbarHeight: 48,
),
@@ -2140,6 +2203,7 @@ void main() {
await tester.pumpWidget(MaterialApp(
home: Scaffold(
appBar: AppBar(
backwardsCompatibility: false,
leading: const Placeholder(key: key),
leadingWidth: 100,
title: const Text('Title'),
@@ -2157,6 +2221,7 @@ void main() {
home: CustomScrollView(
slivers: <Widget>[
SliverAppBar(
backwardsCompatibility: false,
leading: Placeholder(key: key),
leadingWidth: 100,
title: Text('Title'),

View File

@@ -17,6 +17,7 @@ void main() {
testWidgets('Passing no AppBarTheme returns defaults', (WidgetTester tester) async {
await tester.pumpWidget(MaterialApp(
home: Scaffold(appBar: AppBar(
backwardsCompatibility: false,
actions: <Widget>[
IconButton(icon: const Icon(Icons.share), onPressed: () { }),
],
@@ -29,7 +30,7 @@ void main() {
final RichText actionIconText = _getAppBarIconRichText(tester);
final DefaultTextStyle text = _getAppBarText(tester);
expect(SystemChrome.latestStyle!.statusBarBrightness, Brightness.dark);
expect(SystemChrome.latestStyle!.statusBarBrightness, SystemUiOverlayStyle.dark.statusBarBrightness);
expect(widget.color, Colors.blue);
expect(widget.elevation, 4.0);
expect(widget.shadowColor, Colors.black);
@@ -45,6 +46,7 @@ void main() {
await tester.pumpWidget(MaterialApp(
theme: ThemeData(appBarTheme: appBarTheme),
home: Scaffold(appBar: AppBar(
backwardsCompatibility: false,
title: const Text('App Bar Title'),
actions: <Widget>[
IconButton(icon: const Icon(Icons.share), onPressed: () { }),
@@ -59,41 +61,48 @@ void main() {
final DefaultTextStyle text = _getAppBarText(tester);
expect(SystemChrome.latestStyle!.statusBarBrightness, appBarTheme.brightness);
expect(widget.color, appBarTheme.color);
expect(widget.color, appBarTheme.backgroundColor);
expect(widget.elevation, appBarTheme.elevation);
expect(widget.shadowColor, appBarTheme.shadowColor);
expect(iconTheme.data, appBarTheme.iconTheme);
expect(actionsIconTheme.data, appBarTheme.actionsIconTheme);
expect(actionIconText.text.style!.color, appBarTheme.actionsIconTheme!.color);
expect(text.style, appBarTheme.textTheme!.bodyText2);
expect(text.style, appBarTheme.toolbarTextStyle);
});
testWidgets('AppBar widget properties take priority over theme', (WidgetTester tester) async {
const Brightness brightness = Brightness.dark;
const SystemUiOverlayStyle systemOverlayStyle = SystemUiOverlayStyle.light;
const Color color = Colors.orange;
const double elevation = 3.0;
const Color shadowColor = Colors.red;
const IconThemeData iconThemeData = IconThemeData(color: Colors.green);
const IconThemeData actionsIconThemeData = IconThemeData(color: Colors.lightBlue);
const TextTheme textTheme = TextTheme(headline6: TextStyle(color: Colors.orange), bodyText2: TextStyle(color: Colors.pink));
const TextStyle toolbarTextStyle = TextStyle(color: Colors.pink);
const TextStyle titleTextStyle = TextStyle(color: Colors.orange);
final ThemeData themeData = _themeData().copyWith(appBarTheme: _appBarTheme());
await tester.pumpWidget(MaterialApp(
theme: themeData,
home: Scaffold(appBar: AppBar(
backgroundColor: color,
brightness: brightness,
elevation: elevation,
shadowColor: shadowColor,
iconTheme: iconThemeData,
actionsIconTheme: actionsIconThemeData,
textTheme: textTheme,
actions: <Widget>[
IconButton(icon: const Icon(Icons.share), onPressed: () { }),
],
)),
));
await tester.pumpWidget(
MaterialApp(
theme: ThemeData.from(colorScheme: const ColorScheme.light()),
home: Scaffold(
appBar: AppBar(
backwardsCompatibility: false,
backgroundColor: color,
brightness: brightness,
systemOverlayStyle: systemOverlayStyle,
elevation: elevation,
shadowColor: shadowColor,
iconTheme: iconThemeData,
actionsIconTheme: actionsIconThemeData,
toolbarTextStyle: toolbarTextStyle,
titleTextStyle: titleTextStyle,
actions: <Widget>[
IconButton(icon: const Icon(Icons.share), onPressed: () { }),
],
),
),
),
);
final Material widget = _getAppBarMaterial(tester);
final IconTheme iconTheme = _getAppBarIconTheme(tester);
@@ -108,7 +117,7 @@ void main() {
expect(iconTheme.data, iconThemeData);
expect(actionsIconTheme.data, actionsIconThemeData);
expect(actionIconText.text.style!.color, actionsIconThemeData.color);
expect(text.style, textTheme.bodyText2);
expect(text.style, toolbarTextStyle);
});
testWidgets('AppBar icon color takes priority over everything', (WidgetTester tester) async {
@@ -116,11 +125,10 @@ void main() {
const IconThemeData iconThemeData = IconThemeData(color: Colors.green);
const IconThemeData actionsIconThemeData = IconThemeData(color: Colors.lightBlue);
final ThemeData themeData = _themeData().copyWith(appBarTheme: _appBarTheme());
await tester.pumpWidget(MaterialApp(
theme: themeData,
theme: ThemeData.from(colorScheme: const ColorScheme.light()),
home: Scaffold(appBar: AppBar(
backwardsCompatibility: false,
iconTheme: iconThemeData,
actionsIconTheme: actionsIconThemeData,
actions: <Widget>[
@@ -135,16 +143,21 @@ void main() {
testWidgets('AppBarTheme properties take priority over ThemeData properties', (WidgetTester tester) async {
final AppBarTheme appBarTheme = _appBarTheme();
final ThemeData themeData = _themeData().copyWith(appBarTheme: _appBarTheme());
await tester.pumpWidget(MaterialApp(
theme: themeData,
home: Scaffold(appBar: AppBar(
actions: <Widget>[
IconButton(icon: const Icon(Icons.share), onPressed: () { }),
],
)),
));
await tester.pumpWidget(
MaterialApp(
theme: ThemeData.from(colorScheme: const ColorScheme.light())
.copyWith(appBarTheme: _appBarTheme()),
home: Scaffold(
appBar: AppBar(
backwardsCompatibility: false,
actions: <Widget>[
IconButton(icon: const Icon(Icons.share), onPressed: () { }),
],
),
),
),
);
final Material widget = _getAppBarMaterial(tester);
final IconTheme iconTheme = _getAppBarIconTheme(tester);
@@ -153,48 +166,140 @@ void main() {
final DefaultTextStyle text = _getAppBarText(tester);
expect(SystemChrome.latestStyle!.statusBarBrightness, appBarTheme.brightness);
expect(widget.color, appBarTheme.color);
expect(widget.color, appBarTheme.backgroundColor);
expect(widget.elevation, appBarTheme.elevation);
expect(widget.shadowColor, appBarTheme.shadowColor);
expect(iconTheme.data, appBarTheme.iconTheme);
expect(actionsIconTheme.data, appBarTheme.actionsIconTheme);
expect(actionIconText.text.style!.color, appBarTheme.actionsIconTheme!.color);
expect(text.style, appBarTheme.textTheme!.bodyText2);
expect(text.style, appBarTheme.toolbarTextStyle);
});
testWidgets('ThemeData properties are used when no AppBarTheme is set', (WidgetTester tester) async {
final ThemeData themeData = _themeData();
testWidgets('ThemeData colorScheme is used when no AppBarTheme is set', (WidgetTester tester) async {
late ThemeData theme;
Widget buildFrame(ThemeData appTheme) {
return MaterialApp(
theme: appTheme,
home: Builder(
builder: (BuildContext context) {
// This ThemeData has been localized with ThemeData.localize. The
// appTheme parameter has not, so its textTheme is incomplete.
theme = Theme.of(context);
return Scaffold(
appBar: AppBar(
backwardsCompatibility: false,
actions: <Widget>[
IconButton(icon: const Icon(Icons.share), onPressed: () { }),
],
),
);
},
),
);
}
await tester.pumpWidget(MaterialApp(
theme: themeData,
home: Scaffold(appBar: AppBar(
actions: <Widget>[
IconButton(icon: const Icon(Icons.share), onPressed: () { }),
],
)),
));
// AppBar defaults for light themes:
// - elevation: 4
// - shadow color: black
// - background color: ColorScheme.primary
// - foreground color: ColorScheme.onPrimary
// - actions text: style bodyText2, foreground color
// - status bar brightness: dark (based on color scheme brightness)
{
await tester.pumpWidget(buildFrame(ThemeData.from(colorScheme: const ColorScheme.light())));
final Material widget = _getAppBarMaterial(tester);
final IconTheme iconTheme = _getAppBarIconTheme(tester);
final IconTheme actionsIconTheme = _getAppBarActionsIconTheme(tester);
final RichText actionIconText = _getAppBarIconRichText(tester);
final DefaultTextStyle text = _getAppBarText(tester);
final Material widget = _getAppBarMaterial(tester);
final IconTheme iconTheme = _getAppBarIconTheme(tester);
final IconTheme actionsIconTheme = _getAppBarActionsIconTheme(tester);
final RichText actionIconText = _getAppBarIconRichText(tester);
final DefaultTextStyle text = _getAppBarText(tester);
expect(SystemChrome.latestStyle!.statusBarBrightness, themeData.brightness);
expect(widget.color, themeData.primaryColor);
expect(widget.elevation, 4.0);
expect(widget.shadowColor, Colors.black);
expect(iconTheme.data, themeData.primaryIconTheme);
expect(actionsIconTheme.data, themeData.primaryIconTheme);
expect(actionIconText.text.style!.color, themeData.primaryIconTheme.color);
// 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(SystemChrome.latestStyle!.statusBarBrightness, SystemUiOverlayStyle.dark.statusBarBrightness);
expect(widget.color, theme.colorScheme.primary);
expect(widget.elevation, 4.0);
expect(widget.shadowColor, Colors.black);
expect(iconTheme.data.color, theme.colorScheme.onPrimary);
expect(actionsIconTheme.data.color, theme.colorScheme.onPrimary);
expect(actionIconText.text.style!.color, theme.colorScheme.onPrimary);
expect(text.style.compareTo(theme.textTheme.bodyText2!.copyWith(color: theme.colorScheme.onPrimary)), RenderComparison.identical);
}
// AppBar defaults for dark themes:
// - elevation: 4
// - shadow color: black
// - background color: ColorScheme.surface
// - foreground color: ColorScheme.onSurface
// - actions text: style bodyText2, foreground color
// - status bar brightness: dark (based on background color)
{
await tester.pumpWidget(buildFrame(ThemeData.from(colorScheme: const ColorScheme.dark())));
await tester.pumpAndSettle(); // Theme change animation
final Material widget = _getAppBarMaterial(tester);
final IconTheme iconTheme = _getAppBarIconTheme(tester);
final IconTheme actionsIconTheme = _getAppBarActionsIconTheme(tester);
final RichText actionIconText = _getAppBarIconRichText(tester);
final DefaultTextStyle text = _getAppBarText(tester);
expect(SystemChrome.latestStyle!.statusBarBrightness, SystemUiOverlayStyle.light.statusBarBrightness);
expect(widget.color, theme.colorScheme.surface);
expect(widget.elevation, 4.0);
expect(widget.shadowColor, Colors.black);
expect(iconTheme.data.color, theme.colorScheme.onSurface);
expect(actionsIconTheme.data.color, theme.colorScheme.onSurface);
expect(actionIconText.text.style!.color, theme.colorScheme.onSurface);
expect(text.style.compareTo(theme.textTheme.bodyText2!.copyWith(color: theme.colorScheme.onSurface)), RenderComparison.identical);
}
});
testWidgets('AppBar iconTheme with color=null defers to outer IconTheme', (WidgetTester tester) async {
// Verify claim made in https://github.com/flutter/flutter/pull/71184#issuecomment-737419215
Widget buildFrame({ Color? appIconColor, Color? appBarIconColor }) {
return MaterialApp(
theme: ThemeData.from(colorScheme: const ColorScheme.light()),
home: IconTheme(
data: IconThemeData(color: appIconColor),
child: Builder(
builder: (BuildContext context) {
return Scaffold(
appBar: AppBar(
backwardsCompatibility: false,
iconTheme: IconThemeData(color: appBarIconColor),
actions: <Widget>[
IconButton(icon: const Icon(Icons.share), onPressed: () { })
],
),
);
},
),
),
);
}
RichText getIconText() {
return tester.widget<RichText>(
find.descendant(
of: find.byType(Icon),
matching: find.byType(RichText),
),
);
}
await tester.pumpWidget(buildFrame(appIconColor: Colors.lime, appBarIconColor: null));
expect(getIconText().text.style!.color, Colors.lime);
await tester.pumpWidget(buildFrame(appIconColor: Colors.lime, appBarIconColor: Colors.purple));
expect(getIconText().text.style!.color, Colors.purple);
});
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'))),
home: Scaffold(appBar: AppBar(
title: const Text('Title'),
backwardsCompatibility: false,
)),
));
final NavigationToolbar navToolBar = tester.widget(find.byType(NavigationToolbar));
@@ -206,6 +311,7 @@ void main() {
theme: ThemeData(appBarTheme: const AppBarTheme(centerTitle: true)),
home: Scaffold(
appBar: AppBar(
backwardsCompatibility: false,
title: const Text('Title'),
centerTitle: false,
),
@@ -220,7 +326,10 @@ void main() {
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'))),
home: Scaffold(appBar: AppBar(
backwardsCompatibility: false,
title: const Text('Title')
)),
));
final NavigationToolbar navToolBar = tester.widget(find.byType(NavigationToolbar));
@@ -234,6 +343,7 @@ void main() {
theme: ThemeData(appBarTheme: const AppBarTheme(shadowColor: Colors.red)),
home: Scaffold(
appBar: AppBar(
backwardsCompatibility: false,
title: const Text('Title'),
shadowColor: Colors.yellow,
),
@@ -251,6 +361,7 @@ void main() {
theme: ThemeData(appBarTheme: const AppBarTheme(titleSpacing: kTitleSpacing)),
home: Scaffold(
appBar: AppBar(
backwardsCompatibility: false,
title: const Text('Title'),
),
),
@@ -266,6 +377,7 @@ void main() {
theme: ThemeData(appBarTheme: const AppBarTheme(titleSpacing: kTitleSpacing)),
home: Scaffold(
appBar: AppBar(
backwardsCompatibility: false,
title: const Text('Title'),
titleSpacing: 40,
),
@@ -283,6 +395,7 @@ void main() {
home: const CustomScrollView(
slivers: <Widget>[
SliverAppBar(
backwardsCompatibility: false,
title: Text('Title'),
),
],
@@ -300,6 +413,7 @@ void main() {
home: const CustomScrollView(
slivers: <Widget>[
SliverAppBar(
backwardsCompatibility: false,
title: Text('Title'),
titleSpacing: 40,
),
@@ -358,29 +472,20 @@ void main() {
AppBarTheme _appBarTheme() {
const Brightness brightness = Brightness.light;
const Color color = Colors.lightBlue;
const Color backgroundColor = Colors.lightBlue;
const double elevation = 6.0;
const Color shadowColor = Colors.red;
const IconThemeData iconThemeData = IconThemeData(color: Colors.black);
const IconThemeData actionsIconThemeData = IconThemeData(color: Colors.pink);
const TextTheme textTheme = TextTheme(bodyText2: TextStyle(color: Colors.yellow));
return const AppBarTheme(
actionsIconTheme: actionsIconThemeData,
brightness: brightness,
color: color,
backgroundColor: backgroundColor,
elevation: elevation,
shadowColor: shadowColor,
iconTheme: iconThemeData,
textTheme: textTheme,
);
}
ThemeData _themeData() {
return ThemeData(
primaryColor: Colors.purple,
brightness: Brightness.dark,
primaryIconTheme: const IconThemeData(color: Colors.green),
primaryTextTheme: const TextTheme(headline6: TextStyle(color: Colors.orange), bodyText2: TextStyle(color: Colors.pink)),
toolbarTextStyle: TextStyle(color: Colors.yellow),
titleTextStyle: TextStyle(color: Colors.pink),
);
}