新版的flutter已经自带这个功能了。TextSyle 中一个shadow 。


目前flutter中没找到很好的办法给Text增加描边。自己扩展了一个TextEx,可以实现简单的描边效果,能满足大部分情况下的需求。

class TextEx extends LeafRenderObjectWidget {
TextEx(this.data, {
Key key,
this.style,
this.textAlign = TextAlign.left,
this.textDirection,
this.locale,
this.softWrap = true,
this.overflow = TextOverflow.clip,
this.textScaleFactor = 1.0,
this.maxLines,
this.semanticsLabel,
this.shadow = Colors.black12
}) : assert(data != null),
super(key: key); final String data;
TextSpan textSpan;
final TextStyle style;
final TextAlign textAlign;
final TextDirection textDirection;
final Locale locale;
final bool softWrap;
final TextOverflow overflow;
final double textScaleFactor;
final int maxLines;
final String semanticsLabel;
final Color shadow; @override
RenderParagraph createRenderObject(BuildContext context) {
assert(textDirection != null || debugCheckHasDirectionality(context));
final textSpan = new TextSpan(
style: style,
text: data,
children: null,
);
return new RenderParagraphEx(textSpan,
textAlign: textAlign,
textDirection: textDirection ?? Directionality.of(context),
softWrap: softWrap,
overflow: overflow,
textScaleFactor: textScaleFactor,
maxLines: maxLines,
shadow: shadow,
locale: locale ?? Localizations.localeOf(context, nullOk: true),
);
} @override
void updateRenderObject(BuildContext context, RenderParagraph renderObject) {
assert(textDirection != null || debugCheckHasDirectionality(context));
final textSpan = new TextSpan(
style: style,
text: data,
children: null,
);
renderObject
..text = textSpan
..textAlign = textAlign
..textDirection = textDirection ?? Directionality.of(context)
..softWrap = softWrap
..overflow = overflow
..textScaleFactor = textScaleFactor
..maxLines = maxLines
..locale = locale ?? Localizations.localeOf(context, nullOk: true);
} } const String _kEllipsis = '\u2026'; class RenderParagraphEx extends RenderParagraph {
RenderParagraphEx(TextSpan text, {
TextAlign textAlign = TextAlign.start,
@required TextDirection textDirection,
bool softWrap = true,
TextOverflow overflow = TextOverflow.clip,
double textScaleFactor = 1.0,
int maxLines,
Locale locale,
this.shadow = Colors.black87,
}): super(text, textAlign: textAlign, textDirection: textDirection, softWrap: softWrap, overflow: overflow, textScaleFactor: textScaleFactor, maxLines: maxLines, locale: locale) {
initTextPainter();
} TextPainter _textPainter; final Color shadow; void initTextPainter() {
if (_textPainter == null || _textPainter.text.text != text.text) {
_textPainter = new TextPainter(text: TextSpan(style: TextStyle(
inherit: text.style.inherit,
color: shadow,
fontSize: text.style.fontSize,
fontWeight: text.style.fontWeight,
fontFamily: text.style.fontFamily,
fontStyle: text.style.fontStyle,
wordSpacing: text.style.wordSpacing,
textBaseline: text.style.textBaseline,
letterSpacing: text.style.letterSpacing,
height: text.style.height,
locale: text.style.locale,
), text: text.text, children: text.children, recognizer: text.recognizer
),
textAlign: textAlign,
textDirection: textDirection,
textScaleFactor: textScaleFactor,
maxLines: maxLines,
ellipsis: overflow == TextOverflow.ellipsis ? _kEllipsis : null,
locale: locale,
);
}
} void _layoutText({ double minWidth = 0.0, double maxWidth = double.infinity }) {
final bool widthMatters = softWrap || overflow == TextOverflow.ellipsis;
_textPainter.layout(minWidth: minWidth, maxWidth: widthMatters ? maxWidth : double.infinity);
} void _layoutTextWithConstraints(BoxConstraints constraints) {
_layoutText(minWidth: constraints.minWidth, maxWidth: constraints.maxWidth);
} @override
void paint(PaintingContext context, Offset offset) {
final Canvas canvas = context.canvas;
initTextPainter();
_layoutTextWithConstraints(constraints);
_textPainter.paint(canvas, offset.translate(1.0, 1.0));
_textPainter.paint(canvas, offset.translate(-1.0, -1.0));
_textPainter.paint(canvas, offset.translate(-1.0, 1.0));
_textPainter.paint(canvas, offset.translate(1.0, -1.0));
_textPainter.paint(canvas, offset.translate(1.0, 0.0));
_textPainter.paint(canvas, offset.translate(0.0, 1.0));
_textPainter.paint(canvas, offset.translate(1.5, 1.5));
canvas.scale(1.0, 1.0);
super.paint(context, offset);
}
}

