Description
Steps to Reproduce
When defining foregroundColor
for a SliverAppBar.large
or SliverAppBar.medium
either via an AppBarTheme
or their properties, the defined color is not used by the app bar.
Using AppBarTheme to set colors
Expected SliverAppBar.medium
and SliverAppBar.large
title and icon colors to look like they do on AppBar
and SliverAppBar
. The background color does, but the defined AppBarTheme.foregroundColor
is not used by the title.
M2 Light theme | M2 dark theme |
---|---|
![]() |
![]() |
M3 Light theme | M3 dark theme |
---|---|
![]() |
![]() |
Using SliverAppBar properties to set colors
Expected SliverAppBar.medium
and SliverAppBar.large
title and icon colors to look like they do on AppBar
and SliverAppBar
. The background color does, but the defined foregroundColor
for SliverAppBar.medium
and SliverAppBar.large
is not used by the title.
M2 Light theme | M2 dark theme |
---|---|
![]() |
![]() |
M3 Light theme | M3 dark theme |
---|---|
![]() |
![]() |
Cause of Issue
This issue was mentioned in PR comment that implemented the SliverAppBar.medium
and SliverAppBar.large
here: #103962 (comment)
It was discovered that that text style for _MediumScrollUnderFlexibleConfig
and _LargeScrollUnderFlexibleConfig
do not respect and fall through via widget properties or AppBarTheme.of(context)
. They now do this:
@override
TextStyle? get collapsedTextStyle =>
_textTheme.titleLarge?.apply(color: _colors.onSurface);
@override
TextStyle? get expandedTextStyle =>
_textTheme.headlineSmall?.apply(color: _colors.onSurface);
It would be expected that they would fall through via widget foregroundColor
and AppBarTheme.of(context).foregroundColor
before resorting to default value, in order to respect and use widget and themed AppBar
foreground color, just like the vanilla AppBar
and SliverAppBar
does.
Additionally, correct default styles for M2 would be a nice addition in order to keep the new SliverAppBar
inline with previous behavior, also when using Material 2.
Issue Demo App
For convenience the issue demo app is available in DartPad here: https://dartpad.dev/?id=78c1fa0d845892e1cf8ea8b58adbe576
The live DartPad example uses Flutter stable 3.3.0, but the issue and results were also verified and are the same Flutter master 3.4.0-19.0.pre.70.
Issue reproduction sample code
The issue reproduction code is also available in this GIST
// MIT License
//
// Copyright (c) 2022 Mike Rydstrom
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import 'package:flutter/material.dart';
// Used as M3 seed color
const Color seedColor = Color(0xFF386A20);
// Make a seed generated M3 light mode ColorScheme.
final ColorScheme lightScheme = ColorScheme.fromSeed(
brightness: Brightness.light,
seedColor: seedColor,
);
// Make a seed generated M3 dark mode ColorScheme.
final ColorScheme darkScheme = ColorScheme.fromSeed(
brightness: Brightness.dark,
seedColor: seedColor,
);
// AppBar colors set via widget properties
const Color appBarBackground = Color(0xFFE9EEB5);
const Color appBarForeground = Color(0xFF1489C0);
// Make AppBarTheme with custom foreground and background colors.
AppBarTheme appBarTheme({required ColorScheme colorScheme}) => AppBarTheme(
backgroundColor: colorScheme.tertiaryContainer,
foregroundColor: colorScheme.error,
);
// A simple custom theme
ThemeData appTheme(Brightness mode, bool useMaterial3) => ThemeData.from(
colorScheme: mode == Brightness.light ? lightScheme : darkScheme,
useMaterial3: useMaterial3,
).copyWith(
appBarTheme: appBarTheme(
colorScheme: mode == Brightness.light ? lightScheme : darkScheme,
),
);
void main() {
runApp(const IssueDemoApp());
}
class IssueDemoApp extends StatefulWidget {
const IssueDemoApp({super.key});
@override
State<IssueDemoApp> createState() => _IssueDemoAppState();
}
class _IssueDemoAppState extends State<IssueDemoApp> {
bool useMaterial3 = true;
ThemeMode themeMode = ThemeMode.light;
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
themeMode: themeMode,
theme: appTheme(Brightness.light, useMaterial3),
darkTheme: appTheme(Brightness.dark, useMaterial3),
home: Scaffold(
appBar: AppBar(
title: const Text(('AppBar Issue Demo')),
actions: [
IconButton(
icon: useMaterial3
? const Icon(Icons.filter_3)
: const Icon(Icons.filter_2),
onPressed: () {
setState(() {
useMaterial3 = !useMaterial3;
});
},
tooltip: 'Switch to Material ${useMaterial3 ? 2 : 3}',
),
IconButton(
icon: themeMode == ThemeMode.dark
? const Icon(Icons.wb_sunny_outlined)
: const Icon(Icons.wb_sunny),
onPressed: () {
setState(() {
if (themeMode == ThemeMode.light) {
themeMode = ThemeMode.dark;
} else {
themeMode = ThemeMode.light;
}
});
},
tooltip: "Toggle brightness",
),
],
),
body: const HomePage(),
),
);
}
}
class HomePage extends StatelessWidget {
const HomePage({
super.key,
});
@override
Widget build(BuildContext context) {
return ListView(
padding: const EdgeInsets.symmetric(horizontal: 16),
children: [
const SizedBox(height: 8),
Text(
'AppBar Issue',
style: Theme.of(context).textTheme.headlineSmall,
),
Text(
'Foreground color on SliverAppBar.medium and SliverAppBar.large are '
'not respected via AppBarTheme or AppBar properties.',
style: Theme.of(context).textTheme.bodyLarge,
),
const Divider(),
const ShowAppBarColors(),
const SizedBox(height: 8),
const AppBarShowcase(),
const Divider(),
const ShowAppBarColors(
foregroundColor: appBarForeground,
backgroundColor: appBarBackground,
),
const SizedBox(height: 8),
const AppBarShowcase(
foregroundColor: appBarForeground,
backgroundColor: appBarBackground,
),
],
);
}
}
class AppBarShowcase extends StatelessWidget {
const AppBarShowcase({super.key, this.foregroundColor, this.backgroundColor});
final Color? foregroundColor;
final Color? backgroundColor;
@override
Widget build(BuildContext context) {
return MediaQuery.removePadding(
context: context,
removeBottom: true,
removeTop: true,
child: Column(
children: <Widget>[
AppBar(
foregroundColor: foregroundColor,
backgroundColor: backgroundColor,
leading: IconButton(
icon: const Icon(Icons.menu),
onPressed: () {},
),
title: const Text('Normal AppBar'),
actions: <Widget>[
IconButton(
icon: const Icon(Icons.search),
onPressed: () {},
),
],
),
const SizedBox(height: 8),
CustomScrollView(
// Normally avoid shrinkWrap, but for showing a few demo
// widgets here, we can get away with it.
shrinkWrap: true,
slivers: <Widget>[
SliverAppBar(
foregroundColor: foregroundColor,
backgroundColor: backgroundColor,
leading: IconButton(
icon: const Icon(Icons.menu),
onPressed: () {},
),
title: const Text('Sliver AppBar'),
actions: <Widget>[
IconButton(
icon: const Icon(Icons.search),
onPressed: () {},
),
],
),
const SliverToBoxAdapter(child: SizedBox(height: 8)),
SliverAppBar.medium(
foregroundColor: foregroundColor,
backgroundColor: backgroundColor,
leading: IconButton(
icon: const Icon(Icons.menu),
onPressed: () {},
),
title: const Text('SliverAppBar.medium'),
actions: <Widget>[
IconButton(
icon: const Icon(Icons.search),
onPressed: () {},
),
],
),
const SliverToBoxAdapter(child: SizedBox(height: 8)),
SliverAppBar.large(
foregroundColor: foregroundColor,
backgroundColor: backgroundColor,
leading: IconButton(
icon: const Icon(Icons.menu),
onPressed: () {},
),
title: const Text('SliverAppBar.large'),
actions: <Widget>[
IconButton(
icon: const Icon(Icons.search),
onPressed: () {},
),
],
)
],
),
],
),
);
}
}
/// Draw a number of boxes showing the colors of key theme color properties
/// in the ColorScheme of the inherited ThemeData and its color properties.
class ShowAppBarColors extends StatelessWidget {
const ShowAppBarColors({
super.key,
this.foregroundColor,
this.backgroundColor,
});
final Color? foregroundColor;
final Color? backgroundColor;
@override
Widget build(BuildContext context) {
final ThemeData theme = Theme.of(context);
final bool useMaterial3 = theme.useMaterial3;
const double spacing = 6;
final String colorSource =
foregroundColor == null ? ' via AppBarTheme' : ' via AppBar properties';
// Grab the card border from the theme card shape
ShapeBorder? border = theme.cardTheme.shape;
// If we had one, copy in a border side to it.
if (border is RoundedRectangleBorder) {
border = border.copyWith(
side: BorderSide(
color: theme.dividerColor,
width: 1,
),
);
// If
} else {
// If border was null, make one matching Card default, but with border
// side, if it was not null, we leave it as it was.
border ??= RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(useMaterial3 ? 12 : 4)),
side: BorderSide(
color: theme.dividerColor,
width: 1,
),
);
}
// Wrap this widget branch in a custom theme where card has a border outline
// if it did not have one, but retains in ambient themed border radius.
return Theme(
data: Theme.of(context).copyWith(
cardTheme: CardTheme.of(context).copyWith(
elevation: 0,
shape: border,
),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: Text(
'AppBar Colors$colorSource',
style: theme.textTheme.titleLarge,
),
),
Wrap(
alignment: WrapAlignment.start,
crossAxisAlignment: WrapCrossAlignment.center,
spacing: spacing,
runSpacing: spacing,
children: <Widget>[
ColorCard(
label: 'AppBar\nBackground',
color: backgroundColor ?? theme.appBarTheme.backgroundColor!,
textColor:
foregroundColor ?? theme.appBarTheme.foregroundColor!,
),
ColorCard(
label: 'AppBar\nForeground',
color: foregroundColor ?? theme.appBarTheme.foregroundColor!,
textColor:
backgroundColor ?? theme.appBarTheme.backgroundColor!,
),
],
),
],
),
);
}
}
/// A [SizedBox] with a [Card] and string text in it. Used in this demo to
/// display theme color boxes.
///
/// Can specify label text color and background color.
class ColorCard extends StatelessWidget {
const ColorCard({
super.key,
required this.label,
required this.color,
required this.textColor,
this.size,
});
final String label;
final Color color;
final Color textColor;
final Size? size;
@override
Widget build(BuildContext context) {
const double fontSize = 11;
const Size effectiveSize = Size(86, 58);
return SizedBox(
width: effectiveSize.width,
height: effectiveSize.height,
child: Card(
margin: EdgeInsets.zero,
clipBehavior: Clip.antiAlias,
color: color,
child: Center(
child: Text(
label,
style: TextStyle(color: textColor, fontSize: fontSize),
textAlign: TextAlign.center,
),
),
),
);
}
}
Flutter doctor
flutter doctor -v
[✓] Flutter (Channel master, 3.4.0-19.0.pre.70, on macOS 12.5.1 21G83 darwin-arm64, locale en-US)
• Flutter version 3.4.0-19.0.pre.70 on channel master at /Users/rydmike/fvm/versions/master
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 798ce226de (16 minutes ago), 2022-09-04 14:25:25 -0400
• Engine revision 0a2f56cd02
• Dart version 2.19.0 (build 2.19.0-168.0.dev)
• DevTools version 2.17.0
[✓] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
• Android SDK at /Users/rydmike/Library/Android/sdk
• Platform android-32, build-tools 32.1.0-rc1
• Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 13.4)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Build 13F17a
• CocoaPods version 1.11.3
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 2021.2)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
[✓] IntelliJ IDEA Community Edition (version 2022.1.1)
• IntelliJ at /Applications/IntelliJ IDEA CE.app
• Flutter plugin version 67.1.4
• Dart plugin version 221.5591.58
[✓] VS Code (version 1.70.2)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.46.0
[✓] Connected device (2 available)
• macOS (desktop) • macos • darwin-arm64 • macOS 12.5.1 21G83 darwin-arm64
• Chrome (web) • chrome • web-javascript • Google Chrome 104.0.5112.101
[✓] HTTP Host Availability
• All required HTTP hosts are available
• No issues found!
Metadata
Metadata
Assignees
Labels
Type
Projects
Status