134 lines
4.2 KiB
Dart
134 lines
4.2 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
|
|
import 'run_detail_content.dart';
|
|
import 'run_detail_cupertino.dart';
|
|
import 'run_detail_material.dart';
|
|
import 'runs_controller.dart';
|
|
|
|
class RunDetailView extends StatefulWidget {
|
|
const RunDetailView({
|
|
super.key,
|
|
required this.controller,
|
|
required this.onBack,
|
|
});
|
|
|
|
final RunsController controller;
|
|
final VoidCallback onBack;
|
|
|
|
@override
|
|
State<RunDetailView> createState() => _RunDetailViewState();
|
|
}
|
|
|
|
class _RunDetailViewState extends State<RunDetailView> {
|
|
bool _copying = false;
|
|
String? _copyStatus;
|
|
|
|
@override
|
|
Widget build(BuildContext context) => AnimatedBuilder(
|
|
animation: widget.controller,
|
|
builder: (context, _) {
|
|
final cupertino = _usesCupertino(context);
|
|
final run = widget.controller.detail;
|
|
if (run == null) return _placeholder(context, cupertino: cupertino);
|
|
final prompt = detailPrompt(run);
|
|
return cupertino
|
|
? buildCupertinoRunDetail(
|
|
context: context,
|
|
run: run,
|
|
detailLoading: widget.controller.detailLoading,
|
|
error: widget.controller.error,
|
|
copying: _copying,
|
|
copyStatus: _copyStatus,
|
|
onBack: widget.onBack,
|
|
onCopy: () => _copyPrompt(prompt ?? '', cupertino: true),
|
|
)
|
|
: buildMaterialRunDetail(
|
|
context: context,
|
|
run: run,
|
|
detailLoading: widget.controller.detailLoading,
|
|
error: widget.controller.error,
|
|
copying: _copying,
|
|
copyStatus: _copyStatus,
|
|
onBack: widget.onBack,
|
|
onCopy: () => _copyPrompt(prompt ?? '', cupertino: false),
|
|
);
|
|
},
|
|
);
|
|
|
|
/// CupertinoApp supplies CupertinoLocalizations only, while MaterialApp
|
|
/// supplies MaterialLocalizations. This distinguishes the two hosts without
|
|
/// assuming a desktop browser's target platform.
|
|
bool _usesCupertino(BuildContext context) =>
|
|
Localizations.of<MaterialLocalizations>(context, MaterialLocalizations) ==
|
|
null;
|
|
|
|
Widget _placeholder(BuildContext context, {required bool cupertino}) {
|
|
if (widget.controller.detailLoading) {
|
|
return Center(
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
if (cupertino)
|
|
const CupertinoActivityIndicator()
|
|
else
|
|
const CircularProgressIndicator(),
|
|
const SizedBox(height: 12),
|
|
const Text('Loading run details…'),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
final message = widget.controller.error ?? 'Run details unavailable.';
|
|
return Center(
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 24),
|
|
child: Text(message, textAlign: TextAlign.center),
|
|
),
|
|
const SizedBox(height: 12),
|
|
if (cupertino)
|
|
CupertinoButton(
|
|
onPressed: widget.onBack,
|
|
child: const Text('Back to runs'),
|
|
)
|
|
else
|
|
FilledButton(
|
|
onPressed: widget.onBack,
|
|
child: const Text('Back to runs'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
/// Copies the stored prompt byte-for-byte. A refusal is surfaced, never
|
|
/// replaced by copying a title or the formatted detail.
|
|
Future<void> _copyPrompt(String prompt, {required bool cupertino}) async {
|
|
if (_copying) return;
|
|
setState(() => _copying = true);
|
|
String message;
|
|
try {
|
|
await Clipboard.setData(ClipboardData(text: prompt));
|
|
message = 'Prompt copied to clipboard.';
|
|
} catch (_) {
|
|
message = 'Could not copy prompt to clipboard.';
|
|
}
|
|
if (!mounted) return;
|
|
final scaffold = cupertino ? null : Scaffold.maybeOf(context);
|
|
final inline = cupertino || scaffold == null;
|
|
setState(() {
|
|
_copying = false;
|
|
_copyStatus = inline ? message : null;
|
|
});
|
|
if (!cupertino && scaffold != null) {
|
|
ScaffoldMessenger.maybeOf(
|
|
context,
|
|
)?.showSnackBar(SnackBar(content: Text(message)));
|
|
}
|
|
}
|
|
}
|