mirror of
https://github.com/foss42/apidash.git
synced 2025-05-23 01:06:46 +08:00
✨ added: beautify json
This commit is contained in:
59
lib/widgets/raw.dart
Normal file
59
lib/widgets/raw.dart
Normal file
@ -0,0 +1,59 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:apidash/providers/providers.dart';
|
||||
import 'package:apidash/widgets/snackbars.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
class Raw extends StatelessWidget {
|
||||
const Raw({super.key, required this.body, this.style});
|
||||
|
||||
final String body;
|
||||
final TextStyle? style;
|
||||
|
||||
String? _formatJson(String data, {required ScaffoldMessengerState sm}) {
|
||||
try {
|
||||
final dynamic parsedJson = json.decode(body);
|
||||
|
||||
return const JsonEncoder.withIndent(' ').convert(parsedJson);
|
||||
} catch (e) {
|
||||
sm.hideCurrentSnackBar();
|
||||
sm.showSnackBar(getSnackBar("Failed to beautify JSON"));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var sm = ScaffoldMessenger.of(context);
|
||||
return Stack(
|
||||
children: [
|
||||
SingleChildScrollView(
|
||||
child: Consumer(builder: (context, ref, _) {
|
||||
return SelectableText(
|
||||
ref.watch(beautifyJsonProvider) ?? body,
|
||||
style: style,
|
||||
);
|
||||
}),
|
||||
),
|
||||
Align(
|
||||
alignment: Alignment.topRight,
|
||||
child: Consumer(builder: (context, ref, _) {
|
||||
return ref.watch(beautifyJsonProvider) != null
|
||||
? const SizedBox.shrink()
|
||||
: ElevatedButton(
|
||||
onPressed: () {
|
||||
ref.read(beautifyJsonProvider.notifier).state =
|
||||
_formatJson(body, sm: sm);
|
||||
},
|
||||
style: ElevatedButton.styleFrom(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 10),
|
||||
),
|
||||
child: const Text("Beautify"),
|
||||
);
|
||||
}),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user