widget_span.dart 6.58 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9 10
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:ui' as ui show ParagraphBuilder, PlaceholderAlignment;

import 'package:flutter/painting.dart';

import 'framework.dart';

11 12 13
// Examples can assume:
// late WidgetSpan myWidgetSpan;

14 15 16 17 18 19 20 21 22 23 24 25 26 27
/// An immutable widget that is embedded inline within text.
///
/// The [child] property is the widget that will be embedded. Children are
/// constrained by the width of the paragraph.
///
/// The [child] property may contain its own [Widget] children (if applicable),
/// including [Text] and [RichText] widgets which may include additional
/// [WidgetSpan]s. Child [Text] and [RichText] widgets will be laid out
/// independently and occupy a rectangular space in the parent text layout.
///
/// [WidgetSpan]s will be ignored when passed into a [TextPainter] directly.
/// To properly layout and paint the [child] widget, [WidgetSpan] should be
/// passed into a [Text.rich] widget.
///
28
/// {@tool snippet}
29 30 31 32
///
/// A card with `Hello World!` embedded inline within a TextSpan tree.
///
/// ```dart
33
/// const Text.rich(
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
///   TextSpan(
///     children: <InlineSpan>[
///       TextSpan(text: 'Flutter is'),
///       WidgetSpan(
///         child: SizedBox(
///           width: 120,
///           height: 50,
///           child: Card(
///             child: Center(
///               child: Text('Hello World!')
///             )
///           ),
///         )
///       ),
///       TextSpan(text: 'the best!'),
///     ],
///   )
/// )
/// ```
/// {@end-tool}
///
/// [WidgetSpan] contributes the semantics of the [WidgetSpan.child] to the
/// semantics tree.
///
/// See also:
///
///  * [TextSpan], a node that represents text in an [InlineSpan] tree.
///  * [Text], a widget for showing uniformly-styled text.
///  * [RichText], a widget for finer control of text rendering.
///  * [TextPainter], a class for painting [InlineSpan] objects on a [Canvas].
@immutable
class WidgetSpan extends PlaceholderSpan {
  /// Creates a [WidgetSpan] with the given values.
  ///
  /// The [child] property must be non-null. [WidgetSpan] is a leaf node in
  /// the [InlineSpan] tree. Child widgets are constrained by the width of the
  /// paragraph they occupy. Child widget heights are unconstrained, and may
  /// cause the text to overflow and be ellipsized/truncated.
  ///
  /// A [TextStyle] may be provided with the [style] property, but only the
  /// decoration, foreground, background, and spacing options will be used.
  const WidgetSpan({
76
    required this.child,
77 78 79
    super.alignment,
    super.baseline,
    super.style,
80
  }) : assert(child != null),
81 82 83 84 85 86
       assert(
         baseline != null || !(
          identical(alignment, ui.PlaceholderAlignment.aboveBaseline) ||
          identical(alignment, ui.PlaceholderAlignment.belowBaseline) ||
          identical(alignment, ui.PlaceholderAlignment.baseline)
        ),
87
      );
88 89 90 91 92 93 94 95 96 97 98 99 100

  /// The widget to embed inline within text.
  final Widget child;

  /// Adds a placeholder box to the paragraph builder if a size has been
  /// calculated for the widget.
  ///
  /// Sizes are provided through `dimensions`, which should contain a 1:1
  /// in-order mapping of widget to laid-out dimensions. If no such dimension
  /// is provided, the widget will be skipped.
  ///
  /// The `textScaleFactor` will be applied to the laid-out size of the widget.
  @override
101
  void build(ui.ParagraphBuilder builder, { double textScaleFactor = 1.0, List<PlaceholderDimensions>? dimensions }) {
102 103 104 105
    assert(debugAssertIsValid());
    assert(dimensions != null);
    final bool hasStyle = style != null;
    if (hasStyle) {
106
      builder.pushStyle(style!.getTextStyle(textScaleFactor: textScaleFactor));
107
    }
108 109
    assert(builder.placeholderCount < dimensions!.length);
    final PlaceholderDimensions currentDimensions = dimensions![builder.placeholderCount];
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    builder.addPlaceholder(
      currentDimensions.size.width,
      currentDimensions.size.height,
      alignment,
      scale: textScaleFactor,
      baseline: currentDimensions.baseline,
      baselineOffset: currentDimensions.baselineOffset,
    );
    if (hasStyle) {
      builder.pop();
    }
  }

  /// Calls `visitor` on this [WidgetSpan]. There are no children spans to walk.
  @override
  bool visitChildren(InlineSpanVisitor visitor) {
    return visitor(this);
  }

  @override
130
  InlineSpan? getSpanForPositionVisitor(TextPosition position, Accumulator offset) {
131 132 133 134
    if (position.offset == offset.value) {
      return this;
    }
    offset.increment(1);
135 136 137 138
    return null;
  }

  @override
139
  int? codeUnitAtVisitor(int index, Accumulator offset) {
140
    offset.increment(1);
141
    return PlaceholderSpan.placeholderCodeUnit;
142 143 144 145
  }

  @override
  RenderComparison compareTo(InlineSpan other) {
146
    if (identical(this, other)) {
147
      return RenderComparison.identical;
148 149
    }
    if (other.runtimeType != runtimeType) {
150
      return RenderComparison.layout;
151 152
    }
    if ((style == null) != (other.style == null)) {
153
      return RenderComparison.layout;
154
    }
155
    final WidgetSpan typedOther = other as WidgetSpan;
156 157 158 159 160
    if (child != typedOther.child || alignment != typedOther.alignment) {
      return RenderComparison.layout;
    }
    RenderComparison result = RenderComparison.identical;
    if (style != null) {
161
      final RenderComparison candidate = style!.compareTo(other.style!);
162
      if (candidate.index > result.index) {
163
        result = candidate;
164 165
      }
      if (result == RenderComparison.layout) {
166
        return result;
167
      }
168 169 170 171 172
    }
    return result;
  }

  @override
173
  bool operator ==(Object other) {
174
    if (identical(this, other)) {
175
      return true;
176 177
    }
    if (other.runtimeType != runtimeType) {
178
      return false;
179 180
    }
    if (super != other) {
181
      return false;
182
    }
183 184 185 186
    return other is WidgetSpan
        && other.child == child
        && other.alignment == alignment
        && other.baseline == baseline;
187 188 189
  }

  @override
190
  int get hashCode => Object.hash(super.hashCode, child, alignment, baseline);
191 192 193

  /// Returns the text span that contains the given position in the text.
  @override
194
  InlineSpan? getSpanForPosition(TextPosition position) {
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
    assert(debugAssertIsValid());
    return null;
  }

  /// In debug mode, throws an exception if the object is not in a
  /// valid configuration. Otherwise, returns true.
  ///
  /// This is intended to be used as follows:
  ///
  /// ```dart
  /// assert(myWidgetSpan.debugAssertIsValid());
  /// ```
  @override
  bool debugAssertIsValid() {
    // WidgetSpans are always valid as asserts prevent invalid WidgetSpans
    // from being constructed.
    return true;
  }
}