From 64df124e37c78758d500031ff1af01f255ae9861 Mon Sep 17 00:00:00 2001 From: Leandro Borges Ferreira Date: Wed, 8 Feb 2023 12:05:47 +0100 Subject: [PATCH] Extracting reactions align --- .../message_actions_modal.dart | 54 ++----------------- .../reactions/reactions_align.dart | 54 +++++++++++++++++++ 2 files changed, 57 insertions(+), 51 deletions(-) create mode 100644 packages/stream_chat_flutter/lib/src/message_widget/reactions/reactions_align.dart diff --git a/packages/stream_chat_flutter/lib/src/message_actions_modal/message_actions_modal.dart b/packages/stream_chat_flutter/lib/src/message_actions_modal/message_actions_modal.dart index 3e1260d5..18a49c95 100644 --- a/packages/stream_chat_flutter/lib/src/message_actions_modal/message_actions_modal.dart +++ b/packages/stream_chat_flutter/lib/src/message_actions_modal/message_actions_modal.dart @@ -2,6 +2,7 @@ import 'dart:ui'; import 'package:flutter/material.dart' hide ButtonStyle; import 'package:stream_chat_flutter/src/message_actions_modal/mam_widgets.dart'; +import 'package:stream_chat_flutter/src/message_widget/reactions/reactions_align.dart'; import 'package:stream_chat_flutter/stream_chat_flutter.dart'; /// {@template messageActionsModal} @@ -135,8 +136,9 @@ class _MessageActionsModalState extends State { builder: (context, constraints) { return Align( alignment: Alignment( - _calculateReactionsHorizontalAlignmentValue( + calculateReactionsHorizontalAlignmentValue( user, + widget.message, divFactor, shiftFactor, constraints, @@ -274,56 +276,6 @@ class _MessageActionsModalState extends State { ); } - double _calculateReactionsHorizontalAlignmentValue( - User? user, - num divFactor, - double shiftFactor, - BoxConstraints constraints, - ) { - var result = 0.0; - - final maxWidth = constraints.maxWidth; - final maxHeight = constraints.maxHeight; - - print('INFO - max width: $maxWidth. ' - 'maxHeight: $maxHeight ' - 'shiftFactor: $shiftFactor ' - 'divFactor: $divFactor'); - - if (user?.id == widget.message.user?.id) { - if (divFactor >= 1.0) { - /* - This is an empiric value. This number tries to approximate all the - offset necessary for the position of reaction look the best way - possible. - */ - const constant = 1500; - - result = shiftFactor - maxWidth / constant; - } else { - // Small messages, it is simpler to align then. - result = 1.2 - divFactor; - } - } else { - if (divFactor >= 1.0) { - result = shiftFactor + 0.2; - } else { - result = -(1.2 - divFactor); - } - } - - print('INFO - result: $result'); - - // Ensure reactions don't get pushed past the edge of the screen. - // - // Hacky!!! Needs improvement!!! - if (result > 1.0) { - return 1; - } else { - return result; - } - } - InkWell _buildCustomAction( BuildContext context, StreamMessageAction messageAction, diff --git a/packages/stream_chat_flutter/lib/src/message_widget/reactions/reactions_align.dart b/packages/stream_chat_flutter/lib/src/message_widget/reactions/reactions_align.dart new file mode 100644 index 00000000..e6e81eb6 --- /dev/null +++ b/packages/stream_chat_flutter/lib/src/message_widget/reactions/reactions_align.dart @@ -0,0 +1,54 @@ +import 'package:flutter/material.dart'; +import 'package:stream_chat_flutter/stream_chat_flutter.dart'; + +/// Document here!! +double calculateReactionsHorizontalAlignmentValue( + User? user, + Message message, + num divFactor, + double shiftFactor, + BoxConstraints constraints, + ) { + var result = 0.0; + + final maxWidth = constraints.maxWidth; + final maxHeight = constraints.maxHeight; + + print('INFO - max width: $maxWidth. ' + 'maxHeight: $maxHeight ' + 'shiftFactor: $shiftFactor ' + 'divFactor: $divFactor'); + + if (user?.id == message.user?.id) { + if (divFactor >= 1.0) { + /* + This is an empiric value. This number tries to approximate all the + offset necessary for the position of reaction look the best way + possible. + */ + const constant = 1300; + + result = shiftFactor - maxWidth / constant; + } else { + // Small messages, it is simpler to align then. + result = 1.2 - divFactor; + } + } else { + if (divFactor >= 1.0) { + result = shiftFactor + 0.2; + } else { + result = -(1.2 - divFactor); + } + } + + print('INFO - result: $result'); + + // Ensure reactions don't get pushed past the edge of the screen. + // + // Hacky!!! Needs improvement!!! + if (result > 1.0) { + return 1; + } else { + return result; + } +}