|
| 1 | +import 'dart:ui'; |
| 2 | + |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | +import 'package:flutter/rendering.dart'; |
| 5 | +import 'package:flutter_gen/gen_l10n/zulip_localizations.dart'; |
| 6 | + |
| 7 | +import '../api/model/model.dart'; |
| 8 | +import 'icons.dart'; |
| 9 | +import 'theme.dart'; |
| 10 | + |
| 11 | +class SwipableMessageRow extends StatefulWidget { |
| 12 | + const SwipableMessageRow({ |
| 13 | + super.key, |
| 14 | + required this.child, |
| 15 | + required this.message, |
| 16 | + this.movementDuration = const Duration(milliseconds: 200), |
| 17 | + }); |
| 18 | + |
| 19 | + final Duration movementDuration; |
| 20 | + final Widget child; |
| 21 | + final Message message; |
| 22 | + |
| 23 | + @override |
| 24 | + State<StatefulWidget> createState() => _SwipableMessageRowState(); |
| 25 | +} |
| 26 | + |
| 27 | +class _SwipableMessageRowState extends State<SwipableMessageRow> with TickerProviderStateMixin { |
| 28 | + @override |
| 29 | + void initState() { |
| 30 | + super.initState(); |
| 31 | + _controller = AnimationController( |
| 32 | + duration: widget.movementDuration, |
| 33 | + lowerBound: EditStateMarker.widthCollapsed, |
| 34 | + upperBound: EditStateMarker.widthExpanded, |
| 35 | + vsync: this) |
| 36 | + ..addListener(() => setState((){})); |
| 37 | + _slideAnimation = Tween<Offset>(begin: const Offset(0, 0), end: const Offset(1, 0)) |
| 38 | + .animate(_controller); |
| 39 | + } |
| 40 | + |
| 41 | + @override |
| 42 | + void dispose() { |
| 43 | + _controller.dispose(); |
| 44 | + super.dispose(); |
| 45 | + } |
| 46 | + |
| 47 | + late AnimationController _controller; |
| 48 | + late Animation<Offset> _slideAnimation; |
| 49 | + double get dragOffset => _controller.value - EditStateMarker.widthCollapsed; |
| 50 | + |
| 51 | + void _handleDragUpdate(DragUpdateDetails details) { |
| 52 | + _controller.value += details.delta.dx; |
| 53 | + } |
| 54 | + |
| 55 | + void _handleDragEnd(DragEndDetails details) { |
| 56 | + _controller.reverse(); |
| 57 | + } |
| 58 | + |
| 59 | + @override |
| 60 | + Widget build(BuildContext context) { |
| 61 | + final theme = Theme.of(context).extension<DesignVariables>()!; |
| 62 | + |
| 63 | + // TODO(#157): fix how star marker aligns with message content |
| 64 | + // Design from Figma at: |
| 65 | + // https://www.figma.com/file/1JTNtYo9memgW7vV6d0ygq/Zulip-Mobile?node-id=813%3A28817&mode=dev . |
| 66 | + var star = Padding(padding: const EdgeInsets.only(top: 5.5), |
| 67 | + child: Icon(ZulipIcons.star_filled, size: 16, color: theme.starColor)); |
| 68 | + final hasMarker = widget.message.editState != MessageEditState.none; |
| 69 | + |
| 70 | + final content = Stack( |
| 71 | + children: [ |
| 72 | + if (hasMarker) Positioned( |
| 73 | + left: 0, |
| 74 | + child: EditStateMarker( |
| 75 | + editState: widget.message.editState, |
| 76 | + animation: _controller)), |
| 77 | + Padding( |
| 78 | + // Adding [EditStateMarker.widthCollapsed] to the right padding |
| 79 | + // cancels out the left offset applied from the initial value of |
| 80 | + // [_slideAnimation] through [Transform.translate]. This is necessary |
| 81 | + // before we can add a padding of 16 pixels for the star. |
| 82 | + padding: const EdgeInsets.only(right: EditStateMarker.widthCollapsed + 16), |
| 83 | + child: Transform.translate( |
| 84 | + offset: _slideAnimation.value, |
| 85 | + child: widget.child)), |
| 86 | + if (widget.message.flags.contains(MessageFlag.starred)) |
| 87 | + Positioned( |
| 88 | + // Because _controller.value does not start from zero, we subtract |
| 89 | + // its initial value from it so the star is positioned correctly |
| 90 | + // in the beginning. |
| 91 | + right: 0 - (_controller.value - EditStateMarker.widthCollapsed), |
| 92 | + child: star), |
| 93 | + ], |
| 94 | + ); |
| 95 | + |
| 96 | + if (!hasMarker) return content; |
| 97 | + |
| 98 | + return GestureDetector( |
| 99 | + onHorizontalDragEnd: _handleDragEnd, |
| 100 | + onHorizontalDragUpdate: _handleDragUpdate, |
| 101 | + child: content, |
| 102 | + ); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +class EditStateMarker extends StatelessWidget { |
| 107 | + /// The minimum width of the marker. |
| 108 | + /// |
| 109 | + /// This is when no drag has been performed on the message row |
| 110 | + /// where only the moved/edited icon, not the text, is visible. |
| 111 | + static const double widthCollapsed = 16; |
| 112 | + |
| 113 | + /// The maximum width of the marker. |
| 114 | + /// |
| 115 | + /// This is typically wider than the colored pill when the marker is fully |
| 116 | + /// expanded. At that point only the blank space to the right of the colored |
| 117 | + /// block will grow until the marker reaches this width. |
| 118 | + static const double widthExpanded = 100; |
| 119 | + |
| 120 | + const EditStateMarker({ |
| 121 | + super.key, |
| 122 | + required MessageEditState editState, |
| 123 | + required Animation<double> animation, |
| 124 | + }) : _editState = editState, _animation = animation; |
| 125 | + |
| 126 | + final MessageEditState _editState; |
| 127 | + final Animation<double> _animation; |
| 128 | + |
| 129 | + double get _animationProgress => _animation.value / widthExpanded; |
| 130 | + double get _width => _animation.value; |
| 131 | + |
| 132 | + @override |
| 133 | + Widget build(BuildContext context) { |
| 134 | + final theme = DesignVariables.of(context); |
| 135 | + final zulipLocalizations = ZulipLocalizations.of(context); |
| 136 | + |
| 137 | + final IconData icon; |
| 138 | + final double iconSize; |
| 139 | + final String markerText; |
| 140 | + |
| 141 | + switch (_editState) { |
| 142 | + case MessageEditState.none: |
| 143 | + return const SizedBox(width: widthCollapsed); |
| 144 | + case MessageEditState.edited: |
| 145 | + icon = ZulipIcons.edited; |
| 146 | + iconSize = 14; |
| 147 | + markerText = zulipLocalizations.messageIsEdited; |
| 148 | + break; |
| 149 | + case MessageEditState.moved: |
| 150 | + icon = ZulipIcons.message_moved; |
| 151 | + iconSize = 8; |
| 152 | + markerText = zulipLocalizations.messageIsMoved; |
| 153 | + break; |
| 154 | + } |
| 155 | + |
| 156 | + var marker = Row( |
| 157 | + mainAxisAlignment: MainAxisAlignment.end, |
| 158 | + mainAxisSize: MainAxisSize.min, |
| 159 | + children: [ |
| 160 | + Flexible( |
| 161 | + fit: FlexFit.loose, |
| 162 | + // The trailing space serves as a padding between the marker text and |
| 163 | + // the icon without needing another element in the row. |
| 164 | + child: Text('$markerText ', |
| 165 | + overflow: TextOverflow.clip, |
| 166 | + softWrap: false, |
| 167 | + textAlign: TextAlign.center, |
| 168 | + style: TextStyle(fontSize: 15, color: Color.lerp( |
| 169 | + theme.textMarker.withAlpha(0), |
| 170 | + theme.textMarker, |
| 171 | + _animationProgress)))), |
| 172 | + // To match the Figma design, we cannot make the collapsed width of the |
| 173 | + // marker larger. We need to explicitly allow the icon to overflow. |
| 174 | + OverflowBox( |
| 175 | + fit: OverflowBoxFit.deferToChild, |
| 176 | + maxWidth: 10, |
| 177 | + child: Icon(icon, size: iconSize, color: Color.lerp( |
| 178 | + theme.textMarkerLight, |
| 179 | + theme.textMarker, |
| 180 | + _animationProgress)), |
| 181 | + ), |
| 182 | + ], |
| 183 | + ); |
| 184 | + |
| 185 | + return ConstrainedBox( |
| 186 | + constraints: BoxConstraints(maxWidth: _width), |
| 187 | + child: Container( |
| 188 | + margin: EdgeInsets.only(top: 4, left: lerpDouble(0, 10, _animationProgress)!), |
| 189 | + clipBehavior: Clip.hardEdge, |
| 190 | + decoration: BoxDecoration( |
| 191 | + borderRadius: BorderRadius.circular(2), |
| 192 | + color: Color.lerp( |
| 193 | + theme.bgMarker.withAlpha(0), |
| 194 | + theme.bgMarker, |
| 195 | + _animationProgress)), |
| 196 | + child: Padding( |
| 197 | + padding: const EdgeInsets.all(1.0), |
| 198 | + child: marker, |
| 199 | + ), |
| 200 | + ), |
| 201 | + ); |
| 202 | + } |
| 203 | +} |
0 commit comments