text.dart 17.3 KB
Newer Older
1 2 3 4
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:flutter/foundation.dart';
6
import 'package:flutter/painting.dart';
7 8 9

import 'basic.dart';
import 'framework.dart';
10
import 'inherited_theme.dart';
11 12
import 'media_query.dart';

13 14 15
// Examples can assume:
// String _name;

16
/// The text style to apply to descendant [Text] widgets without explicit style.
17 18 19 20 21 22 23
///
/// See also:
///
///  * [AnimatedDefaultTextStyle], which animates changes in the text style
///    smoothly over a given duration.
///  * [DefaultTextStyleTransition], which takes a provided [Animation] to
///    animate changes in text style smoothly over time.
24
class DefaultTextStyle extends InheritedTheme {
25 26
  /// Creates a default text style for the given subtree.
  ///
27
  /// Consider using [DefaultTextStyle.merge] to inherit styling information
28
  /// from the current default text style for a given [BuildContext].
29 30 31 32 33 34 35 36
  ///
  /// The [style] and [child] arguments are required and must not be null.
  ///
  /// The [softWrap] and [overflow] arguments must not be null (though they do
  /// have default values).
  ///
  /// The [maxLines] property may be null (and indeed defaults to null), but if
  /// it is not null, it must be greater than zero.
37
  const DefaultTextStyle({
38 39 40
    Key key,
    @required this.style,
    this.textAlign,
41 42
    this.softWrap = true,
    this.overflow = TextOverflow.clip,
43
    this.maxLines,
44
    this.textWidthBasis = TextWidthBasis.parent,
45
    @required Widget child,
46 47 48
  }) : assert(style != null),
       assert(softWrap != null),
       assert(overflow != null),
49
       assert(maxLines == null || maxLines > 0),
50
       assert(child != null),
51
       assert(textWidthBasis != null),
52
       super(key: key, child: child);
53 54 55 56

  /// A const-constructible default text style that provides fallback values.
  ///
  /// Returned from [of] when the given [BuildContext] doesn't have an enclosing default text style.
57 58 59
  ///
  /// This constructor creates a [DefaultTextStyle] that lacks a [child], which
  /// means the constructed value cannot be incorporated into the tree.
60 61 62 63
  const DefaultTextStyle.fallback()
    : style = const TextStyle(),
      textAlign = null,
      softWrap = true,
64
      maxLines = null,
65 66
      overflow = TextOverflow.clip,
      textWidthBasis = TextWidthBasis.parent;
67

68 69
  /// Creates a default text style that overrides the text styles in scope at
  /// this point in the widget tree.
70 71
  ///
  /// The given [style] is merged with the [style] from the default text style
72 73 74
  /// for the [BuildContext] where the widget is inserted, and any of the other
  /// arguments that are not null replace the corresponding properties on that
  /// same default text style.
75 76 77 78 79 80 81 82 83
  ///
  /// This constructor cannot be used to override the [maxLines] property of the
  /// ancestor with the value null, since null here is used to mean "defer to
  /// ancestor". To replace a non-null [maxLines] from an ancestor with the null
  /// value (to remove the restriction on number of lines), manually obtain the
  /// ambient [DefaultTextStyle] using [DefaultTextStyle.of], then create a new
  /// [DefaultTextStyle] using the [new DefaultTextStyle] constructor directly.
  /// See the source below for an example of how to do this (since that's
  /// essentially what this constructor does).
84
  static Widget merge({
85 86 87 88 89
    Key key,
    TextStyle style,
    TextAlign textAlign,
    bool softWrap,
    TextOverflow overflow,
90
    int maxLines,
91
    TextWidthBasis textWidthBasis,
92
    @required Widget child,
93 94
  }) {
    assert(child != null);
95
    return Builder(
96 97
      builder: (BuildContext context) {
        final DefaultTextStyle parent = DefaultTextStyle.of(context);
98
        return DefaultTextStyle(
99 100 101 102 103 104
          key: key,
          style: parent.style.merge(style),
          textAlign: textAlign ?? parent.textAlign,
          softWrap: softWrap ?? parent.softWrap,
          overflow: overflow ?? parent.overflow,
          maxLines: maxLines ?? parent.maxLines,
105
          textWidthBasis: textWidthBasis ?? parent.textWidthBasis,
106
          child: child,
107 108
        );
      },
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    );
  }

