Flutter Markdown解析插件gpt_markdown的使用

Flutter Markdown解析插件gpt_markdown的使用

📦 GPT Markdown & LaTeX for Flutter

Pub Version Pub Points GitHub

这是一个全面的Flutter包,用于在您的应用程序中渲染丰富的Markdown和LaTeX内容,专为与ChatGPT和Gemini等AI输出无缝集成而设计。

⭐ 如果您觉得这个包很有帮助,请在 pub.dev 上给它点赞!您的支持非常重要! ⭐


Supported Markdown & LaTeX Features

✨ Feature ✅ Supported 🔜 Upcoming
💻 Code Block
📊 Table
📝 Heading
📌 Unordered List
📋 Ordered List
🔘 Radio Button
☑️ Check Box
➖ Horizontal Line
🔢 Latex Math
↩️ Indent
🖼️ Image
✨ Highlighted Text
✂️ Striked Text
🔵 Bold Text
🜜 Italic Text
🔗 Links
📱 Selectable
📎 Underline 🔜
🧩 Custom components 🔜

✨ Key Features

  • List
    • Unordered list item
    • Ordered list item
  • Horizontal line

  • Links text here
  • Images with size withxhight someText
  • Table
    Name Roll
    sohag 1
  • Striked text striked text
  • Bold text Bold text
  • Italic text Italic text
  • Heading texts

    Heading 1

    Heading 2

    Heading 3

    Heading 4

    Heading 5
    Heading 6
  • Latex formula (\frac a b) or [\frac ab]
  • Radio button and checkbox () Unchecked radio (x) Checked radio [] Unchecked checkbox [x] Checked checkbox
  • Selectable Content 使用 SelectionArea widget 可以使内容可选

🚀 Why Use GPT Markdown?

  • Optimized for AI Outputs: 完美渲染ChatGPT和Gemini的响应。
  • Rich Customization: 使用Flutter widgets如 TextStyle 轻松应用自定义样式。
  • Selectable Content: 使用 SelectionArea 启用内容选择。
  • Seamless Integration: 开箱即用,设置简单。

🛠️ Getting Started

运行以下命令添加包:

flutter pub add gpt_markdown

📖 Usage

以下是使用 gpt_markdown 的完整示例代码:

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

void main() {
  runApp(const MyApp());
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        useMaterial3: true,
        colorSchemeSeed: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final TextEditingController _controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Column(
        children: [
          Expanded(
            child: ListView(
              children: [
                AnimatedBuilder(
                  animation: _controller,
                  builder: (context, _) {
                    return GptMarkdown(
                      _controller.text,
                      style: const TextStyle(
                        color: Colors.red,
                      ),
                    );
                  },
                ),
              ],
            ),
          ),
          ConstrainedBox(
            constraints: const BoxConstraints(maxHeight: 300),
            child: TextField(
              decoration: const InputDecoration(border: OutlineInputBorder()),
              maxLines: null,
              controller: _controller,
            ),
          ),
        ],
      ),
    );
  }
}

Output from gpt_markdown


💡 ChatGPT Response Examples

ChatGPT Response

Welcome to ChatGPT! Below is an example of a response with Markdown and LaTeX code:

Markdown Example

You can use Markdown to format text easily. Here are some examples:

  • Bold Text: This text is bold
  • Italic Text: This text is italicized
  • Link: This is a link
  • Lists:
    1. Item 1
    2. Item 2
    3. Item 3

LaTeX Example

You can also use LaTeX for mathematical expressions. Here’s an example:

  • Equation: ( f(x) = x^2 + 2x + 1 )
  • Integral: ( \int_{0}^{1} x^2 , dx )
  • Matrix:

[ \begin{bmatrix} 1 & 2 & 3 \ 4 & 5 & 6 \ 7 & 8 & 9 \end{bmatrix} ]

Conclusion

Markdown and LaTeX can be powerful tools for formatting text and mathematical expressions in your Flutter app. If you have any questions or need further assistance, feel free to ask!


