搜尋套件、儲存庫與 APP 分類

markdown_widget 封面
套件詳情v2.3.2+8

markdown_widget

一個新的 markdown 套件。支援目錄(TOC)功能、程式碼強調,並支援所有平台。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
1萬
喜歡數
411
Pub 分數
160

安裝套件

flutter pub add markdown_widget

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

一個跨平台的 Markdown 渲染套件,適用於 Flutter,支援目錄生成、程式碼區塊語法強調,並在 Android、iOS、Web、macOS、Windows 與 Linux 上實現無縫整合。以簡潔與效能為設計核心,讓開發者能以最少設定顯示豐富的 Markdown 內容。此套件持續維護,專為需要從 Markdown 源動態呈現文字的應用程式所設計。

適用情境

  • 在應用程式中渲染文件
  • 顯示使用者產生的內容
  • 建立幫助或常見問題區塊
  • 產生動態部落格文章
  • 構建知識庫介面

主要特色

  • 目錄支援
  • 語法強調
  • 跨平台相容性
  • 輕量級且快速渲染
  • 可自訂樣式
  • 支援所有主要平台

適合對象

  • 文件應用程式
  • 教育工具
  • 內容密集型應用
  • 開發者工具
  • Web 與桌面端 Flutter 應用

使用建議

  • 需要 Markdown 源格式化
  • 僅限基本 HTML 渲染
  • 可能需要自定主題調整
  • 不適用於複雜佈局
  • 效能取決於內容大小
  • 無內建影像載入支援

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

Language:简体中文 | English | 日本語

screen

📖markdown_widget

Coverage Status pub package demo

A simple and easy-to-use markdown rendering component.

  • Supports TOC (Table of Contents) function for quick location through Headings
  • Supports code highlighting
  • Supports all platforms

🚀Usage

Before starting, you can try out the online demo by clicking demo

import 'package:flutter/material.dart';
import 'package:markdown_widget/markdown_widget.dart';

class MarkdownPage extends StatelessWidget {
  final String data;

  MarkdownPage(this.data);

  @override
  Widget build(BuildContext context) => Scaffold(body: buildMarkdown());

  Widget buildMarkdown() => MarkdownWidget(data: data);
}

If you want to use your own Column or other list widget, you can use MarkdownGenerator

  Widget buildMarkdown() =>
      Column(children: MarkdownGenerator().buildWidgets(data));

Or use MarkdownBlock

  Widget buildMarkdown() =>
      SingleChildScrollView(child: MarkdownBlock(data: data));

📁 More Examples

For advanced usage examples, check out the example/lib/markdown_custom folder in the repository:

  • video.dart - Custom video tag support
  • latex.dart - LaTeX math formula rendering
  • mermaid.dart - Mermaid diagram support (flowcharts, sequence diagrams, etc.)
  • html_support.dart - HTML tag extension
  • custom_node.dart - Custom node implementation examples

These examples demonstrate how to extend the package with custom tags and features.

🌠Night mode

markdown_widget supports night mode by default. Simply use a different MarkdownConfig to enable it.

  Widget buildMarkdown(BuildContext context) {
    final isDark = Theme.of(context).brightness == Brightness.dark;
    final config = isDark
        ? MarkdownConfig.darkConfig
        : MarkdownConfig.defaultConfig;
    final codeWrapper = (child, text, language) =>
        CodeWrapperWidget(child, text, language);
    return MarkdownWidget(
        data: data,
        config: config.copy(configs: [
        isDark
        ? PreConfig.darkConfig.copy(wrapper: codeWrapper)
        : PreConfig().copy(wrapper: codeWrapper)
    ]));
  }
Default mode Night mode

🔗Link

You can customize the style and click events of links, like this

  Widget buildMarkdown() => MarkdownWidget(
      data: data,
      config: MarkdownConfig(configs: [
        LinkConfig(
          style: TextStyle(
            color: Colors.red,
            decoration: TextDecoration.underline,
          ),
          onTap: (url) {
            ///TODO:on tap
          },
        )
      ]));

📜TOC (Table of Contents) feature

Using the TOC is very simple

  final tocController = TocController();

  Widget buildTocWidget() => TocWidget(controller: tocController);

  Widget buildMarkdown() => MarkdownWidget(data: data, tocController: tocController);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Row(
          children: <Widget>[
            Expanded(child: buildTocWidget()),
            Expanded(child: buildMarkdown(), flex: 3)
          ],
        ));
  }

🎈Highlighting code

Highlighting code supports multiple themes.

import 'package:flutter_highlight/themes/a11y-light.dart';

  Widget buildMarkdown() => MarkdownWidget(
      data: data,
      config: MarkdownConfig(configs: [
        PreConfig(theme: a11yLightTheme),
      ]));

🧬Select All and Copy

Cross-platform support for Select All and Copy function.

image

🌐Html tag

As the current package only implements the conversion of Markdown tags, it does not support the conversion of HTML tags by default. However, this functionality can be supported through extension. You can refer to the usage in html_support.dart for more details.

Here is the online HTML demo showcase

🧮Latex support

The example also includes simple support for LaTeX, which can be implemented by referring to the implementation in latex.dart

Here is the online latex demo showcase

🔷Mermaid diagram support

The example includes support for Mermaid diagrams, which can render flowcharts, sequence diagrams, state diagrams, and more. Refer to the implementation in mermaid.dart

Features:

  • Multiple diagram types (flowchart, sequence diagram, state diagram, ER diagram, etc.)
  • Theme support (auto light/dark mode)
  • Interactive display modes (code only, diagram only, or both)
  • Fullscreen viewer with pan & zoom support
  • Independent horizontal scrolling for wide diagrams
  • Smart caching and debouncing for optimal performance

Here is the online Mermaid demo showcase

import 'package:markdown_widget/markdown_widget.dart';
import 'markdown_custom/mermaid.dart';

// Basic usage
final isDark = Theme.of(context).brightness == Brightness.dark;
final preConfig = PreConfig(
  wrapper: createMermaidWrapper(
    config: const MermaidConfig(),
    isDark: isDark,
    preConfig: preConfig,
  ),
);

MarkdownWidget(
  data: markdown,
  config: config.copy(configs: [preConfig]),
)

// With custom configuration
final preConfig = PreConfig(
  wrapper: createMermaidWrapper(
    config: MermaidConfig(
      displayMode: MermaidDisplayMode.codeAndDiagram,
      diagramPadding: EdgeInsets.all(16.0),
      showLoadingIndicator: true,
    ),
    isDark: isDark,
    preConfig: preConfig,
  ),
);

🍑Custom tag implementation

By passing a SpanNodeGeneratorWithTag to MarkdownGeneratorConfig, you can add new tags and the corresponding SpanNodes for those tags. You can also use existing tags to override the corresponding SpanNodes.

You can also customize the parsing rules for Markdown strings using InlineSyntax and BlockSyntax, and generate new tags.

You can refer to this issue to learn how to implement a custom tag.

If you have any good ideas or suggestions, or have any issues using this package, please feel free to open a pull request or issue.

🧾Appendix

Here are the other libraries used in markdown_widget

Packages Descriptions
markdown Parsing markdown data
flutter_highlight Code highlighting
highlight Code highlighting
url_launcher Opening links
visibility_detector Listening for visibility of a widget;
scroll_to_index Enabling ListView to jump to an index.

所屬分類

依賴

依賴清單 6 項

原始碼儲存庫

使用此套件的專案