|
| 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 | + vsync: this); |
| 34 | + _animation = Tween<double>(begin: 0, end: 1).animate(_controller) |
| 35 | + ..addListener(() { |
| 36 | + setState(() {}); |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + @override |
| 41 | + void dispose() { |
| 42 | + _controller.dispose(); |
| 43 | + super.dispose(); |
| 44 | + } |
| 45 | + |
| 46 | + late AnimationController _controller; |
| 47 | + late Animation<double> _animation; |
| 48 | + |
| 49 | + double _dragExtent = 0; |
| 50 | + double get _overallDragExtent => context.size!.width / 2; |
| 51 | + |
| 52 | + void _handleDragStart(DragStartDetails details) { |
| 53 | + _dragExtent = 0; |
| 54 | + } |
| 55 | + |
| 56 | + void _handleDragUpdate(DragUpdateDetails details) { |
| 57 | + _dragExtent += details.delta.dx; |
| 58 | + _controller.value = _dragExtent / _overallDragExtent; |
| 59 | + } |
| 60 | + |
| 61 | + void _handleDragEnd(DragEndDetails details) { |
| 62 | + _controller.reverse(); |
| 63 | + } |
| 64 | + |
| 65 | + @override |
| 66 | + Widget build(BuildContext context) { |
| 67 | + // on start: |
| 68 | + // remember drag start position |
| 69 | + // calculate relative position x |
| 70 | + // |
| 71 | + // on update: |
| 72 | + // the marker rect follows the pointer until it becomes a bit wider |
| 73 | + // than the maximum width (> |
| 74 | + |
| 75 | + // Initial state |
| 76 | + // No text, no background, lighter colored icon |
| 77 | + // Final state |
| 78 | + // Text, light blue background, darker colored icon, text pushed to the right |
| 79 | + final theme = Theme.of(context).extension<DesignVariables>()!; |
| 80 | + |
| 81 | + var marker = EditStateMarker(editState: widget.message.editState, animation: _animation); |
| 82 | + |
| 83 | + final content = Stack( |
| 84 | + children: [ |
| 85 | + marker, |
| 86 | + Row( |
| 87 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 88 | + children: [ |
| 89 | + SizedBox(width: marker.marginLeft + marker.marginRight + marker.width), |
| 90 | + Expanded(child: widget.child), |
| 91 | + SizedBox(width: 16, |
| 92 | + child: widget.message.flags.contains(MessageFlag.starred) |
| 93 | + // TODO(#157): fix how star marker aligns with message content |
| 94 | + // Design from Figma at: |
| 95 | + // https://www.figma.com/file/1JTNtYo9memgW7vV6d0ygq/Zulip-Mobile?node-id=813%3A28817&mode=dev . |
| 96 | + ? Padding(padding: const EdgeInsets.only(top: 5.5), |
| 97 | + child: Icon(ZulipIcons.star_filled, size: 16, color: theme.starColor)) |
| 98 | + : null), |
| 99 | + ]), |
| 100 | + ], |
| 101 | + ); |
| 102 | + |
| 103 | + if (widget.message.editState == MessageEditState.none) return content; |
| 104 | + |
| 105 | + return GestureDetector( |
| 106 | + onHorizontalDragStart: _handleDragStart, |
| 107 | + onHorizontalDragEnd: _handleDragEnd, |
| 108 | + onHorizontalDragUpdate: _handleDragUpdate, |
| 109 | + child: content, |
| 110 | + ); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +class EditStateMarker extends StatelessWidget { |
| 115 | + const EditStateMarker({ |
| 116 | + super.key, |
| 117 | + required this.editState, |
| 118 | + required Animation<double> animation, |
| 119 | + }) : _animation = animation; |
| 120 | + |
| 121 | + final Animation<double> _animation; |
| 122 | + final MessageEditState editState; |
| 123 | + |
| 124 | + static const double _marginLeftCollapsed = 5; |
| 125 | + static const double _marignRightCollapsed = 0; |
| 126 | + static const double _widthCollapsed = 10; |
| 127 | + |
| 128 | + static const double _marginLeftExpanded = 9; |
| 129 | + static const double _marginRightExpanded = 3; |
| 130 | + static const double _widthExpanded = 60; |
| 131 | + static const double _markerFontSize = 15; |
| 132 | + |
| 133 | + double get marginLeft => lerpDouble( |
| 134 | + _marginLeftCollapsed, |
| 135 | + _marginLeftExpanded, |
| 136 | + _animation.value)!; |
| 137 | + double get marginRight => lerpDouble( |
| 138 | + _marignRightCollapsed, |
| 139 | + _marginRightExpanded, |
| 140 | + _animation.value)!; |
| 141 | + double get width => lerpDouble( |
| 142 | + _widthCollapsed, |
| 143 | + _widthExpanded, |
| 144 | + _animation.value)!; |
| 145 | + |
| 146 | + @override |
| 147 | + Widget build(BuildContext context) { |
| 148 | + final DesignVariables theme = DesignVariables.of(context); |
| 149 | + final IconData icon; |
| 150 | + final double iconSize; |
| 151 | + final String markerText; |
| 152 | + final double iconRightMargin; |
| 153 | + final zulipLocalizations = ZulipLocalizations.of(context); |
| 154 | + |
| 155 | + switch (editState) { |
| 156 | + case MessageEditState.none: |
| 157 | + return const SizedBox(width: _marginLeftCollapsed + _widthCollapsed + _marignRightCollapsed); |
| 158 | + case MessageEditState.edited: |
| 159 | + icon = ZulipIcons.edited; |
| 160 | + iconSize = 14; |
| 161 | + iconRightMargin = 0; |
| 162 | + markerText = zulipLocalizations.messageIsEdited; |
| 163 | + break; |
| 164 | + case MessageEditState.moved: |
| 165 | + icon = ZulipIcons.message_moved; |
| 166 | + iconSize = 8; |
| 167 | + iconRightMargin = 1; |
| 168 | + markerText = zulipLocalizations.messageIsMoved; |
| 169 | + break; |
| 170 | + } |
| 171 | + |
| 172 | + var marker = Row( |
| 173 | + mainAxisAlignment: MainAxisAlignment.end, |
| 174 | + children: [ |
| 175 | + // Apply animation for both the text and the icon |
| 176 | + Expanded(child: Text(markerText, |
| 177 | + overflow: TextOverflow.clip, |
| 178 | + softWrap: false, |
| 179 | + textAlign: TextAlign.center, |
| 180 | + style: TextStyle(fontSize: _markerFontSize, color: Color.lerp( |
| 181 | + theme.textMarker.withAlpha(0), |
| 182 | + theme.textMarker, |
| 183 | + _animation.value)))), |
| 184 | + OverflowBox( |
| 185 | + fit: OverflowBoxFit.deferToChild, |
| 186 | + maxWidth: _widthCollapsed, |
| 187 | + child: Icon(icon, size: iconSize, color: Color.lerp( |
| 188 | + theme.textMarkerLight, |
| 189 | + theme.textMarker, |
| 190 | + _animation.value)), |
| 191 | + ), |
| 192 | + SizedBox(width: iconRightMargin * _animation.value), |
| 193 | + ], |
| 194 | + ); |
| 195 | + |
| 196 | + return Padding( |
| 197 | + padding: EdgeInsets.only(top: 4, left: marginLeft), |
| 198 | + child: Container( |
| 199 | + height: _markerFontSize * 1.5, |
| 200 | + width: width, |
| 201 | + clipBehavior: Clip.hardEdge, |
| 202 | + decoration: BoxDecoration( |
| 203 | + borderRadius: BorderRadius.circular(2), |
| 204 | + color: Color.lerp( |
| 205 | + theme.bgMarker.withAlpha(0), |
| 206 | + theme.bgMarker, |
| 207 | + _animation.value)), |
| 208 | + child: marker, |
| 209 | + )); |
| 210 | + } |
| 211 | +} |
0 commit comments