From 70cd0a6239f91d817b74daadb1024f97ceab9f56 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Wed, 31 May 2023 16:38:10 +0200 Subject: [PATCH] feat(neon): Implement RichObject Signed-off-by: jld3103 --- packages/app/pubspec.lock | 8 +++ .../neon/lib/src/widgets/rich_object.dart | 50 +++++++++++++++++++ packages/neon/neon/lib/widgets.dart | 1 + packages/neon/neon/pubspec.yaml | 1 + 4 files changed, 60 insertions(+) create mode 100644 packages/neon/neon/lib/src/widgets/rich_object.dart diff --git a/packages/app/pubspec.lock b/packages/app/pubspec.lock index 9c094fbb..9895f3ec 100644 --- a/packages/app/pubspec.lock +++ b/packages/app/pubspec.lock @@ -398,6 +398,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.3.5" + flutter_parsed_text: + dependency: transitive + description: + name: flutter_parsed_text + sha256: "529cf5793b7acdf16ee0f97b158d0d4ba0bf06e7121ef180abe1a5b59e32c1e2" + url: "https://pub.dev" + source: hosted + version: "2.2.1" flutter_plugin_android_lifecycle: dependency: transitive description: diff --git a/packages/neon/neon/lib/src/widgets/rich_object.dart b/packages/neon/neon/lib/src/widgets/rich_object.dart new file mode 100644 index 00000000..2453caac --- /dev/null +++ b/packages/neon/neon/lib/src/widgets/rich_object.dart @@ -0,0 +1,50 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_parsed_text/flutter_parsed_text.dart'; +import 'package:neon/src/blocs/accounts.dart'; +import 'package:neon/src/widgets/user_avatar.dart'; +import 'package:provider/provider.dart'; + +class NeonRichObject extends MatchText { + NeonRichObject({ + required final Map parameters, + }) : super( + onTap: (final _) {}, + pattern: '{(${parameters.keys.join('|')})}', + renderWidget: ({ + required final pattern, + required final text, + }) => + Builder( + builder: (final context) { + final richObject = parameters[text.substring(1, text.length - 1)] as Map; + switch (richObject['type']! as String) { + case 'user': + case 'call': + final account = Provider.of(context, listen: false).activeAccount.value!; + final id = richObject['id']! as String; + final isOwnUser = id == account.username; + return Chip( + avatar: NeonUserAvatar( + account: account, + username: id, + showStatus: false, + ), + label: Text( + richObject['name']! as String, + style: TextStyle( + color: isOwnUser + ? Theme.of(context).colorScheme.onPrimary + : Theme.of(context).colorScheme.onBackground, + ), + ), + backgroundColor: + isOwnUser ? Theme.of(context).colorScheme.primary : Theme.of(context).colorScheme.background, + ); + default: + debugPrint('Rich message type ${richObject['type']} not implemented yet'); + return Text(text); + } + }, + ), + ); +} diff --git a/packages/neon/neon/lib/widgets.dart b/packages/neon/neon/lib/widgets.dart index a1b6d690..7504b54b 100644 --- a/packages/neon/neon/lib/widgets.dart +++ b/packages/neon/neon/lib/widgets.dart @@ -4,4 +4,5 @@ export 'package:neon/src/widgets/image.dart'; export 'package:neon/src/widgets/linear_progress_indicator.dart'; export 'package:neon/src/widgets/list_view.dart'; export 'package:neon/src/widgets/relative_time.dart'; +export 'package:neon/src/widgets/rich_object.dart'; export 'package:neon/src/widgets/server_icon.dart'; diff --git a/packages/neon/neon/pubspec.yaml b/packages/neon/neon/pubspec.yaml index f6139ab9..a695b252 100644 --- a/packages/neon/neon/pubspec.yaml +++ b/packages/neon/neon/pubspec.yaml @@ -22,6 +22,7 @@ dependencies: sdk: flutter flutter_material_design_icons: ^1.0.0 flutter_native_splash: ^2.0.0 + flutter_parsed_text: ^2.0.0 flutter_svg: ^2.0.0 flutter_zxing: ^1.0.0 go_router: ^12.0.0