搜索插件、代码仓与 APP 分类

syntax_highlight 封面
插件详情v0.5.0

syntax_highlight

适用于 Flutter 的 VSCode 风格语法高亮,接收一个字符串并返回格式化的 TextSpan。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
2.7万
喜欢数
79
Pub 分数
150

安装插件

flutter pub add syntax_highlight

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个 Flutter/Dart 包,为代码片段提供类似 VSCode 的语法高亮。它将纯文本字符串转换为带有适当颜色的 TextSpan,以突出显示关键字、字符串、注释和其他语言元素。非常适合在应用程序、文档或开发工具中显示代码。该包支持多种平台,包括 Android、iOS、Web、macOS、Windows 和 Linux,确保在各种设备上呈现一致。它轻量且可无缝集成到现有的 Flutter 项目中。

适合场景

  • 在文档应用程序中显示代码片段
  • 增强IDE类应用程序中的代码编辑器小部件
  • 在学习或教程平台中突出显示源代码
  • 在开发工具中集成语法感知的文本渲染
  • 在实时协作应用中可视化代码

主要特点

  • VSCode 风格配色方案
  • 支持多种编程语言
  • 返回一个 TextSpan 以方便集成
  • 轻量且高效
  • 跨平台兼容性
  • 可配置的主题和样式

适合对象

  • Flutter 开发人员构建代码编辑器
  • 带有代码示例的教育类应用程序
  • 开发者工具和IDE扩展
  • 文档和教程平台
  • 实时代码共享或协作应用

使用建议

  • 需要手动检测或指定语言
  • 仅限基本语法规则(无高级解析)
  • 可能需要针对非标准语言进行定制
  • 性能取决于输入大小
  • 不支持运行时动态语言切换

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Flutter Syntax Highlight

Syntax Highlight

The Syntax Highlight package uses the TextMate rules for highlighting code, which is used by many popular applications such as VSCode. It is easy to extend the language support by dropping in new grammar files in the grammars directory.

Currently supported languages are: css, dart, go, html, java, javascript, json, kotlin, python, rust, serverpod_protocol, sql, swift, typescript, and yaml.

Code editor

The Syntax Highlight package also comes bundled with a CodeEditor widget, which provides a simplistic interface for editing code. It supports rich text copy (e.g., for pasting into Google Slides).

Live Demo

Usage

Before you can use the Highlighter class it needs to be initialized. The initialization will load the requested grammar files and parse them.

// Initialize the highlighter.
await Highlighter.initialize(['dart', 'yaml', 'sql']);

Next load a theme and create a highlighter.

// Load the default light theme and create a highlighter.
var theme = await HighlighterTheme.loadLightTheme();
var highlighter = Highlighter(
  language: 'dart',
  theme: theme,
);

Now, you can highlight your code by calling the highlight method. The highlight method will return a TextSpan, which you can use in a Text widget.

Widget build(BuildContext context) {
  var highlightedCode = highlighter.highlight(myCodeString);
  return Text.rich(highlightedCode);
}

An example of highlighted code: Highlighted code

所属分类

依赖

依赖清单 3 项

源码仓库

使用此插件的项目