  /// The text style to apply.
  final TextStyle style;

  /// How the text should be aligned horizontally.
  final TextAlign textAlign;

  /// Whether the text should break at soft line breaks.
  ///
  /// If false, the glyphs in the text will be positioned as if there was unlimited horizontal space.
  final bool softWrap;

  /// How visual overflow should be handled.
  final TextOverflow overflow;

126 127 128
  /// An optional maximum number of lines for the text to span, wrapping if necessary.
  /// If the text exceeds the given number of lines, it will be truncated according
  /// to [overflow].
129 130 131 132 133 134
  ///
  /// If this is 1, text will not wrap. Otherwise, text will be wrapped at the
  /// edge of the box.
  ///
  /// If this is non-null, it will override even explicit null values of
  /// [Text.maxLines].
135 136
  final int maxLines;

137 138 139 140
  /// The strategy to use when calculating the width of the Text.
  /// See [TextWidthBasis] for possible values and their implications.
  final TextWidthBasis textWidthBasis;

141 142 143 144
  /// The closest instance of this class that encloses the given context.
  ///
  /// If no such instance exists, returns an instance created by
  /// [DefaultTextStyle.fallback], which contains fallback values.
145 146 147 148 149 150
  ///
  /// Typical usage is as follows:
  ///
  /// ```dart
  /// DefaultTextStyle style = DefaultTextStyle.of(context);
  /// ```
151 152 153 154 155
  static DefaultTextStyle of(BuildContext context) {
    return context.inheritFromWidgetOfExactType(DefaultTextStyle) ?? const DefaultTextStyle.fallback();
  }

  @override
156 157 158 159 160
  bool updateShouldNotify(DefaultTextStyle oldWidget) {
    return style != oldWidget.style ||
        textAlign != oldWidget.textAlign ||
        softWrap != oldWidget.softWrap ||
        overflow != oldWidget.overflow ||
161 162
        maxLines != oldWidget.maxLines ||
        textWidthBasis != oldWidget.textWidthBasis;
163 164 165 166 167 168 169 170 171 172 173 174 175 176
  }

  @override
  Widget wrap(BuildContext context, Widget child) {
    final DefaultTextStyle defaultTextStyle = context.ancestorWidgetOfExactType(DefaultTextStyle);
    return identical(this, defaultTextStyle) ? child : DefaultTextStyle(
      style: style,
      textAlign: textAlign,
      softWrap: softWrap,
      overflow: overflow,
      maxLines: maxLines,
      textWidthBasis: textWidthBasis,
      child: child,
    );
177
  }
178 179

  @override
180 181 182
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
    style?.debugFillProperties(properties);
183 184 185 186
    properties.add(EnumProperty<TextAlign>('textAlign', textAlign, defaultValue: null));
    properties.add(FlagProperty('softWrap', value: softWrap, ifTrue: 'wrapping at box width', ifFalse: 'no wrapping except at line break characters', showName: true));
    properties.add(EnumProperty<TextOverflow>('overflow', overflow, defaultValue: null));
    properties.add(IntProperty('maxLines', maxLines, defaultValue: null));
187
    properties.add(EnumProperty<TextWidthBasis>('textWidthBasis', textWidthBasis, defaultValue: TextWidthBasis.parent));
188 189 190 191 192 193 194 195 196 197 198
  }
}

