如何在 Flutter 中制作多种颜色的 TextField

news/2024/10/18 6:12:24/

TextField widget 本身并不施加任何样式。相反,它会要求 TextEditingController 生成一个样式化的 TextSpan 对象,即一段带有样式的文本。

TextField 将其样式传递给 TextEditingController ,默认实现只是将其放入 TextSpan 对象中,这就是通常应用颜色的方式。

要重载该方法,请子类化 TextEditingController 并重载该方法:

class GradientTextEditingController extends TextEditingController {TextSpan buildTextSpan({required BuildContext context,TextStyle? style,bool? withComposing,}) {style ??= const TextStyle();final leftStyle = style.copyWith(color: Colors.red);final rightStyle = style.copyWith(color: Colors.indigo);final children = <TextSpan>[];for (final char in text.characters) {children.add(TextSpan(text: char,style: TextStyle.lerp(leftStyle,rightStyle,children.length / text.length,),),);}return TextSpan(style: style, children: children);}
}

请参阅此处的完整代码。

https://gist.github.com/alexeyinkin/ee65ed81913c8962c2d19e28e11cb262

你可以进行更复杂的处理。例如,我们通过解析语法树并对关键字、字面量、注释等进行不同的着色,来制作代码高亮:

我们首先导入为另一个项目制作的 highlighting 和 flutter_highlighting 包:

import 'package:flutter_highlighting/themes/vs.dart';
import 'package:highlighting/highlighting.dart';
import 'package:highlighting/languages/java.dart';

然后我们解析文本并得到语法树的简单形式:

class SyntaxTextEditingController extends TextEditingController {TextSpan buildTextSpan({required BuildContext context,TextStyle? style,bool? withComposing,}) {final highlighted = highlight.parse(text, languageId: java.id);return TextSpan(style: style,children: _buildList(nodes: highlighted.nodes,styles: vsTheme, // Built-in theme from flutter_highlightingancestorStyle: style,),);}// ...

接下来是遍历语法树并为每个节点返回 TextSpan

 List<TextSpan>? _buildList({required List<Node>? nodes,required Map<String, TextStyle> styles,TextStyle? ancestorStyle,}) {return nodes?.map((node) => _buildNode(node: node,styles: styles,ancestorStyle: ancestorStyle,),).toList(growable: false);}TextSpan _buildNode({required Node node,required Map<String, TextStyle> styles,TextStyle? ancestorStyle,}) {final style = styles[node.className] ?? ancestorStyle;return TextSpan(text: node.value,children: _buildList(nodes: node.children,styles: styles,ancestorStyle: style,),style: style,);}

请参阅此处的完整代码。

https://gist.github.com/alexeyinkin/bff79a057cbf04ecd5166243d06f1d44

因此,TextEditingController 类是实现各种自定义功能的大门。我们在这条路上走得更远,并制作了一个可以做到这一点的高级代码编辑器:

如果您有兴趣,请查看这里。

https://medium.com/akvelon/flutter-code-editor-19e0090a62cc


原文:https://medium.com/akvelon/how-to-make-textfield-in-multiple-colors-in-flutter-c317ae0efafe


http://www.ppmy.cn/news/1432275.html

相关文章

fiddler软件汉化的方法

实现Fiddler软件的汉化&#xff0c;可以按照以下步骤操作&#xff1a; 首先下载并安装Fiddler软件。可以在Fiddler官方网站&#xff08;http://www.telerik.com/fiddler&#xff09;上下载最新版本的Fiddler软件。 在下载完毕后&#xff0c;运行Fiddler软件&#xff0c;确保软…

HBase的数据模型与架构

官方文档&#xff1a;Apache HBase – Apache HBase™ Homehttps://hbase.apache.org/ 一、HBase概述 1.概述 HBase的技术源自Google的BigTable论文&#xff0c;HBase建立在Hadoop之上&#xff0c;是一个高可靠性、高性能、面向列、可伸缩的分布式存储系统&#xff0c;用于…

2024平替电容笔买哪个品牌好?iPad电容笔全能榜单热门款TOP5分享!

2024年&#xff0c;随着科技的不断发展和消费者对生活品质的追求&#xff0c;电容笔作为一种创新的无纸化工具&#xff0c;逐渐走进人们的生活和工作中。然而&#xff0c;在电容笔市场的繁荣背后&#xff0c;也隐藏着品质良莠不齐的现象。众多品牌为了追求利润&#xff0c;推出…

【好书推荐7】《机器学习平台架构实战》

【好书推荐7】《机器学习平台架构实战》 写在最前面《机器学习平台架构实战》编辑推荐内容简介作者简介目  录前  言本书读者内容介绍充分利用本书下载示例代码文件下载彩色图像本书约定 &#x1f308;你好呀&#xff01;我是 是Yu欸 &#x1f30c; 2024每日百字篆刻时光&…

python识别电脑是windows还是linux

代码实现 import osif os.name nt:print(当前操作系统是 Windows) elif os.name posix:print(当前操作系统是 Linux 或 Unix 类型的系统) else:print(未知的操作系统)

视频教程下载:用ChatGPT玩转海外抖音TikTok

CHATGPT for TikTok是一门前沿课程&#xff0c;旨在帮助您充分发挥TikTok广告活动的全部潜力。随着数字营销的爆炸性增长&#xff0c;企业需要使用先进的工具来保持竞争优势。在这门课程中&#xff0c;您将学习如何利用CHATGPT——一种先进的人工智能工具——来创建与目标受众产…

jsp实验10 JavaBean

二、实验项目内容&#xff08;实验题目&#xff09; 编写代码&#xff0c;掌握javabean的用法。【参考课本 上机实验 5.5.1 】 三、源代码以及执行结果截图&#xff1a; 源代码&#xff1a; Fraction.java package sea.water; public class Fraction { public double numbe…

【Ubuntu - php环境配置】

本文记录在ubuntu环境下&#xff0c;分别安装php 和 php-fpm&#xff0c;以及如何在nginx中使用。 安装php 通过以下步骤来完成&#xff1a; 打开终端。 更新系统软件包列表&#xff0c;以确保安装的软件包是最新的&#xff1a; sudo apt update安装PHP及其相关组件。如果你…