-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathbase_json_view.dart
More file actions
55 lines (46 loc) · 1.32 KB
/
base_json_view.dart
File metadata and controls
55 lines (46 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_json_view/flutter_json_view.dart';
import 'package:flutter_json_view/src/utils/utils.dart';
class BaseJsonView extends StatefulWidget {
const BaseJsonView({
Key? key,
required this.jsonViewTheme,
this.jsonData,
required this.onError,
this.assetsPath,
}) : super(key: key);
final String? jsonData;
final Widget onError;
final JsonViewTheme jsonViewTheme;
final String? assetsPath;
@override
State<BaseJsonView> createState() => _BaseJsonViewState();
}
class _BaseJsonViewState extends State<BaseJsonView> {
String? _assetsJsonString;
@override
void initState() {
_loadAssetsJson();
super.initState();
}
Future<void> _loadAssetsJson() async {
if (widget.assetsPath != null) {
final json =
await AssetLoader.getAssetJson(widget.assetsPath!, widget.onError);
_assetsJsonString = _encoder.convert(json);
setState(() {});
}
}
@override
Widget build(BuildContext context) {
if (widget.jsonData != null || _assetsJsonString != null) {
return SelectableText(
(widget.jsonData ?? _assetsJsonString)!,
style: widget.jsonViewTheme.defaultTextStyle,
);
}
return const SizedBox();
}
}
const _encoder = JsonEncoder.withIndent(' ');