2025-08-20 17:01:46 +05:30
|
|
|
import 'package:flutter/material.dart';
|
2025-10-02 13:52:28 +05:30
|
|
|
import 'package:gpt_markdown/gpt_markdown.dart';
|
2025-08-20 17:01:46 +05:30
|
|
|
|
2025-09-30 20:49:02 +05:30
|
|
|
import 'markdown_config.dart';
|
2025-08-20 17:01:46 +05:30
|
|
|
|
2025-10-02 13:52:28 +05:30
|
|
|
typedef MarkdownLinkTapCallback = void Function(String url, String title);
|
|
|
|
|
|
2025-10-02 15:21:44 +05:30
|
|
|
class StreamingMarkdownWidget extends StatelessWidget {
|
2025-08-20 17:01:46 +05:30
|
|
|
const StreamingMarkdownWidget({
|
|
|
|
|
super.key,
|
2025-09-30 20:49:02 +05:30
|
|
|
required this.content,
|
2025-08-20 17:01:46 +05:30
|
|
|
required this.isStreaming,
|
2025-09-30 20:49:02 +05:30
|
|
|
this.onTapLink,
|
2025-08-20 17:01:46 +05:30
|
|
|
});
|
|
|
|
|
|
2025-09-30 20:49:02 +05:30
|
|
|
final String content;
|
|
|
|
|
final bool isStreaming;
|
2025-10-02 13:52:28 +05:30
|
|
|
final MarkdownLinkTapCallback? onTapLink;
|
2025-08-20 17:01:46 +05:30
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
2025-09-30 20:49:02 +05:30
|
|
|
final markdownTheme = ConduitMarkdownConfig.resolve(context);
|
2025-08-20 17:01:46 +05:30
|
|
|
|
2025-09-30 20:49:02 +05:30
|
|
|
if (content.trim().isEmpty) {
|
|
|
|
|
return isStreaming ? const SizedBox.shrink() : const SizedBox.shrink();
|
2025-08-20 17:01:46 +05:30
|
|
|
}
|
|
|
|
|
|
2025-10-02 13:52:28 +05:30
|
|
|
final textScaler = MediaQuery.maybeOf(context)?.textScaler;
|
|
|
|
|
|
|
|
|
|
return GptMarkdownTheme(
|
|
|
|
|
gptThemeData: markdownTheme.themeData,
|
|
|
|
|
child: GptMarkdown(
|
|
|
|
|
content,
|
|
|
|
|
style: markdownTheme.textStyle,
|
|
|
|
|
followLinkColor: markdownTheme.followLinkColor,
|
|
|
|
|
textDirection: Directionality.of(context),
|
|
|
|
|
textScaler: textScaler,
|
|
|
|
|
onLinkTap: onTapLink,
|
|
|
|
|
codeBuilder: markdownTheme.codeBuilder,
|
|
|
|
|
imageBuilder: markdownTheme.imageBuilder,
|
|
|
|
|
),
|
2025-09-30 19:53:19 +05:30
|
|
|
);
|
2025-08-20 17:01:46 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extension StreamingMarkdownExtension on String {
|
2025-09-30 16:02:34 +05:30
|
|
|
Widget toMarkdown({required BuildContext context, bool isStreaming = false}) {
|
2025-09-30 20:49:02 +05:30
|
|
|
return StreamingMarkdownWidget(content: this, isStreaming: isStreaming);
|
2025-08-20 17:01:46 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class MarkdownWithLoading extends StatelessWidget {
|
2025-09-30 20:49:02 +05:30
|
|
|
const MarkdownWithLoading({super.key, this.content, required this.isLoading});
|
|
|
|
|
|
2025-08-20 17:01:46 +05:30
|
|
|
final String? content;
|
|
|
|
|
final bool isLoading;
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
2025-09-30 20:49:02 +05:30
|
|
|
final value = content ?? '';
|
|
|
|
|
if (isLoading && value.isEmpty) {
|
2025-09-30 16:02:34 +05:30
|
|
|
return const Center(child: CircularProgressIndicator());
|
2025-08-20 17:01:46 +05:30
|
|
|
}
|
|
|
|
|
|
2025-09-30 20:49:02 +05:30
|
|
|
return StreamingMarkdownWidget(content: value, isStreaming: isLoading);
|
2025-08-20 17:01:46 +05:30
|
|
|
}
|
2025-09-24 12:00:49 +05:30
|
|
|
}
|