added: beautify json

This commit is contained in:
Priyaranjan
2024-01-01 22:11:07 +05:30
parent ef4e3d22f1
commit 36cfbfb97e
2 changed files with 85 additions and 13 deletions

59
lib/widgets/raw.dart Normal file
View 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"),
);
}),
),
],
);
}
}

View File

@ -1,3 +1,4 @@
import 'package:apidash/widgets/raw.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:http_parser/http_parser.dart'; import 'package:http_parser/http_parser.dart';
@ -407,22 +408,31 @@ class _BodySuccessState extends State<BodySuccess> {
return Padding( return Padding(
padding: kP10, padding: kP10,
child: Column( child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [ children: [
Row( Wrap(
alignment: WrapAlignment.spaceBetween,
children: [ children: [
(widget.options == kRawBodyViewOptions) (widget.options == kRawBodyViewOptions)
? const SizedBox() ? const SizedBox()
: SegmentedButton<ResponseBodyView>( : SegmentedButton<ResponseBodyView>(
selectedIcon: Icon( selectedIcon: showLabel
kResponseBodyViewIcons[currentSeg]![kKeyIcon]), ? Icon(
kResponseBodyViewIcons[currentSeg]![kKeyIcon],
)
: null,
segments: widget.options segments: widget.options
.map<ButtonSegment<ResponseBodyView>>( .map<ButtonSegment<ResponseBodyView>>(
(e) => ButtonSegment<ResponseBodyView>( (e) => ButtonSegment<ResponseBodyView>(
value: e, value: e,
label: Text( label: showLabel
kResponseBodyViewIcons[e]![kKeyName]), ? Text(
kResponseBodyViewIcons[e]![kKeyName],
)
: null,
icon: Icon( icon: Icon(
kResponseBodyViewIcons[e]![kKeyIcon]), kResponseBodyViewIcons[e]![kKeyIcon],
),
), ),
) )
.toList(), .toList(),
@ -434,7 +444,10 @@ class _BodySuccessState extends State<BodySuccess> {
}); });
}, },
), ),
const Spacer(), // const Spacer(),
Row(
mainAxisSize: MainAxisSize.min,
children: [
kCodeRawBodyViewOptions.contains(currentSeg) kCodeRawBodyViewOptions.contains(currentSeg)
? CopyButton( ? CopyButton(
toCopy: widget.body, toCopy: widget.body,
@ -447,6 +460,8 @@ class _BodySuccessState extends State<BodySuccess> {
showLabel: showLabel, showLabel: showLabel,
), ),
], ],
)
],
), ),
kVSpacer10, kVSpacer10,
Visibility( Visibility(
@ -491,15 +506,13 @@ class _BodySuccessState extends State<BodySuccess> {
width: double.maxFinite, width: double.maxFinite,
padding: kP8, padding: kP8,
decoration: textContainerdecoration, decoration: textContainerdecoration,
child: SingleChildScrollView( child: Raw(
child: SelectableText( body: widget.body,
widget.body,
style: kCodeStyle, style: kCodeStyle,
), ),
), ),
), ),
), ),
),
], ],
), ),
); );