62 lines
1.7 KiB
Dart
62 lines
1.7 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../../utils/clipboard_helper.dart';
|
|
|
|
class JsonViewer extends StatelessWidget {
|
|
final String title;
|
|
final Map<String, dynamic> data;
|
|
|
|
const JsonViewer({super.key, required this.title, required this.data});
|
|
|
|
@override
|
|
Widget build(BuildContext context) => Scaffold(
|
|
appBar: AppBar(title: Text(title)),
|
|
body: SingleChildScrollView(
|
|
scrollDirection: Axis.vertical,
|
|
child: Text(format(data)),
|
|
),
|
|
);
|
|
|
|
static final _encoder = const JsonEncoder.withIndent(' ');
|
|
|
|
static String format(Map<String, dynamic> jsonInput) =>
|
|
_encoder.convert(jsonInput);
|
|
|
|
static void asDialog(BuildContext context, Map<String, dynamic> dataMap) {
|
|
showDialog(
|
|
context: context,
|
|
builder: (dialogCtx) => AlertDialog(
|
|
scrollable: true,
|
|
title: const Row(
|
|
children: [Icon(Icons.bug_report_outlined), Text('Rohdaten')],
|
|
),
|
|
content: Text(JsonViewer.format(dataMap)),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => copyToClipboard(
|
|
dialogCtx,
|
|
JsonViewer.format(dataMap),
|
|
successMessage: 'Formatiertes JSON kopiert',
|
|
),
|
|
child: const Text('Kopieren'),
|
|
),
|
|
TextButton(
|
|
onPressed: () => copyToClipboard(
|
|
dialogCtx,
|
|
dataMap.toString(),
|
|
successMessage: 'Inline JSON kopiert',
|
|
),
|
|
child: const Text('Inline Kopieren'),
|
|
),
|
|
TextButton(
|
|
onPressed: () => Navigator.of(dialogCtx).pop(),
|
|
child: const Text('Schließen'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|