onMarkup method

  1. @override
HypertextSpan onMarkup(
  1. List<HypertextSpan>? children,
  2. MarkupContext ctx
)
override

Implementation

@override
HypertextSpan onMarkup(List<HypertextSpan>? children, MarkupContext ctx) {
  final colors = ctx.getColors('colors', colorMapper: ctx.colorMapper);
  if (colors == null) {
    return HypertextTextSpan(children: children);
  }

  assert(() {
    if (children != null) {
      bool styleColorChecker(InlineSpan s) {
        final existColor =
            s.style?.color != null && s.style?.color != Colors.white;
        if (existColor) {
          debugPrint(
            '[WARN] GradientMarkup: The color set by the children may cause '
            'the gradient to fail to take effect.',
          );
        }
        return !existColor;
      }

      TextSpan(children: children).dfvChildren(styleColorChecker);
    }
    return true;
  }());

  final stops = ctx.getDoubles('stops');
  final rotate = ctx.getInt('rotation');
  final tileMode = ctx.switchT(ctx.get('tile-mode'), mpTileMode);
  final alignment = ctx.switchT(ctx.get('alignment'), mpPLAlignment);
  final baseline = ctx.switchT(ctx.get('baseline'), mpBaselines);
  final transform = rotate != null ? GradientRotation(deg2rad(rotate)) : null;

  final effectiveAlignment =
      alignment ?? this.alignment ?? PlaceholderAlignment.baseline;
  final effectiveBaseline = baseline ??
      this.baseline ??
      (this.alignment == null ? TextBaseline.alphabetic : null);

  final gradient = LinearGradient(
    colors: colors,
    stops: stops,
    tileMode: tileMode ?? TileMode.clamp,
    transform: transform,
  );
  /* 需要显示设置为白色 */
  const textStyle = TextStyle(
    color: Colors.white,
    decorationColor: Colors.white,
  );
  return HypertextWidgetSpan(
    alignment: effectiveAlignment,
    baseline: effectiveBaseline,
    child: ShaderMask(
      shaderCallback: gradient.createShader,
      child: Text.rich(
        HypertextTextSpan(children: children, style: textStyle),
      ),
    ),
  );
}