搜尋套件、儲存庫與 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 項

原始碼儲存庫

使用此套件的專案