Files
pipelines/app/lib/ui/text/markdown_source_view.dart
T

58 lines
1.8 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../features/chat/ui/transcript/chat_markdown_view.dart';
import '../adaptive/host.dart';
/// Authored markdown (a pipeline's source, a stage's prompt) shown rendered,
/// with a switch to the raw text.
class MarkdownSourceView extends StatefulWidget {
const MarkdownSourceView({super.key, required this.text, required this.raw});
final String text;
/// The exact text as the screen already showed it.
final Widget raw;
@override
State<MarkdownSourceView> createState() => _MarkdownSourceViewState();
}
class _MarkdownSourceViewState extends State<MarkdownSourceView> {
// Raw first: the exact text the screen always showed.
bool _raw = true;
void _set(bool raw) => setState(() => _raw = raw);
@override
Widget build(BuildContext context) {
final cupertino = usesCupertinoHost(context);
final control = cupertino
? CupertinoSlidingSegmentedControl<bool>(
key: const Key('markdown-source-mode'),
groupValue: _raw,
onValueChanged: (raw) => _set(raw ?? false),
children: const {false: Text('Rendered'), true: Text('Raw')},
)
: SegmentedButton<bool>(
key: const Key('markdown-source-mode'),
showSelectedIcon: false,
segments: const [
ButtonSegment(value: false, label: Text('Rendered')),
ButtonSegment(value: true, label: Text('Raw')),
],
selected: {_raw},
onSelectionChanged: (value) => _set(value.single),
);
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
control,
const SizedBox(height: 8),
if (_raw) widget.raw else ChatMarkdownView(text: widget.text),
],
);
}
}