/// A run of text with a single style.
///
/// The [Text] widget displays a string of text with single style. The string
/// might break across multiple lines or might all be displayed on the same line
/// depending on the layout constraints.
///
/// The [style] argument is optional. When omitted, the text will use the style
/// from the closest enclosing [DefaultTextStyle]. If the given style's
199 200 201 202
/// [TextStyle.inherit] property is true (the default), the given style will
/// be merged with the closest enclosing [DefaultTextStyle]. This merging
/// behavior is useful, for example, to make the text bold while using the
/// default font family and size.
203
///
204
/// {@tool sample}
205
///
206 207 208 209 210 211 212
/// This example shows how to display text using the [Text] widget. If the text
/// overflows, it truncates the text with an ellipsis.
///
/// ![A screenshot of the Text widget](https://flutter.github.io/assets-for-api-docs/assets/widgets/text.png)
///
/// ![A screenshot of the Text widget displaying an ellipsis to trim the overflowing text](https://flutter.github.io/assets-for-api-docs/assets/widgets/text_ellipsis.png)
///
213
/// ```dart
214
/// Text(
215
///   'Hello, $_name! How are you?',
216 217
///   textAlign: TextAlign.center,
///   overflow: TextOverflow.ellipsis,
218
///   style: TextStyle(fontWeight: FontWeight.bold),
219 220
/// )
/// ```
221
/// {@end-tool}
222
///
223 224 225 226 227
/// Using the [Text.rich] constructor, the [Text] widget can
/// display a paragraph with differently styled [TextSpan]s. The sample
/// that follows displays "Hello beautiful world" with different styles
/// for each word.
///
228
/// {@tool sample}
229
///
230 231
/// ![A screenshot of the following rich text example](https://flutter.github.io/assets-for-api-docs/assets/widgets/text_rich.png)
///
232 233
/// ```dart
/// const Text.rich(
234
///   TextSpan(
235
///     text: 'Hello', // default text style
236 237 238
///     children: <TextSpan>[
///       TextSpan(text: ' beautiful ', style: TextStyle(fontStyle: FontStyle.italic)),
///       TextSpan(text: 'world', style: TextStyle(fontWeight: FontWeight.bold)),
239 240
///     ],
///   ),
241
/// )
242
/// ```
243
/// {@end-tool}
244
///
245 246 247 248 249 250 251 252 253 254 255 256 257
/// ## Interactivity
///
/// To make [Text] react to touch events, wrap it in a [GestureDetector] widget
/// with a [GestureDetector.onTap] handler.
///
/// In a material design application, consider using a [FlatButton] instead, or
/// if that isn't appropriate, at least using an [InkWell] instead of
/// [GestureDetector].
///
/// To make sections of the text interactive, use [RichText] and specify a
/// [TapGestureRecognizer] as the [TextSpan.recognizer] of the relevant part of
/// the text.
///
258 259
/// See also:
///
260 261
///  * [RichText], which gives you more control over the text styles.
///  * [DefaultTextStyle], which sets default styles for [Text] widgets.
262 263 264 265 266
class Text extends StatelessWidget {
  /// Creates a text widget.
  ///
  /// If the [style] argument is null, the text will use the style from the
  /// closest enclosing [DefaultTextStyle].
267 268
  ///
  /// The [data] parameter must not be null.
269 270
  const Text(
    this.data, {
271 272
    Key key,
    this.style,
273
    this.strutStyle,
274
    this.textAlign,
Ian Hickson's avatar
Ian Hickson committed
275
    this.textDirection,
276
    this.locale,
277 278
    this.softWrap,
    this.overflow,
279 280
    this.textScaleFactor,
    this.maxLines,
281
    this.semanticsLabel,
282
    this.textWidthBasis,
283 284 285 286
  }) : assert(
         data != null,
         'A non-null String must be provided to a Text widget.',
       ),
287
       textSpan = null,
288
       super(key: key);
289

290 291 292 293 294 295
  /// Creates a text widget with a [InlineSpan].
  ///
  /// The following subclasses of [InlineSpan] may be used to build rich text:
  ///
  /// * [TextSpan]s define text and children [InlineSpan]s.
  /// * [WidgetSpan]s define embedded inline widgets.
296 297
  ///
  /// The [textSpan] parameter must not be null.
298 299
  ///
  /// See [RichText] which provides a lower-level way to draw text.
300 301
  const Text.rich(
    this.textSpan, {
302 303
    Key key,
    this.style,
304
    this.strutStyle,
305 306
    this.textAlign,
    this.textDirection,
307
    this.locale,
308 309 310 311
    this.softWrap,
    this.overflow,
    this.textScaleFactor,
    this.maxLines,
312
    this.semanticsLabel,
313
    this.textWidthBasis,
314 315 316 317
  }) : assert(
         textSpan != null,
         'A non-null TextSpan must be provided to a Text.rich widget.',
       ),
318 319
       data = null,
       super(key: key);
320

321
  /// The text to display.
322 323
  ///
  /// This will be null if a [textSpan] is provided instead.
324 325
  final String data;

326
  /// The text to display as a [InlineSpan].
327 328
  ///
  /// This will be null if [data] is provided instead.
329
  final InlineSpan textSpan;
330

331 332 333 334 335 336 337
  /// If non-null, the style to use for this text.
  ///
  /// If the style's "inherit" property is true, the style will be merged with
  /// the closest enclosing [DefaultTextStyle]. Otherwise, the style will
  /// replace the closest enclosing [DefaultTextStyle].
  final TextStyle style;

338 339 340
  /// {@macro flutter.painting.textPainter.strutStyle}
  final StrutStyle strutStyle;

341 342 343
  /// How the text should be aligned horizontally.
  final TextAlign textAlign;

Ian Hickson's avatar
Ian Hickson committed
344 345 346 347 348 349 350 351 352
  /// The directionality of the text.
  ///
  /// This decides how [textAlign] values like [TextAlign.start] and
  /// [TextAlign.end] are interpreted.
  ///
  /// This is also used to disambiguate how to render bidirectional text. For
  /// example, if the [data] is an English phrase followed by a Hebrew phrase,
  /// in a [TextDirection.ltr] context the English phrase will be on the left
  /// and the Hebrew phrase to its right, while in a [TextDirection.rtl]
353
  /// context, the English phrase will be on the right and the Hebrew phrase on
Ian Hickson's avatar
Ian Hickson committed
354 355 356 357 358
  /// its left.
  ///
  /// Defaults to the ambient [Directionality], if any.
  final TextDirection textDirection;

359 360 361 362 363 364 365 366 367
  /// Used to select a font when the same Unicode character can
  /// be rendered differently, depending on the locale.
  ///
  /// It's rarely necessary to set this property. By default its value
  /// is inherited from the enclosing app with `Localizations.localeOf(context)`.
  ///
  /// See [RenderParagraph.locale] for more information.
  final Locale locale;

368 369 370 371 372 373 374 375 376 377 378 379 380
  /// Whether the text should break at soft line breaks.
  ///
  /// If false, the glyphs in the text will be positioned as if there was unlimited horizontal space.
  final bool softWrap;

