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

74 lines
2.2 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) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
MarkdownModeSwitch(raw: _raw, onChanged: _set),
const SizedBox(height: 8),
if (_raw) widget.raw else ChatMarkdownView(text: widget.text),
],
);
}
}
/// The Rendered | Raw switch for markdown text; one switch can govern a
/// whole page of fields.
class MarkdownModeSwitch extends StatelessWidget {
const MarkdownModeSwitch({
super.key,
required this.raw,
required this.onChanged,
});
final bool raw;
final ValueChanged<bool> onChanged;
@override
Widget build(BuildContext context) {
final cupertino = usesCupertinoHost(context);
return cupertino
? CupertinoSlidingSegmentedControl<bool>(
key: const Key('markdown-source-mode'),
groupValue: raw,
onValueChanged: (value) => onChanged(value ?? 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) => onChanged(value.single),
);
}
}