新版的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. Sublime 下配置vim模式 + VintageEx-master下载地址

       VintageEx-master下载地址: 官方地址:https://github.com/SublimeText/VintageEx 百度云链接: http://pan.baidu.com/s ...

  2. 【python】面试常考数据结构算法

    这里整理的都是基础的不能再基础的算法,目的就是进行一个回忆,同时作为剑指offer的一个补充~嘿嘿~ 查找算法二分查找# 实现一个二分查找# 输入:一个顺序list# 输出: 待查找的元素的位置def ...

  3. MySQL--禁用账号和设置账号有效期

    ======================================================================= MySQL5.5/5.6版本 在MySQL 5.7 版本 ...

  4. pykd试用

    啥是pykd? 一个windbg插件,能在windbg里面运行python指令 试用步骤 下载from https://pykd.codeplex.com/releases/view/615625 解 ...

  5. 【转】每天一个linux命令(38):cal 命令

    原文网址:http://www.cnblogs.com/peida/archive/2012/12/14/2817473.html cal命令可以用来显示公历(阳历)日历.公历是现在国际通用的历法,又 ...

  6. RabbitMQ 知识点

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

  7. Elasticsearch 知识点

    Elasticsearch 知识点 table th:first-of-type { width: 200px; } table th:nth-of-type(2) { } 功能 curl命令 运行 ...

  8. HTML5 localStorage使用教程

    在客户端存储数据,HTML5 提供了两种在客户端存储数据的新方法: localStorage - 没有时间限制的数据存储 sessionStorage - 针对一个 session 的数据存储 之前, ...

  9. selenium启动谷歌所遇到的问题

    最近在学习selenium webdriver,发现启动火狐时,运行非常慢,几天前一直在尝试启动谷歌驱动启动,但启动中总遇到问题,启动不起来,一直百度查找方法,还是没搞定,个人比较执着,爱钻牛角尖,弄 ...

  10. Openwrt 3G模块的添加

    一. 在menuconfig中添加相关驱动 1. Kernel Modules -> USB Support <*> kmod-usb2 <*> kmod-usb-ohc ...