  /// How visual overflow should be handled.
  final TextOverflow overflow;

  /// The number of font pixels for each logical pixel.
  ///
  /// For example, if the text scale factor is 1.5, text will be 50% larger than
  /// the specified font size.
  ///
381
  /// The value given to the constructor as textScaleFactor. If null, will
382
  /// use the [MediaQueryData.textScaleFactor] obtained from the ambient
383
  /// [MediaQuery], or 1.0 if there is no [MediaQuery] in scope.
384 385
  final double textScaleFactor;

386
  /// An optional maximum number of lines for the text to span, wrapping if necessary.
387 388
  /// If the text exceeds the given number of lines, it will be truncated according
  /// to [overflow].
389 390 391 392 393 394 395 396
  ///
  /// If this is 1, text will not wrap. Otherwise, text will be wrapped at the
  /// edge of the box.
  ///
  /// If this is null, but there is an ambient [DefaultTextStyle] that specifies
  /// an explicit number for its [DefaultTextStyle.maxLines], then the
  /// [DefaultTextStyle] value will take precedence. You can use a [RichText]
  /// widget directly to entirely override the [DefaultTextStyle].
397 398
  final int maxLines;

399 400 401
  /// An alternative semantics label for this text.
  ///
  /// If present, the semantics of this widget will contain this value instead
402 403
  /// of the actual text. This will overwrite any of the semantics labels applied
  /// directly to the [TextSpan]s.
404
  ///
405
  /// This is useful for replacing abbreviations or shorthands with the full
406 407 408
  /// text value:
  ///
  /// ```dart
409
  /// Text(r'$$', semanticsLabel: 'Double dollars')
410 411 412
  /// ```
  final String semanticsLabel;

413 414 415
  /// {@macro flutter.dart:ui.text.TextWidthBasis}
  final TextWidthBasis textWidthBasis;

416 417
  @override
  Widget build(BuildContext context) {
418
    final DefaultTextStyle defaultTextStyle = DefaultTextStyle.of(context);
419 420 421
    TextStyle effectiveTextStyle = style;
    if (style == null || style.inherit)
      effectiveTextStyle = defaultTextStyle.style.merge(style);
422 423
    if (MediaQuery.boldTextOverride(context))
      effectiveTextStyle = effectiveTextStyle.merge(const TextStyle(fontWeight: FontWeight.bold));
424
    Widget result = RichText(
Ian Hickson's avatar
Ian Hickson committed
425 426
      textAlign: textAlign ?? defaultTextStyle.textAlign ?? TextAlign.start,
      textDirection: textDirection, // RichText uses Directionality.of to obtain a default if this is null.
427
      locale: locale, // RichText uses Localizations.localeOf to obtain a default if this is null
428 429
      softWrap: softWrap ?? defaultTextStyle.softWrap,
      overflow: overflow ?? defaultTextStyle.overflow,
430
      textScaleFactor: textScaleFactor ?? MediaQuery.textScaleFactorOf(context),
431
      maxLines: maxLines ?? defaultTextStyle.maxLines,
432
      strutStyle: strutStyle,
433
      textWidthBasis: textWidthBasis ?? defaultTextStyle.textWidthBasis,
434
      text: TextSpan(
435
        style: effectiveTextStyle,
436
        text: data,
437 438
        children: textSpan != null ? <TextSpan>[textSpan] : null,
      ),
439
    );
440
    if (semanticsLabel != null) {
441
      result = Semantics(
442 443
        textDirection: textDirection,
        label: semanticsLabel,
444
        child: ExcludeSemantics(
445
          child: result,
446
        ),
447 448 449
      );
    }
    return result;
450 451 452
  }

  @override
453 454
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
455
    properties.add(StringProperty('data', data, showName: false));
456
    if (textSpan != null) {
457
      properties.add(textSpan.toDiagnosticsNode(name: 'textSpan', style: DiagnosticsTreeStyle.transition));
458
    }
459
    style?.debugFillProperties(properties);
460 461 462 463 464 465 466
    properties.add(EnumProperty<TextAlign>('textAlign', textAlign, defaultValue: null));
    properties.add(EnumProperty<TextDirection>('textDirection', textDirection, defaultValue: null));
    properties.add(DiagnosticsProperty<Locale>('locale', locale, defaultValue: null));
    properties.add(FlagProperty('softWrap', value: softWrap, ifTrue: 'wrapping at box width', ifFalse: 'no wrapping except at line break characters', showName: true));
    properties.add(EnumProperty<TextOverflow>('overflow', overflow, defaultValue: null));
    properties.add(DoubleProperty('textScaleFactor', textScaleFactor, defaultValue: null));
    properties.add(IntProperty('maxLines', maxLines, defaultValue: null));
467
    if (semanticsLabel != null) {
468
      properties.add(StringProperty('semanticsLabel', semanticsLabel));
469
    }
470 471
  }
}