127 lines
3.5 KiB
Dart
127 lines
3.5 KiB
Dart
import 'dart:typed_data';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../domain/artifact_preview.dart';
|
|
|
|
/// Renders fetched markdown text for the viewer. It takes the build context so
|
|
/// a themed markdown renderer can be supplied; the viewer's own default stays
|
|
/// plain selectable text for a standalone preview.
|
|
typedef ArtifactMarkdownBuilder =
|
|
Widget Function(BuildContext context, String text);
|
|
|
|
Widget defaultArtifactMarkdown(BuildContext context, String text) =>
|
|
SelectableText(text);
|
|
|
|
/// The viewer's preview surface for one page.
|
|
///
|
|
/// It draws only what was actually fetched: image bytes as an [Image], text as
|
|
/// selectable text, markdown through [markdownBuilder], and an explicit message
|
|
/// for unsupported/unavailable pages. It never substitutes a placeholder that
|
|
/// looks like a preview.
|
|
class ArtifactPreviewBody extends StatelessWidget {
|
|
const ArtifactPreviewBody({
|
|
super.key,
|
|
required this.plan,
|
|
required this.loading,
|
|
required this.markdownBuilder,
|
|
this.text,
|
|
this.bytes,
|
|
this.error,
|
|
});
|
|
|
|
final ArtifactPreviewPlan plan;
|
|
final bool loading;
|
|
final ArtifactMarkdownBuilder markdownBuilder;
|
|
final String? text;
|
|
final Uint8List? bytes;
|
|
final String? error;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
if (plan.kind == ArtifactPreviewKind.unavailable ||
|
|
plan.kind == ArtifactPreviewKind.unsupported) {
|
|
return _Message(plan.message);
|
|
}
|
|
if (loading) {
|
|
return const _Loading();
|
|
}
|
|
if (error != null) {
|
|
return _Message(error!);
|
|
}
|
|
switch (plan.kind) {
|
|
case ArtifactPreviewKind.image:
|
|
final data = bytes;
|
|
if (data == null) return const _Message('The image is empty.');
|
|
return Center(
|
|
child: Image.memory(
|
|
data,
|
|
fit: BoxFit.contain,
|
|
errorBuilder: (_, _, _) =>
|
|
const _Message('The image could not be decoded.'),
|
|
),
|
|
);
|
|
case ArtifactPreviewKind.text:
|
|
return _PreviewPadding(child: SelectableText(text ?? ''));
|
|
case ArtifactPreviewKind.markdown:
|
|
return _PreviewPadding(child: markdownBuilder(context, text ?? ''));
|
|
case ArtifactPreviewKind.unsupported:
|
|
case ArtifactPreviewKind.unavailable:
|
|
return const SizedBox.shrink();
|
|
}
|
|
}
|
|
}
|
|
|
|
class _Loading extends StatelessWidget {
|
|
const _Loading();
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Center(
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
const CircularProgressIndicator(),
|
|
const SizedBox(height: 12),
|
|
Text(
|
|
'Loading preview…',
|
|
style: Theme.of(context).textTheme.bodyMedium,
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
/// Preview text at its natural height; the viewer body owns the page scroll so
|
|
/// a long document and its actions scroll together instead of nested scrolls.
|
|
class _PreviewPadding extends StatelessWidget {
|
|
const _PreviewPadding({required this.child});
|
|
|
|
final Widget child;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Padding(
|
|
padding: const EdgeInsetsDirectional.fromSTEB(16, 8, 16, 16),
|
|
child: child,
|
|
);
|
|
}
|
|
}
|
|
|
|
class _Message extends StatelessWidget {
|
|
const _Message(this.message);
|
|
|
|
final String message;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Center(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(16),
|
|
child: Text(message, textAlign: TextAlign.center),
|
|
),
|
|
);
|
|
}
|
|
}
|