Skip to content

Commit

Permalink
display of json from payload of SD-JWT
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkbee1 committed Oct 23, 2024
1 parent 27cf548 commit 6e35174
Showing 1 changed file with 114 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,56 +134,130 @@ class DisplayCredentialField extends StatelessWidget {

@override
Widget build(BuildContext context) {
late Widget widget;
Widget? widget;
try {
final json = jsonDecode(data.toString());
if (json is Map<String, dynamic>) {
widget = Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title ?? '',
style: Theme.of(context).textTheme.bodyMedium!.copyWith(
color: Theme.of(context).colorScheme.onSurface,
fontWeight: FontWeight.bold,
),
),
Padding(
padding: const EdgeInsets.only(left: 8),
child: DisplayCredentialFieldMap(
showVertically: showVertically,
data: data,
type: type,
),
),
],
widget = IndentedCredentialFields(
title: title,
children: DisplayCredentialFieldMap(
showVertically: showVertically,
data: json,
type: type,
),
);
}

if (json is List<dynamic>) {
widget = IndentedCredentialFields(
title: title,
children: DisplayCredentialFieldList(
showVertically: showVertically,
data: json,
type: type,
),
);
}
} catch (e) {
widget = CredentialField(
padding: const EdgeInsets.only(top: 10),
title: title,
value: data.toString(),
titleColor: Theme.of(context).colorScheme.onSurface,
valueColor: Theme.of(context).colorScheme.onSurface,
showVertically: showVertically,
type: type,
/// Empty catch because data is not a valid json and
/// the return of the function will take care of
/// this usecase (widget is null)
}

return widget ??
CredentialField(
padding: const EdgeInsets.only(top: 10),
title: title,
value: data.toString(),
titleColor: Theme.of(context).colorScheme.onSurface,
valueColor: Theme.of(context).colorScheme.onSurface,
showVertically: showVertically,
type: type,
);
}

List<Widget> DisplayCredentialFieldMap({
required bool showVertically,
required Map<String, dynamic> data,
required String type,
}) {
final List<Widget> column = [];

/// for each element in Map data, call DisplayCredentialField
for (final element in data.entries) {
column.add(
DisplayCredentialField(
title: element.key,
data: element.value is String
? element.value
: jsonEncode(element.value),
type: type,
showVertically: showVertically,
),
);
}

return CredentialField(
padding: const EdgeInsets.only(top: 10),
title: title,
value: data.toString(),
titleColor: Theme.of(context).colorScheme.onSurface,
valueColor: Theme.of(context).colorScheme.onSurface,
showVertically: showVertically,
type: type,
);
return column;
}

List<Widget> DisplayCredentialFieldList({
required bool showVertically,
required List<dynamic> data,
required String type,
}) {
final List<Widget> column = [];

/// for each element in Map data, call DisplayCredentialField
for (final element in data) {
column.add(
DisplayCredentialField(
title: null,
data: element is String ? element : jsonEncode(element),
type: type,
showVertically: showVertically,
),
);
}

return column;
}
}

class IndentedCredentialFields extends StatelessWidget {
const IndentedCredentialFields({
super.key,
required this.children,
this.title,
});

Widget DisplayCredentialFieldMap(
{required bool showVertically, required data, required String type}) {
return Text('DisplayCredentialFieldMap');
final List<Widget> children;
final String? title;
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (title != null)
Padding(
padding: const EdgeInsets.only(top: 10),
child: Text(
title!,
style: Theme.of(context).textTheme.bodyMedium!.copyWith(
color: Theme.of(context).colorScheme.onSurface,
fontWeight: FontWeight.bold,
),
),
)
else
const SizedBox.shrink(),
Padding(
padding: const EdgeInsets.only(left: 8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: children,
),
),
],
);
}
}

0 comments on commit 6e35174

Please sign in to comment.