notification_item.dart 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import 'package:appflowy/generated/flowy_svgs.g.dart';
  2. import 'package:appflowy/generated/locale_keys.g.dart';
  3. import 'package:appflowy/workspace/application/settings/appearance/appearance_cubit.dart';
  4. import 'package:appflowy/workspace/application/settings/date_time/date_format_ext.dart';
  5. import 'package:appflowy_popover/appflowy_popover.dart';
  6. import 'package:easy_localization/easy_localization.dart';
  7. import 'package:fixnum/fixnum.dart';
  8. import 'package:flowy_infra/theme_extension.dart';
  9. import 'package:flowy_infra_ui/flowy_infra_ui.dart';
  10. import 'package:flutter/material.dart';
  11. import 'package:provider/provider.dart';
  12. class NotificationItem extends StatefulWidget {
  13. const NotificationItem({
  14. super.key,
  15. required this.reminderId,
  16. required this.title,
  17. required this.scheduled,
  18. required this.body,
  19. required this.isRead,
  20. this.readOnly = false,
  21. this.onAction,
  22. this.onDelete,
  23. this.onReadChanged,
  24. });
  25. final String reminderId;
  26. final String title;
  27. final Int64 scheduled;
  28. final String body;
  29. final bool isRead;
  30. final bool readOnly;
  31. final VoidCallback? onAction;
  32. final VoidCallback? onDelete;
  33. final void Function(bool isRead)? onReadChanged;
  34. @override
  35. State<NotificationItem> createState() => _NotificationItemState();
  36. }
  37. class _NotificationItemState extends State<NotificationItem> {
  38. final PopoverMutex mutex = PopoverMutex();
  39. bool _isHovering = false;
  40. @override
  41. Widget build(BuildContext context) {
  42. return MouseRegion(
  43. onEnter: (_) => _onHover(true),
  44. onExit: (_) => _onHover(false),
  45. cursor: widget.onAction != null
  46. ? SystemMouseCursors.click
  47. : MouseCursor.defer,
  48. child: Stack(
  49. children: [
  50. GestureDetector(
  51. onTap: widget.onAction,
  52. child: Opacity(
  53. opacity: widget.isRead && !widget.readOnly ? 0.5 : 1,
  54. child: Container(
  55. padding: const EdgeInsets.all(10),
  56. decoration: BoxDecoration(
  57. borderRadius: const BorderRadius.all(Radius.circular(6)),
  58. color: _isHovering && widget.onAction != null
  59. ? AFThemeExtension.of(context).lightGreyHover
  60. : Colors.transparent,
  61. ),
  62. child: Row(
  63. crossAxisAlignment: CrossAxisAlignment.start,
  64. children: [
  65. Stack(
  66. children: [
  67. const FlowySvg(FlowySvgs.time_s, size: Size.square(20)),
  68. if (!widget.isRead && !widget.readOnly)
  69. Positioned(
  70. bottom: 1,
  71. right: 1,
  72. child: DecoratedBox(
  73. decoration: BoxDecoration(
  74. shape: BoxShape.circle,
  75. color: AFThemeExtension.of(context).warning,
  76. ),
  77. child: const SizedBox(height: 8, width: 8),
  78. ),
  79. ),
  80. ],
  81. ),
  82. const HSpace(10),
  83. Expanded(
  84. child: Column(
  85. crossAxisAlignment: CrossAxisAlignment.start,
  86. mainAxisAlignment: MainAxisAlignment.center,
  87. children: [
  88. Row(
  89. children: [
  90. FlowyText.semibold(
  91. widget.title,
  92. fontSize: 14,
  93. ),
  94. const HSpace(8),
  95. FlowyText.regular(
  96. _scheduledString(widget.scheduled),
  97. fontSize: 10,
  98. ),
  99. ],
  100. ),
  101. const VSpace(5),
  102. FlowyText.regular(widget.body, maxLines: 4),
  103. ],
  104. ),
  105. ),
  106. ],
  107. ),
  108. ),
  109. ),
  110. ),
  111. if (_isHovering && !widget.readOnly)
  112. Positioned(
  113. right: 4,
  114. top: 4,
  115. child: NotificationItemActions(
  116. isRead: widget.isRead,
  117. onDelete: widget.onDelete,
  118. onReadChanged: widget.onReadChanged,
  119. ),
  120. ),
  121. ],
  122. ),
  123. );
  124. }
  125. String _scheduledString(Int64 secondsSinceEpoch) => context
  126. .read<AppearanceSettingsCubit>()
  127. .state
  128. .dateFormat
  129. .formatDate(
  130. DateTime.fromMillisecondsSinceEpoch(secondsSinceEpoch.toInt() * 1000),
  131. true,
  132. );
  133. void _onHover(bool isHovering) => setState(() => _isHovering = isHovering);
  134. }
  135. class NotificationItemActions extends StatelessWidget {
  136. const NotificationItemActions({
  137. super.key,
  138. required this.isRead,
  139. this.onDelete,
  140. this.onReadChanged,
  141. });
  142. final bool isRead;
  143. final VoidCallback? onDelete;
  144. final void Function(bool isRead)? onReadChanged;
  145. @override
  146. Widget build(BuildContext context) {
  147. return Container(
  148. height: 30,
  149. decoration: BoxDecoration(
  150. color: Theme.of(context).cardColor,
  151. border: Border.all(color: Theme.of(context).dividerColor),
  152. borderRadius: BorderRadius.circular(6),
  153. ),
  154. child: IntrinsicHeight(
  155. child: Row(
  156. children: [
  157. if (isRead) ...[
  158. FlowyIconButton(
  159. height: 28,
  160. tooltipText:
  161. LocaleKeys.reminderNotification_tooltipMarkUnread.tr(),
  162. icon: const FlowySvg(FlowySvgs.restore_s),
  163. onPressed: () => onReadChanged?.call(false),
  164. ),
  165. ] else ...[
  166. FlowyIconButton(
  167. height: 28,
  168. tooltipText:
  169. LocaleKeys.reminderNotification_tooltipMarkRead.tr(),
  170. icon: const FlowySvg(FlowySvgs.messages_s),
  171. onPressed: () => onReadChanged?.call(true),
  172. ),
  173. ],
  174. VerticalDivider(
  175. width: 3,
  176. thickness: 1,
  177. indent: 2,
  178. endIndent: 2,
  179. color: Theme.of(context).dividerColor,
  180. ),
  181. FlowyIconButton(
  182. height: 28,
  183. tooltipText: LocaleKeys.reminderNotification_tooltipDelete.tr(),
  184. icon: const FlowySvg(FlowySvgs.delete_s),
  185. onPressed: onDelete,
  186. ),
  187. ],
  188. ),
  189. ),
  190. );
  191. }
  192. }