Commit 55f9145e authored by Adam Barth's avatar Adam Barth

Rename DefaultTextStyle constructor to explicit (#3920)

To make it clear that this constructor requires an explicit style. Also
throw a descriptive error recommending the inherit constructor for
styles with the inherit bit set.

Fixes #3842
parent 707fe582
...@@ -97,7 +97,7 @@ class ExampleDragSource extends StatelessWidget { ...@@ -97,7 +97,7 @@ class ExampleDragSource extends StatelessWidget {
if (heavy) if (heavy)
size *= kHeavyMultiplier; size *= kHeavyMultiplier;
Widget contents = new DefaultTextStyle( Widget contents = new DefaultTextStyle.explicit(
style: Theme.of(context).textTheme.body1, style: Theme.of(context).textTheme.body1,
textAlign: TextAlign.center, textAlign: TextAlign.center,
child: new Dot( child: new Dot(
...@@ -185,7 +185,7 @@ class MovableBall extends StatelessWidget { ...@@ -185,7 +185,7 @@ class MovableBall extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
Widget ball = new DefaultTextStyle( Widget ball = new DefaultTextStyle.explicit(
style: Typography.white.body1, style: Typography.white.body1,
textAlign: TextAlign.center, textAlign: TextAlign.center,
child: new Dot( child: new Dot(
......
...@@ -84,7 +84,7 @@ class ColorSwatchTabView extends StatelessWidget { ...@@ -84,7 +84,7 @@ class ColorSwatchTabView extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
List<Widget> colorItems = swatch.colors.keys.map((int index) { List<Widget> colorItems = swatch.colors.keys.map((int index) {
return new DefaultTextStyle( return new DefaultTextStyle.explicit(
style: index > swatch.threshold ? whiteTextStyle : blackTextStyle, style: index > swatch.threshold ? whiteTextStyle : blackTextStyle,
child: new ColorItem(index: index, color: swatch.colors[index]) child: new ColorItem(index: index, color: swatch.colors[index])
); );
...@@ -93,7 +93,7 @@ class ColorSwatchTabView extends StatelessWidget { ...@@ -93,7 +93,7 @@ class ColorSwatchTabView extends StatelessWidget {
if (swatch.accentColors != null) { if (swatch.accentColors != null) {
colorItems.addAll(swatch.accentColors.keys.map((int index) { colorItems.addAll(swatch.accentColors.keys.map((int index) {
return new DefaultTextStyle( return new DefaultTextStyle.explicit(
style: index > swatch.threshold ? whiteTextStyle : blackTextStyle, style: index > swatch.threshold ? whiteTextStyle : blackTextStyle,
child: new ColorItem(index: index, color: swatch.accentColors[index], prefix: 'A') child: new ColorItem(index: index, color: swatch.accentColors[index], prefix: 'A')
); );
......
...@@ -18,7 +18,7 @@ class _ContactCategory extends StatelessWidget { ...@@ -18,7 +18,7 @@ class _ContactCategory extends StatelessWidget {
decoration: new BoxDecoration( decoration: new BoxDecoration(
border: new Border(bottom: new BorderSide(color: Theme.of(context).dividerColor)) border: new Border(bottom: new BorderSide(color: Theme.of(context).dividerColor))
), ),
child: new DefaultTextStyle( child: new DefaultTextStyle.explicit(
style: Theme.of(context).textTheme.subhead, style: Theme.of(context).textTheme.subhead,
child: new Row( child: new Row(
children: <Widget>[ children: <Widget>[
......
...@@ -31,7 +31,7 @@ class DateTimeItem extends StatelessWidget { ...@@ -31,7 +31,7 @@ class DateTimeItem extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
final ThemeData theme = Theme.of(context); final ThemeData theme = Theme.of(context);
return new DefaultTextStyle( return new DefaultTextStyle.explicit(
style: theme.textTheme.subhead, style: theme.textTheme.subhead,
child: new Row( child: new Row(
children: <Widget>[ children: <Widget>[
......
...@@ -94,7 +94,7 @@ class _ProgressIndicatorDemoState extends State<ProgressIndicatorDemo> { ...@@ -94,7 +94,7 @@ class _ProgressIndicatorDemoState extends State<ProgressIndicatorDemo> {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return new Scaffold( return new Scaffold(
appBar: new AppBar(title: new Text('Progress indicators')), appBar: new AppBar(title: new Text('Progress indicators')),
body: new DefaultTextStyle( body: new DefaultTextStyle.explicit(
style: Theme.of(context).textTheme.title, style: Theme.of(context).textTheme.title,
child: new GestureDetector( child: new GestureDetector(
onTap: _handleTap, onTap: _handleTap,
......
...@@ -26,8 +26,8 @@ class _BarGraphic extends StatelessWidget { ...@@ -26,8 +26,8 @@ class _BarGraphic extends StatelessWidget {
width: 200.0, width: 200.0,
padding: const EdgeInsets.symmetric(horizontal: 16.0), padding: const EdgeInsets.symmetric(horizontal: 16.0),
decoration: new BoxDecoration(backgroundColor: color), decoration: new BoxDecoration(backgroundColor: color),
child: new DefaultTextStyle( child: new DefaultTextStyle.explicit(
style: Theme.of(context).textTheme.body1.copyWith(color: Colors.white), style: Typography.white.body1,
child: new Row( child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween, mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[ children: <Widget>[
......
...@@ -203,7 +203,7 @@ class AppBar extends StatelessWidget { ...@@ -203,7 +203,7 @@ class AppBar extends StatelessWidget {
child: new Padding( child: new Padding(
padding: new EdgeInsets.only(left: 8.0), padding: new EdgeInsets.only(left: 8.0),
child: title != null ? child: title != null ?
new DefaultTextStyle( new DefaultTextStyle.explicit(
style: centerStyle, style: centerStyle,
softWrap: false, softWrap: false,
overflow: TextOverflow.ellipsis, overflow: TextOverflow.ellipsis,
...@@ -218,7 +218,7 @@ class AppBar extends StatelessWidget { ...@@ -218,7 +218,7 @@ class AppBar extends StatelessWidget {
height: kToolBarHeight, height: kToolBarHeight,
child: new IconTheme( child: new IconTheme(
data: iconTheme, data: iconTheme,
child: new DefaultTextStyle( child: new DefaultTextStyle.explicit(
style: sideStyle, style: sideStyle,
child: new Row(children: toolBarRow) child: new Row(children: toolBarRow)
) )
......
...@@ -82,7 +82,7 @@ class Chip extends StatelessWidget { ...@@ -82,7 +82,7 @@ class Chip extends StatelessWidget {
)); ));
} }
children.add(new DefaultTextStyle( children.add(new DefaultTextStyle.explicit(
style: _kLabelStyle, style: _kLabelStyle,
child: label child: label
)); ));
......
...@@ -52,7 +52,7 @@ class CircleAvatar extends StatelessWidget { ...@@ -52,7 +52,7 @@ class CircleAvatar extends StatelessWidget {
shape: BoxShape.circle shape: BoxShape.circle
), ),
child: new Center( child: new Center(
child: new DefaultTextStyle( child: new DefaultTextStyle.explicit(
style: theme.primaryTextTheme.title, style: theme.primaryTextTheme.title,
child: child child: child
) )
......
...@@ -403,6 +403,7 @@ class DataTable extends StatelessWidget { ...@@ -403,6 +403,7 @@ class DataTable extends StatelessWidget {
} }
Widget _buildDataCell({ Widget _buildDataCell({
BuildContext context,
EdgeInsets padding, EdgeInsets padding,
Widget label, Widget label,
bool numeric, bool numeric,
...@@ -421,7 +422,8 @@ class DataTable extends StatelessWidget { ...@@ -421,7 +422,8 @@ class DataTable extends StatelessWidget {
height: _kDataRowHeight, height: _kDataRowHeight,
child: new Align( child: new Align(
alignment: new FractionalOffset(numeric ? 1.0 : 0.0, 0.5), // TODO(ianh): RTL for non-numeric alignment: new FractionalOffset(numeric ? 1.0 : 0.0, 0.5), // TODO(ianh): RTL for non-numeric
child: new DefaultTextStyle( child: new DefaultTextStyle.inherit(
context: context,
style: new TextStyle( style: new TextStyle(
// TODO(ianh): font family should be Roboto; see https://github.com/flutter/flutter/issues/3116 // TODO(ianh): font family should be Roboto; see https://github.com/flutter/flutter/issues/3116
fontSize: 13.0, fontSize: 13.0,
...@@ -528,6 +530,7 @@ class DataTable extends StatelessWidget { ...@@ -528,6 +530,7 @@ class DataTable extends StatelessWidget {
for (DataRow row in rows) { for (DataRow row in rows) {
DataCell cell = row.cells[dataColumnIndex]; DataCell cell = row.cells[dataColumnIndex];
tableRows[rowIndex].children[displayColumnIndex] = _buildDataCell( tableRows[rowIndex].children[displayColumnIndex] = _buildDataCell(
context: context,
padding: padding, padding: padding,
label: cell.widget, label: cell.widget,
numeric: column.numeric, numeric: column.numeric,
......
...@@ -71,7 +71,7 @@ class Dialog extends StatelessWidget { ...@@ -71,7 +71,7 @@ class Dialog extends StatelessWidget {
padding = new EdgeInsets.fromLTRB(24.0, 24.0, 24.0, content == null ? 20.0 : 0.0); padding = new EdgeInsets.fromLTRB(24.0, 24.0, 24.0, content == null ? 20.0 : 0.0);
dialogBody.add(new Padding( dialogBody.add(new Padding(
padding: padding, padding: padding,
child: new DefaultTextStyle( child: new DefaultTextStyle.explicit(
style: Theme.of(context).textTheme.title, style: Theme.of(context).textTheme.title,
child: title child: title
) )
...@@ -84,7 +84,7 @@ class Dialog extends StatelessWidget { ...@@ -84,7 +84,7 @@ class Dialog extends StatelessWidget {
padding = const EdgeInsets.fromLTRB(24.0, 20.0, 24.0, 24.0); padding = const EdgeInsets.fromLTRB(24.0, 20.0, 24.0, 24.0);
dialogBody.add(new Padding( dialogBody.add(new Padding(
padding: padding, padding: padding,
child: new DefaultTextStyle( child: new DefaultTextStyle.explicit(
style: Theme.of(context).textTheme.subhead, style: Theme.of(context).textTheme.subhead,
child: content child: content
) )
......
...@@ -53,7 +53,7 @@ class DrawerHeader extends StatelessWidget { ...@@ -53,7 +53,7 @@ class DrawerHeader extends StatelessWidget {
new Flexible(child: new Container()), new Flexible(child: new Container()),
new Container( new Container(
padding: const EdgeInsets.symmetric(horizontal: 16.0), padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: new DefaultTextStyle( child: new DefaultTextStyle.explicit(
style: Theme.of(context).textTheme.body2, style: Theme.of(context).textTheme.body2,
child: child child: child
) )
......
...@@ -277,7 +277,7 @@ class DropDownMenuItem<T> extends StatelessWidget { ...@@ -277,7 +277,7 @@ class DropDownMenuItem<T> extends StatelessWidget {
return new Container( return new Container(
height: _kMenuItemHeight, height: _kMenuItemHeight,
padding: const EdgeInsets.symmetric(horizontal: 8.0), padding: const EdgeInsets.symmetric(horizontal: 8.0),
child: new DefaultTextStyle( child: new DefaultTextStyle.explicit(
style: Theme.of(context).textTheme.subhead, style: Theme.of(context).textTheme.subhead,
child: new Baseline( child: new Baseline(
baselineType: TextBaseline.alphabetic, baselineType: TextBaseline.alphabetic,
......
...@@ -130,7 +130,7 @@ class _FlexibleSpaceBarState extends State<FlexibleSpaceBar> { ...@@ -130,7 +130,7 @@ class _FlexibleSpaceBarState extends State<FlexibleSpaceBar> {
scale: new Tween<double>(begin: 1.5, end: 1.0).animate(scaleAndAlignCurve), scale: new Tween<double>(begin: 1.5, end: 1.0).animate(scaleAndAlignCurve),
child: new Align( child: new Align(
alignment: new FractionalOffset(0.0, 1.0), alignment: new FractionalOffset(0.0, 1.0),
child: new DefaultTextStyle(style: titleStyle, child: config.title) child: new DefaultTextStyle.explicit(style: titleStyle, child: config.title)
) )
) )
) )
......
...@@ -83,13 +83,13 @@ class GridTileBar extends StatelessWidget { ...@@ -83,13 +83,13 @@ class GridTileBar extends StatelessWidget {
child: new Column( child: new Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[ children: <Widget>[
new DefaultTextStyle( new DefaultTextStyle.explicit(
style: Typography.white.subhead, style: Typography.white.subhead,
softWrap: false, softWrap: false,
overflow: TextOverflow.ellipsis, overflow: TextOverflow.ellipsis,
child: title child: title
), ),
new DefaultTextStyle( new DefaultTextStyle.explicit(
style: Typography.white.caption, style: Typography.white.caption,
softWrap: false, softWrap: false,
overflow: TextOverflow.ellipsis, overflow: TextOverflow.ellipsis,
...@@ -102,7 +102,7 @@ class GridTileBar extends StatelessWidget { ...@@ -102,7 +102,7 @@ class GridTileBar extends StatelessWidget {
} else if (title != null || subtitle != null) { } else if (title != null || subtitle != null) {
children.add( children.add(
new Flexible( new Flexible(
child: new DefaultTextStyle( child: new DefaultTextStyle.explicit(
style: Typography.white.subhead, style: Typography.white.subhead,
softWrap: false, softWrap: false,
overflow: TextOverflow.ellipsis, overflow: TextOverflow.ellipsis,
......
...@@ -137,7 +137,7 @@ class SnackBar extends StatelessWidget { ...@@ -137,7 +137,7 @@ class SnackBar extends StatelessWidget {
new Flexible( new Flexible(
child: new Container( child: new Container(
margin: const EdgeInsets.symmetric(vertical: _kSingleLineVerticalPadding), margin: const EdgeInsets.symmetric(vertical: _kSingleLineVerticalPadding),
child: new DefaultTextStyle( child: new DefaultTextStyle.explicit(
style: Typography.white.subhead, style: Typography.white.subhead,
child: content child: content
) )
......
...@@ -920,7 +920,7 @@ class _TabBarState<T> extends ScrollableState<TabBar<T>> implements TabBarSelect ...@@ -920,7 +920,7 @@ class _TabBarState<T> extends ScrollableState<TabBar<T>> implements TabBarSelect
Widget contents = new IconTheme( Widget contents = new IconTheme(
data: iconTheme, data: iconTheme,
child: new DefaultTextStyle( child: new DefaultTextStyle.explicit(
style: textStyle, style: textStyle,
child: new _TabBarWrapper( child: new _TabBarWrapper(
children: tabs, children: tabs,
......
...@@ -116,7 +116,7 @@ class _TwoLevelSublistState extends State<TwoLevelSublist> { ...@@ -116,7 +116,7 @@ class _TwoLevelSublistState extends State<TwoLevelSublist> {
child: new TwoLevelListItem( child: new TwoLevelListItem(
onTap: _handleOnTap, onTap: _handleOnTap,
leading: config.leading, leading: config.leading,
title: new DefaultTextStyle( title: new DefaultTextStyle.explicit(
style: Theme.of(context).textTheme.subhead.copyWith(color: _headerColor.evaluate(_easeInAnimation)), style: Theme.of(context).textTheme.subhead.copyWith(color: _headerColor.evaluate(_easeInAnimation)),
child: config.title child: config.title
), ),
......
...@@ -187,7 +187,7 @@ class WidgetsAppState<T extends WidgetsApp> extends State<T> implements WidgetsB ...@@ -187,7 +187,7 @@ class WidgetsAppState<T extends WidgetsApp> extends State<T> implements WidgetsB
) )
); );
if (config.textStyle != null) { if (config.textStyle != null) {
new DefaultTextStyle( new DefaultTextStyle.explicit(
style: config.textStyle, style: config.textStyle,
child: result child: result
); );
......
...@@ -1954,9 +1954,12 @@ class RichText extends LeafRenderObjectWidget { ...@@ -1954,9 +1954,12 @@ class RichText extends LeafRenderObjectWidget {
class DefaultTextStyle extends InheritedWidget { class DefaultTextStyle extends InheritedWidget {
/// Creates a default text style for the given subtree. /// Creates a default text style for the given subtree.
/// ///
/// Can be used only with a [TextStyle] whose [TextStyle.inherit] field is
/// false.
///
/// Consider using [DefaultTextStyle.inherit] to inherit styling information /// Consider using [DefaultTextStyle.inherit] to inherit styling information
/// from a the current default text style for a given [BuildContext]. /// from a the current default text style for a given [BuildContext].
DefaultTextStyle({ DefaultTextStyle.explicit({
Key key, Key key,
this.style, this.style,
this.textAlign, this.textAlign,
...@@ -1968,6 +1971,20 @@ class DefaultTextStyle extends InheritedWidget { ...@@ -1968,6 +1971,20 @@ class DefaultTextStyle extends InheritedWidget {
assert(softWrap != null); assert(softWrap != null);
assert(overflow != null); assert(overflow != null);
assert(child != null); assert(child != null);
assert(() {
if (style.inherit) {
throw new FlutterError(
'Inherited style cannot be used with DefaultTextStyle.explicit.\n'
'DefaultTextStyle.explicit does not inherit styles from the '
'DefaultTextStyle for the current BuildContext. Please either use an '
'explicit text style (i.e., one without the "inherit" flag set to '
'true) or use the DefaultTextStyle.inherit constructor, which does '
'inherit styles from the DefaultTextStyle for the current '
'BuildContext.'
);
}
return true;
});
} }
/// A const-constructible default text style that provides fallback values. /// A const-constructible default text style that provides fallback values.
...@@ -1997,7 +2014,7 @@ class DefaultTextStyle extends InheritedWidget { ...@@ -1997,7 +2014,7 @@ class DefaultTextStyle extends InheritedWidget {
assert(context != null); assert(context != null);
assert(child != null); assert(child != null);
DefaultTextStyle parent = DefaultTextStyle.of(context); DefaultTextStyle parent = DefaultTextStyle.of(context);
return new DefaultTextStyle( return new DefaultTextStyle.explicit(
key: key, key: key,
style: parent.style.merge(style), style: parent.style.merge(style),
textAlign: textAlign ?? parent.textAlign, textAlign: textAlign ?? parent.textAlign,
......
...@@ -560,7 +560,8 @@ class _AnimatedDefaultTextStyleState extends AnimatedWidgetBaseState<AnimatedDef ...@@ -560,7 +560,8 @@ class _AnimatedDefaultTextStyleState extends AnimatedWidgetBaseState<AnimatedDef
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return new DefaultTextStyle( return new DefaultTextStyle.inherit(
context: context,
style: _style.evaluate(animation), style: _style.evaluate(animation),
child: config.child child: config.child
); );
......
...@@ -14,7 +14,8 @@ Widget buildCard(BuildContext context, int index) { ...@@ -14,7 +14,8 @@ Widget buildCard(BuildContext context, int index) {
return new Container( return new Container(
key: new ValueKey<int>(items[index]), key: new ValueKey<int>(items[index]),
height: 100.0, height: 100.0,
child: new DefaultTextStyle( child: new DefaultTextStyle.inherit(
context: context,
style: new TextStyle(fontSize: 2.0 + items.length.toDouble()), style: new TextStyle(fontSize: 2.0 + items.length.toDouble()),
child: new Text('${items[index]}') child: new Text('${items[index]}')
) )
......
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