2025-08-20 17:01:46 +05:30
|
|
|
import 'package:flutter/material.dart';
|
2025-09-30 19:53:19 +05:30
|
|
|
import 'package:flutter_markdown_plus/flutter_markdown_plus.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-09-30 20:49:02 +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;
|
|
|
|
|
final MarkdownTapLinkCallback? 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-09-30 19:53:19 +05:30
|
|
|
return MarkdownBody(
|
2025-09-30 20:49:02 +05:30
|
|
|
data: content,
|
|
|
|
|
styleSheet: markdownTheme.styleSheet,
|
2025-09-30 19:53:19 +05:30
|
|
|
softLineBreak: true,
|
|
|
|
|
selectable: true,
|
2025-09-30 20:49:02 +05:30
|
|
|
builders: markdownTheme.builders,
|
|
|
|
|
inlineSyntaxes: markdownTheme.inlineSyntaxes,
|
|
|
|
|
imageBuilder: markdownTheme.imageBuilder,
|
|
|
|
|
onTapLink: onTapLink,
|
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
|
|
|
}
|