From 6e351745a5ad17646bd4098190e7fc7f3bb5c703 Mon Sep 17 00:00:00 2001 From: hawkbee1 Date: Wed, 23 Oct 2024 13:41:33 +0000 Subject: [PATCH] display of json from payload of SD-JWT --- .../widgets/credential_subject_data.dart | 154 +++++++++++++----- 1 file changed, 114 insertions(+), 40 deletions(-) diff --git a/lib/dashboard/home/tab_bar/credentials/detail/widgets/credential_subject_data.dart b/lib/dashboard/home/tab_bar/credentials/detail/widgets/credential_subject_data.dart index 914ba8d20..bbb1ed1a7 100644 --- a/lib/dashboard/home/tab_bar/credentials/detail/widgets/credential_subject_data.dart +++ b/lib/dashboard/home/tab_bar/credentials/detail/widgets/credential_subject_data.dart @@ -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) { - 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) { + 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 DisplayCredentialFieldMap({ + required bool showVertically, + required Map data, + required String type, + }) { + final List 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 DisplayCredentialFieldList({ + required bool showVertically, + required List data, + required String type, + }) { + final List 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 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, + ), + ), + ], + ); } }