---

## 🔗 Additional Information

您可以在这里找到源代码:[GitHub](https://github.com/Infinitix-LLC/gpt_markdown)

更多详细信息请参阅 [README.md](https://github.com/Infinitix-LLC/gpt_markdown) 和 [example](https://github.com/Infinitix-LLC/gpt_markdown/tree/main/example/lib/main.dart) 应用程序。

更多关于Flutter Markdown解析插件gpt_markdown的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter Markdown解析插件gpt_markdown的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是一个关于如何在Flutter项目中使用gpt_markdown插件来解析Markdown内容的示例代码。gpt_markdown是一个假定的Markdown解析插件,因为实际上并没有一个广泛认可的名为gpt_markdown的Flutter插件专门用于Markdown解析(截至我最后的知识更新)。不过,为了演示目的,我会假设gpt_markdown提供了类似flutter_markdown插件的功能。

在实际应用中,你可能会使用flutter_markdown插件,它是Flutter官方提供的一个用于解析和渲染Markdown的插件。但既然要求是关于gpt_markdown,我将按照假设的API进行设计。

假设的gpt_markdown插件使用示例

首先,你需要在你的pubspec.yaml文件中添加gpt_markdown依赖(注意:这只是一个假设的依赖,实际中并不存在):

dependencies:
  flutter:
    sdk: flutter
  gpt_markdown: ^0.1.0  # 假设的版本号

然后运行flutter pub get来获取依赖。

接下来,在你的Flutter项目中,你可以这样使用gpt_markdown(再次强调,以下代码是基于假设的API):

import 'package:flutter/material.dart';
import 'package:gpt_markdown/gpt_markdown.dart'; // 假设的导入路径

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('GPT Markdown Demo'),
        ),
        body: MarkdownViewer(
          data: '''
# Hello, Markdown!

This is a demo of **gpt_markdown** plugin in Flutter.

- Item 1
- Item 2
- Item 3

```dart
void main() {
  print('Hello, World!');
}
      ''',
    ),
  ),
);

} }

// 假设的MarkdownViewer组件,用于显示Markdown内容 class MarkdownViewer extends StatelessWidget { final String data;

MarkdownViewer({required this.data});

@override Widget build(BuildContext context) { return GptMarkdown( data: data, // 假设的样式参数,实际使用时请参考插件文档 styleSheet: GptMarkdownStyleSheet( h1: TextStyle(fontSize: 24, fontWeight: FontWeight.bold), p: TextStyle(fontSize: 16), // 其他样式… ), ); } }

// 假设的GptMarkdown组件和样式表类 class GptMarkdown extends StatelessWidget { final String data; final GptMarkdownStyleSheet styleSheet;

GptMarkdown({required this.data, required this.styleSheet});

@override Widget build(BuildContext context) { // 这里应该是解析和渲染Markdown内容的逻辑,但因为是假设的API,所以简化处理 return SingleChildScrollView( child: Padding( padding: const EdgeInsets.all(16.0), child: Text(data), // 实际中应该是解析后的Widget树 ), ); } }

class GptMarkdownStyleSheet { final TextStyle h1; final TextStyle p; // 其他样式属性…

GptMarkdownStyleSheet({ required this.h1, required this.p, // 初始化其他样式… }); }


### 注意事项

1. **实际插件使用**:在实际开发中,你应该使用像`flutter_markdown`这样的官方或广泛认可的插件,而不是假设的`gpt_markdown`。

2. **文档参考**:在使用任何第三方插件时,务必查阅其官方文档以了解正确的使用方法和API。

3. **样式定制**:大多数Markdown解析插件都允许你自定义样式,以满足你的应用需求。

4. **错误处理**:在实际应用中,你可能需要添加错误处理逻辑来处理Markdown解析过程中的潜在问题。

如果你确实需要解析Markdown,并且想要一个实际可用的解决方案,请考虑使用`flutter_markdown`插件。
回到顶部