[Flutter] 支持描边效果的Text的更多相关文章

  1. Unity Shader实现描边效果

    http://gad.qq.com/article/detail/28346 描边效果是游戏里面非常常用的一种效果,一般是为了凸显游戏中的某个对象,会给对象增加一个描边效果.本篇文章和大家介绍下利用S ...

  2. 使用 WPF 做个 PowerPoint 系列 基于 OpenXML 解析实现 PPT 文本描边效果

    本文是使用 WPF 做个 PowerPoint 系列的博客,本文来告诉大家如何解析 PPT 里面的文本描边效果,在 WPF 应用中绘制出来,实现像素级相同 背景知识 在开始之前,期望你了解了 PPT ...

  3. UE4实现描边效果

    描边效果属于常见常用的功能,现VR项目中,也需要射线选中一个物体,使物体高亮. 于是在网上找了部分资料,同时也感谢群里的一位大神的提点,总算将描边的功能实现了,这里也写一个简单的示例步骤. 1.我并不 ...

  4. unity描边效果

    这里总结了几种在unity实现描边效果的方法,首先准备一个模型导入在unity中,使用默认shader,上传一张原始图,以便后面实现功能效果的对比 一.边缘光,这里参照官方的一个SurfaceShad ...

  5. osgEarth2.8加载矢量数据描边效果

    通过修改osgearth自带的agglite插件,实现矢量描边效果,可以自定义描边的颜色和宽度(单位像素) 测试文件osgearth_features.cpp #include <osg/Not ...

  6. three.js使用卷积法实现物体描边效果

    法线延展法 网上使用法线延展法实现物体描边效果的文章比较多,这里不再描述. 但是这种方法有个缺点:当两个面的法线夹角差别较大时,两个面的描边无法完美连接.如下图所示: 卷积法 这里使用另一种方法卷积法 ...

  7. 用CSS3实现文字描边效果【效果在这儿,创意在你!】

    CSS3作为新兴的前端技术可以实现很多复杂变化的效果,比如文字描边. 这里主要用到text-shadow属性,顾名思义就是为文字加上阴影效果.例: text-shadow:10px 5px 2px # ...

  8. Flutter Container容器组件、Text文本组件详解

    import 'package:flutter/material.dart'; void main(){ runApp(MyApp()); } class MyApp extends Stateles ...

  9. 使IE6支持:hover效果

    :hover是在CSS中用来制作效果最常用到的一个伪类,比如:标签或div上的鼠标悬停效果 li:hover,div:hover等. 但这种效果是css2及以上版本才添加的,对于只支持css1的浏览器 ...

随机推荐

  1. CodeForces - 367E:Sereja and Intervals(组合数&&DP)

    Sereja is interested in intervals of numbers, so he has prepared a problem about intervals for you. ...

  2. 配置搭建与使用redis

    redis单点.redis主从.redis哨兵 sentinel,redis集群cluster配置搭建与使用 redis是如今被互联网公司使用最广泛的一个中间件,我们打开GitHub搜索redis,边 ...

  3. MacOS 快捷键技巧

    Ctrl+↑打开mission ctrl 可以在mission ctrl 里新建桌面 ,通过Ctrl+←→切换桌面 可以拖拽应用窗口到新的桌面 Ctrl+↓可以打开fonder的所有窗口 F11 全屏

  4. 动画Completed事件里获取执行该动画的UI对象

    转:http://www.cnblogs.com/xyq/p/3775732.html 昨天群里有位童鞋提出如何在动画完成事件Completed里获取到执行该动画的UI对象. WPF里动画的Compl ...

  5. 用C实现FFT算法

    用C语言编写FFT算法  转http://blog.sina.com.cn/s/blog_65d639d50101buo1.html #include "math.h" #defi ...

  6. vault 集群搭建(active standby 模式)

        参考架构图: consul server cluster 搭建 consul 基本配置格式 { "server": true, "node_name": ...

  7. hadoop之 参数调优

    一. hdfs-site.xml 配置文件 1. dfs.blocksize 参数:hadoop文件块大小描述:新文件的默认块大小,以字节为单位,默认 134217728 字节.可以使用以下后缀(大小 ...

  8. RabbitMQ 知识点

    mac下 rabbitmq 安装: brew命令 brew install rabbitmq 注意: rabbitmq的安装目录: /usr/local/Cellar/rabbitmq/3.6.6 r ...

  9. ADO.NET EF 中的实体修改方法

    http://www.cnblogs.com/zfz15011/archive/2010/05/30/1747486.html 1.传统修改模式,看下列代码 using (NorthwindEntit ...

  10. BASIC-4_蓝桥杯_数列特征

    示例代码: #include <stdio.h>#include <stdlib.h> int main(void){ int n = 0 ; int i = 0 ,  max ...