新版的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. ACdream - 1735:输油管道

    Time Limit: 2000/1000MS (Java/Others) Memory Limit: 262144/131072KB (Java/Others) Problem Descriptio ...

  2. springboot项目搭建

    https://blog.csdn.net/u012702547/article/details/54319508

  3. MySQL Transaction--RC事务隔离级别下加锁测试

    ==============================================================================非索引列更新 在读提交的事务隔离级别下,在非 ...

  4. MySQL--修改普通表为自增表

    =========================================================== 修改普通表为自增表 将普通表修改为自增表,可分为两种类型: 1.将现有列修改为自 ...

  5. POJ3177(3352)(边双连通分量)

    题目: 原本没有记录桥是谁,而是染色时即时判断的.后来发现不行,因为a去b可能满足low[b]>dfn[a],但b去a就不满足了. 这是因为low和dfn的关系是相对的,仅限于tarjan时的那 ...

  6. POJ3013 Big Christmas Tree

    题目:http://poj.org/problem?id=3013 求每个点到1的最短路.不是最小生成树. 总是WA.看讨论里说INF至少2e10,于是真的A了! 算一下,dis最大可能3276800 ...

  7. 栈的一个实例——Dijkstra的双栈算术表达式求值法

    Dijkstra的双栈算术表达式求值法,即是计算算术表达式的值,如表达式(1 + ( (2+3) * (4*5) ) ). 该方法是 使用两个栈分别存储算术表达式的运算符与操作数 忽略左括号 遇到右括 ...

  8. AngularJs $scope 里面的$apply 方法和$watch方法

    Angular $scope 里面的$apply 方法 Scope提供$apply方法传播Model变化 <!DOCTYPE html> <html> <head> ...

  9. 最新hadoop虚拟机安装教程(附带图文)

    前两天看到有人留言问在什么情况下需要部署hadoop,我给的回答也很简单,就是在需要处理海量数据的时候才需要考虑部署hadoop.关于这个问题在很早之前的一篇分享文档也有说到这个问题,数据量少的完全发 ...

  10. opencv中的滤波

    以前的时候,为了过滤图像中的一些噪点,学过一些简单的滤波,比如中值滤波,均值滤波,也是自己实现的. 在opencv中有现成的函数可以调用,实现滤波的操作. 函数的原型如下: CVAPI(void) c ...