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

flutter_syntax_view 封面
插件详情v4.1.7

flutter_syntax_view

一个静态语法高亮小部件,使用原生 Dart 代码根据编程语言语法突出显示代码文本。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
1.1万
喜欢数
101
Pub 分数
150

安装插件

flutter pub add flutter_syntax_view

插件介绍

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

一个 Flutter 小部件,使用纯 Dart 实现为代码片段提供语法高亮。它支持多种编程语言,并且无需依赖外部资源即可高效渲染高亮文本。非常适合在 Flutter 应用中的文档、教程或开发工具中显示代码。该小部件轻量级,可在所有主要平台(包括移动设备、网页和桌面)上兼容使用。

适合场景

  • 在应用中显示代码示例
  • 在文档中突出显示语法
  • 构建代码编辑器组件
  • 带有代码片段的教育类应用程序
  • 展示编程教程

主要特点

  • 纯 Dart 实现
  • 支持多种编程语言
  • 跨平台兼容性
  • 轻量且高效
  • 无原生依赖
  • 可自定义的主题

适合对象

  • 开发教育类应用的开发者
  • 文档和教程应用
  • 代码共享平台
  • 内部开发者工具
  • 需要语法高亮代码的 UI

使用建议

  • 限制为静态文本(不可编辑)
  • 不支持实时编辑
  • 主题自定义需要手动配置
  • 性能可能因非常大的代码块而有所不同

原始 README

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

flutter_syntax_view

Flutter Syntax Highlighter

Basic Usage

class HomePage extends StatelessWidget {
  const HomePage({super.key});

  final String code = """
// Importing core libraries
import 'dart:math';
int fibonacci(int n) {
  if (n == 0 || n == 1) return n;
  return fibonacci(n - 1) + fibonacci(n - 2);
}          
final int result = fibonacci(20);
/* and there 
    you have it! */
""";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: SyntaxView(
            code: code, // Code text
            syntax: Syntax.DART, // Language
            syntaxTheme: SyntaxTheme.vscodeDark(), // Theme
            fontSize: 12.0, // Font size
            withZoom: true, // Enable/Disable zoom icon controls
            withLinesCount: true, // Enable/Disable line number
            expanded: false, // Enable/Disable container expansion
            selectable: true // Enable/Disable code text selection
            ),
      ),
    );
  }
}

Create a Custom SyntaxTheme

class AppColors{

  static const Color backgroundColor = Color(0xff1A1A19);
  static const Color linesCountColor = Color(0xffEFE9D5);
  static const Color commentStyle    = Color(0xffEEDF7A);
  static const Color zoomIconColor   = Color(0xff77CDFF);
  static const Color stringStyle     = Color(0xffF87A53);
  static const Color baseStyle       = Color(0xffF5F5F5);
  static const Color keywordStyle    = Color(0xffCAE0BC);
  static const Color classStyle      = Color(0xffB9E5E8);

}

class HomePage extends StatelessWidget {
   HomePage({super.key});

 static const String code = r"""
import 'dart:math' as math;

// Coffee class is the best!
class Coffee {
  late int _temperature;

  void heat() => _temperature = 100;
  void chill() => _temperature = -5;

  void sip() {
    final bool isTooHot = math.max(37, _temperature) > 37;
    if (isTooHot)
      print("myyy liiips!");
    else
      print("mmmmm refreshing!");
  }

  int? get temperature => temperature;
}
void main() {
  var coffee = Coffee();
  coffee.heat();
  coffee.sip();
  coffee.chill();
  coffee.sip();
}
/* And there
        you have it */""";


  final SyntaxTheme myCustomTheme = SyntaxTheme.standard().copyWith(
    backgroundColor : AppColors.backgroundColor,
    linesCountColor : AppColors.linesCountColor,
    commentStyle    : const TextStyle(color: AppColors.commentStyle),
    zoomIconColor   : AppColors.zoomIconColor,
    stringStyle     :  const TextStyle(color: AppColors.stringStyle),
    baseStyle       : const TextStyle(color: AppColors.baseStyle),
    keywordStyle    :  const TextStyle(color: AppColors.keywordStyle),
    punctuationStyle:  const TextStyle(color: AppColors.keywordStyle),
    classStyle      :  const TextStyle(color: AppColors.classStyle),
  );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: SyntaxView(
            code: code, // Code text
            syntax: Syntax.DART, // Language
            syntaxTheme: myCustomTheme, // Theme
            fontSize: 12.0, // Font size
            withZoom: true, // Enable/Disable zoom icon controls
            withLinesCount: true, // Enable/Disable line number
            expanded: false, // Enable/Disable container expansion
            selectable: true // Enable/Disable code text selection
          ),
      ),
    );
  }
}

Supported Syntax

  • [x] Dart
  • [x] C
  • [x] C++
  • [x] Java
  • [x] Kotlin
  • [x] Swift
  • [x] JavaScript
  • [x] YAML
  • [x] Rust
  • [x] Lua
  • [x] Python

Themes

Installing

Package

Contributing

  • if you are familiar with Regular Expressions in Dart and would like contribute in adding further syntax support. it will be very appreciated!

Contributors ✨

Thanks goes to these wonderful people!

Features and bugs

If you face any problems feel free to open an issue at the issue tracker. If you feel the library is missing a feature, please raise a ticket on Github. Pull request are also welcome.

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目