Commit b1b47435 authored by Adam Barth's avatar Adam Barth

Merge pull request #1798 from abarth/inherit_text_style

Be explicit about whether text styles inherit
parents 4da5aea5 7a811e5a
...@@ -16,30 +16,30 @@ import 'colors.dart'; ...@@ -16,30 +16,30 @@ import 'colors.dart';
class TextTheme { class TextTheme {
const TextTheme._black() const TextTheme._black()
: display4 = const TextStyle(fontSize: 112.0, fontWeight: FontWeight.w100, color: Colors.black54, textBaseline: TextBaseline.alphabetic), : display4 = const TextStyle(inherit: false, fontSize: 112.0, fontWeight: FontWeight.w100, color: Colors.black54, textBaseline: TextBaseline.alphabetic),
display3 = const TextStyle(fontSize: 56.0, fontWeight: FontWeight.w400, color: Colors.black54, textBaseline: TextBaseline.alphabetic), display3 = const TextStyle(inherit: false, fontSize: 56.0, fontWeight: FontWeight.w400, color: Colors.black54, textBaseline: TextBaseline.alphabetic),
display2 = const TextStyle(fontSize: 45.0, fontWeight: FontWeight.w400, color: Colors.black54, height: 48.0 / 45.0, textBaseline: TextBaseline.alphabetic), display2 = const TextStyle(inherit: false, fontSize: 45.0, fontWeight: FontWeight.w400, color: Colors.black54, height: 48.0 / 45.0, textBaseline: TextBaseline.alphabetic),
display1 = const TextStyle(fontSize: 34.0, fontWeight: FontWeight.w400, color: Colors.black54, height: 40.0 / 34.0, textBaseline: TextBaseline.alphabetic), display1 = const TextStyle(inherit: false, fontSize: 34.0, fontWeight: FontWeight.w400, color: Colors.black54, height: 40.0 / 34.0, textBaseline: TextBaseline.alphabetic),
headline = const TextStyle(fontSize: 24.0, fontWeight: FontWeight.w400, color: Colors.black87, height: 32.0 / 24.0, textBaseline: TextBaseline.alphabetic), headline = const TextStyle(inherit: false, fontSize: 24.0, fontWeight: FontWeight.w400, color: Colors.black87, height: 32.0 / 24.0, textBaseline: TextBaseline.alphabetic),
title = const TextStyle(fontSize: 20.0, fontWeight: FontWeight.w500, color: Colors.black87, height: 28.0 / 20.0, textBaseline: TextBaseline.alphabetic), title = const TextStyle(inherit: false, fontSize: 20.0, fontWeight: FontWeight.w500, color: Colors.black87, height: 28.0 / 20.0, textBaseline: TextBaseline.alphabetic),
subhead = const TextStyle(fontSize: 16.0, fontWeight: FontWeight.w400, color: Colors.black87, height: 24.0 / 16.0, textBaseline: TextBaseline.alphabetic), subhead = const TextStyle(inherit: false, fontSize: 16.0, fontWeight: FontWeight.w400, color: Colors.black87, height: 24.0 / 16.0, textBaseline: TextBaseline.alphabetic),
body2 = const TextStyle(fontSize: 14.0, fontWeight: FontWeight.w500, color: Colors.black87, height: 24.0 / 14.0, textBaseline: TextBaseline.alphabetic), body2 = const TextStyle(inherit: false, fontSize: 14.0, fontWeight: FontWeight.w500, color: Colors.black87, height: 24.0 / 14.0, textBaseline: TextBaseline.alphabetic),
body1 = const TextStyle(fontSize: 14.0, fontWeight: FontWeight.w400, color: Colors.black87, height: 20.0 / 14.0, textBaseline: TextBaseline.alphabetic), body1 = const TextStyle(inherit: false, fontSize: 14.0, fontWeight: FontWeight.w400, color: Colors.black87, height: 20.0 / 14.0, textBaseline: TextBaseline.alphabetic),
caption = const TextStyle(fontSize: 12.0, fontWeight: FontWeight.w400, color: Colors.black54, textBaseline: TextBaseline.alphabetic), caption = const TextStyle(inherit: false, fontSize: 12.0, fontWeight: FontWeight.w400, color: Colors.black54, textBaseline: TextBaseline.alphabetic),
button = const TextStyle(fontSize: 14.0, fontWeight: FontWeight.w500, color: Colors.black87, textBaseline: TextBaseline.alphabetic); button = const TextStyle(inherit: false, fontSize: 14.0, fontWeight: FontWeight.w500, color: Colors.black87, textBaseline: TextBaseline.alphabetic);
const TextTheme._white() const TextTheme._white()
: display4 = const TextStyle(fontSize: 112.0, fontWeight: FontWeight.w100, color: Colors.white70, textBaseline: TextBaseline.alphabetic), : display4 = const TextStyle(inherit: false, fontSize: 112.0, fontWeight: FontWeight.w100, color: Colors.white70, textBaseline: TextBaseline.alphabetic),
display3 = const TextStyle(fontSize: 56.0, fontWeight: FontWeight.w400, color: Colors.white70, textBaseline: TextBaseline.alphabetic), display3 = const TextStyle(inherit: false, fontSize: 56.0, fontWeight: FontWeight.w400, color: Colors.white70, textBaseline: TextBaseline.alphabetic),
display2 = const TextStyle(fontSize: 45.0, fontWeight: FontWeight.w400, color: Colors.white70, height: 48.0 / 45.0, textBaseline: TextBaseline.alphabetic), display2 = const TextStyle(inherit: false, fontSize: 45.0, fontWeight: FontWeight.w400, color: Colors.white70, height: 48.0 / 45.0, textBaseline: TextBaseline.alphabetic),
display1 = const TextStyle(fontSize: 34.0, fontWeight: FontWeight.w400, color: Colors.white70, height: 40.0 / 34.0, textBaseline: TextBaseline.alphabetic), display1 = const TextStyle(inherit: false, fontSize: 34.0, fontWeight: FontWeight.w400, color: Colors.white70, height: 40.0 / 34.0, textBaseline: TextBaseline.alphabetic),
headline = const TextStyle(fontSize: 24.0, fontWeight: FontWeight.w400, color: Colors.white, height: 32.0 / 24.0, textBaseline: TextBaseline.alphabetic), headline = const TextStyle(inherit: false, fontSize: 24.0, fontWeight: FontWeight.w400, color: Colors.white, height: 32.0 / 24.0, textBaseline: TextBaseline.alphabetic),
title = const TextStyle(fontSize: 20.0, fontWeight: FontWeight.w500, color: Colors.white, height: 28.0 / 20.0, textBaseline: TextBaseline.alphabetic), title = const TextStyle(inherit: false, fontSize: 20.0, fontWeight: FontWeight.w500, color: Colors.white, height: 28.0 / 20.0, textBaseline: TextBaseline.alphabetic),
subhead = const TextStyle(fontSize: 16.0, fontWeight: FontWeight.w400, color: Colors.white, height: 24.0 / 16.0, textBaseline: TextBaseline.alphabetic), subhead = const TextStyle(inherit: false, fontSize: 16.0, fontWeight: FontWeight.w400, color: Colors.white, height: 24.0 / 16.0, textBaseline: TextBaseline.alphabetic),
body2 = const TextStyle(fontSize: 14.0, fontWeight: FontWeight.w500, color: Colors.white, height: 24.0 / 14.0, textBaseline: TextBaseline.alphabetic), body2 = const TextStyle(inherit: false, fontSize: 14.0, fontWeight: FontWeight.w500, color: Colors.white, height: 24.0 / 14.0, textBaseline: TextBaseline.alphabetic),
body1 = const TextStyle(fontSize: 14.0, fontWeight: FontWeight.w400, color: Colors.white, height: 20.0 / 14.0, textBaseline: TextBaseline.alphabetic), body1 = const TextStyle(inherit: false, fontSize: 14.0, fontWeight: FontWeight.w400, color: Colors.white, height: 20.0 / 14.0, textBaseline: TextBaseline.alphabetic),
caption = const TextStyle(fontSize: 12.0, fontWeight: FontWeight.w400, color: Colors.white70, textBaseline: TextBaseline.alphabetic), caption = const TextStyle(inherit: false, fontSize: 12.0, fontWeight: FontWeight.w400, color: Colors.white70, textBaseline: TextBaseline.alphabetic),
button = const TextStyle(fontSize: 14.0, fontWeight: FontWeight.w500, color: Colors.white, textBaseline: TextBaseline.alphabetic); button = const TextStyle(inherit: false, fontSize: 14.0, fontWeight: FontWeight.w500, color: Colors.white, textBaseline: TextBaseline.alphabetic);
final TextStyle display4; final TextStyle display4;
final TextStyle display3; final TextStyle display3;
......
...@@ -24,6 +24,7 @@ const lineThrough = const <TextDecoration>[TextDecoration.lineThrough]; ...@@ -24,6 +24,7 @@ const lineThrough = const <TextDecoration>[TextDecoration.lineThrough];
/// An immutable style in which paint text /// An immutable style in which paint text
class TextStyle { class TextStyle {
const TextStyle({ const TextStyle({
this.inherit: true,
this.color, this.color,
this.fontFamily, this.fontFamily,
this.fontSize, this.fontSize,
...@@ -37,6 +38,9 @@ class TextStyle { ...@@ -37,6 +38,9 @@ class TextStyle {
this.decorationStyle this.decorationStyle
}); });
/// Whether null values are replaced with their value in an ancestor text style.
final bool inherit;
/// The color to use when painting the text /// The color to use when painting the text
final Color color; final Color color;
...@@ -87,6 +91,7 @@ class TextStyle { ...@@ -87,6 +91,7 @@ class TextStyle {
TextDecorationStyle decorationStyle TextDecorationStyle decorationStyle
}) { }) {
return new TextStyle( return new TextStyle(
inherit: inherit,
color: color != null ? color : this.color, color: color != null ? color : this.color,
fontFamily: fontFamily != null ? fontFamily : this.fontFamily, fontFamily: fontFamily != null ? fontFamily : this.fontFamily,
fontSize: fontSize != null ? fontSize : this.fontSize, fontSize: fontSize != null ? fontSize : this.fontSize,
...@@ -102,8 +107,12 @@ class TextStyle { ...@@ -102,8 +107,12 @@ class TextStyle {
} }
/// Returns a new text style that matches this text style but with some values /// Returns a new text style that matches this text style but with some values
/// replaced by the non-null parameters of the given text style /// replaced by the non-null parameters of the given text style. If the given
/// text style is null, simply returns this text style.
TextStyle merge(TextStyle other) { TextStyle merge(TextStyle other) {
if (other == null)
return this;
assert(other.inherit);
return copyWith( return copyWith(
color: other.color, color: other.color,
fontFamily: other.fontFamily, fontFamily: other.fontFamily,
...@@ -146,7 +155,8 @@ class TextStyle { ...@@ -146,7 +155,8 @@ class TextStyle {
if (other is! TextStyle) if (other is! TextStyle)
return false; return false;
final TextStyle typedOther = other; final TextStyle typedOther = other;
return color == typedOther.color && return inherit == typedOther.inherit &&
color == typedOther.color &&
fontFamily == typedOther.fontFamily && fontFamily == typedOther.fontFamily &&
fontSize == typedOther.fontSize && fontSize == typedOther.fontSize &&
fontWeight == typedOther.fontWeight && fontWeight == typedOther.fontWeight &&
...@@ -161,6 +171,7 @@ class TextStyle { ...@@ -161,6 +171,7 @@ class TextStyle {
int get hashCode { int get hashCode {
// Use Quiver: https://github.com/domokit/mojo/issues/236 // Use Quiver: https://github.com/domokit/mojo/issues/236
int value = 373; int value = 373;
value = 37 * value + inherit.hashCode;
value = 37 * value + color.hashCode; value = 37 * value + color.hashCode;
value = 37 * value + fontFamily.hashCode; value = 37 * value + fontFamily.hashCode;
value = 37 * value + fontSize.hashCode; value = 37 * value + fontSize.hashCode;
...@@ -176,6 +187,7 @@ class TextStyle { ...@@ -176,6 +187,7 @@ class TextStyle {
String toString([String prefix = '']) { String toString([String prefix = '']) {
List<String> result = <String>[]; List<String> result = <String>[];
result.add('${prefix}inhert: $inherit');
if (color != null) if (color != null)
result.add('${prefix}color: $color'); result.add('${prefix}color: $color');
if (fontFamily != null) if (fontFamily != null)
......
...@@ -857,13 +857,9 @@ class Text extends StatelessComponent { ...@@ -857,13 +857,9 @@ class Text extends StatelessComponent {
Widget build(BuildContext context) { Widget build(BuildContext context) {
TextSpan text = new PlainTextSpan(data); TextSpan text = new PlainTextSpan(data);
TextStyle defaultStyle = DefaultTextStyle.of(context);
TextStyle combinedStyle; TextStyle combinedStyle;
if (defaultStyle != null) { if (style == null || style.inherit) {
if (style != null) combinedStyle = DefaultTextStyle.of(context)?.merge(style) ?? style;
combinedStyle = defaultStyle.merge(style);
else
combinedStyle = defaultStyle;
} else { } else {
combinedStyle = style; combinedStyle = style;
} }
......
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