[Flutter] 支持描边效果的Text
新版的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的更多相关文章
- Unity Shader实现描边效果
http://gad.qq.com/article/detail/28346 描边效果是游戏里面非常常用的一种效果,一般是为了凸显游戏中的某个对象,会给对象增加一个描边效果.本篇文章和大家介绍下利用S ...
- 使用 WPF 做个 PowerPoint 系列 基于 OpenXML 解析实现 PPT 文本描边效果
本文是使用 WPF 做个 PowerPoint 系列的博客,本文来告诉大家如何解析 PPT 里面的文本描边效果,在 WPF 应用中绘制出来,实现像素级相同 背景知识 在开始之前,期望你了解了 PPT ...
- UE4实现描边效果
描边效果属于常见常用的功能,现VR项目中,也需要射线选中一个物体,使物体高亮. 于是在网上找了部分资料,同时也感谢群里的一位大神的提点,总算将描边的功能实现了,这里也写一个简单的示例步骤. 1.我并不 ...
- unity描边效果
这里总结了几种在unity实现描边效果的方法,首先准备一个模型导入在unity中,使用默认shader,上传一张原始图,以便后面实现功能效果的对比 一.边缘光,这里参照官方的一个SurfaceShad ...
- osgEarth2.8加载矢量数据描边效果
通过修改osgearth自带的agglite插件,实现矢量描边效果,可以自定义描边的颜色和宽度(单位像素) 测试文件osgearth_features.cpp #include <osg/Not ...
- three.js使用卷积法实现物体描边效果
法线延展法 网上使用法线延展法实现物体描边效果的文章比较多,这里不再描述. 但是这种方法有个缺点:当两个面的法线夹角差别较大时,两个面的描边无法完美连接.如下图所示: 卷积法 这里使用另一种方法卷积法 ...
- 用CSS3实现文字描边效果【效果在这儿,创意在你!】
CSS3作为新兴的前端技术可以实现很多复杂变化的效果,比如文字描边. 这里主要用到text-shadow属性,顾名思义就是为文字加上阴影效果.例: text-shadow:10px 5px 2px # ...
- Flutter Container容器组件、Text文本组件详解
import 'package:flutter/material.dart'; void main(){ runApp(MyApp()); } class MyApp extends Stateles ...
- 使IE6支持:hover效果
:hover是在CSS中用来制作效果最常用到的一个伪类,比如:标签或div上的鼠标悬停效果 li:hover,div:hover等. 但这种效果是css2及以上版本才添加的,对于只支持css1的浏览器 ...
随机推荐
- jqprint 打印分页
再要分页的结尾处加上<div style="page-break-after:always;"></div> 就可以了
- .NET 除了用 Task 之外,如何自己写一个可以 await 的对象?
.NET 中的 async / await 写异步代码用起来真的很爽,就像写同步一样.我们可以在各种各样的异步代码中看到 Task 返回值,这样大家便可以使用 await 等待这个方法.不过,有时需要 ...
- 状压dp2
2018年全国多校算法寒假训练营练习比赛(第二场) https://www.nowcoder.com/acm/contest/74/F 上一篇状压dp例题由于每个位置都含有一个非负数,所以不需要判断能 ...
- 《DSP using MATLAB》Problem 4.2
用matlab不会证,惭愧.
- Mysql隔离级别 sql示例理解
前言 事务要解决的是多线程并发修改数据库的问题.Mysql innodb 引擎支持事务.类似 Java 中的各种锁,例如乐观锁(CAS),读写锁,悲观锁.事务也有很多级别. 每个隔离级别要解决的问题都 ...
- 【vue】创建一个vue前端项目,编译,发布
npm: Nodejs下的包管理器. webpack: 它主要的用途是通过CommonJS的语法把所有浏览器端需要发布的静态资源做相应的准备,比如资源的合并和打包. vue-cli: 用户生成Vue工 ...
- jquery,从后台查数据,给页面上添加树形。
前台jquery+ajax请求往页面上添加树形的js代码 //传入当前点击节点的id,在后台代码查询出parentid=当前节点id的记录数,从而实现点击当前节点,往后台发送ajax请求,查询出子节点 ...
- Python学习系列(一)(基础入门)
Python入门 本系列为Python学习相关笔记整理所得,IT人,多学无害,多多探索,激发学习兴趣,开拓思维,不求高大上,只求懂点皮毛,作为知识储备,不至于落后太远.如果兴趣学习者,推荐一个基础视频 ...
- django模板里关闭特殊字符转换,在前端以html语法渲染
变量 pagination_html是一个方法返回的html代码,需要在前端渲染出来,不是当字符串显示 <!-- /.box-body --> {% autoescape off %} { ...
- telinit:Did not receive a reply.Possible causes include:the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired
问题: Enabling /etc/fstab swaps: [ok]telinit:Did not receive a reply.Possible causes include:the remot ...