Skip to content

Commit be19a7d

Browse files
committed
ui: Support edited/moved marker swipe animation.
This adds full support to the edited/moved marker feature by allowing the user to expand the edited/moved marker to show a helper text on a colored block in the background. The marker retracts as soon as the user releases the touch. Fixes #171. Signed-off-by: Zixuan James Li <[email protected]>
1 parent ec025b0 commit be19a7d

File tree

3 files changed

+156
-20
lines changed

3 files changed

+156
-20
lines changed

assets/l10n/app_en.arb

+8
Original file line numberDiff line numberDiff line change
@@ -483,5 +483,13 @@
483483
"notifSelfUser": "You",
484484
"@notifSelfUser": {
485485
"description": "Display name for the user themself, to show after replying in an Android notification"
486+
},
487+
"messageIsEdited": "Edited",
488+
"@messageIsEdited": {
489+
"description": "Text that appears on a marker next to an edited message."
490+
},
491+
"messageIsMoved": "Moved",
492+
"@messageIsMoved": {
493+
"description": "Text that appears on a marker next to a moved message."
486494
}
487495
}

lib/widgets/edit_state_marker.dart

+134-20
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
import 'dart:ui';
2+
13
import 'package:flutter/material.dart';
4+
import 'package:flutter/rendering.dart';
5+
import 'package:flutter_gen/gen_l10n/zulip_localizations.dart';
26

37
import '../api/model/model.dart';
48
import 'icons.dart';
59
import 'text.dart';
610
import 'theme.dart';
711

8-
class EditStateMarker extends StatelessWidget {
12+
class EditStateMarker extends StatefulWidget {
913
const EditStateMarker({
1014
super.key,
1115
required this.message,
@@ -15,53 +19,163 @@ class EditStateMarker extends StatelessWidget {
1519
final Message message;
1620
final List<Widget> children;
1721

22+
@override
23+
State<StatefulWidget> createState() => _EditStateMarkerState();
24+
}
25+
26+
class _EditStateMarkerState extends State<EditStateMarker> with TickerProviderStateMixin {
27+
@override
28+
void initState() {
29+
super.initState();
30+
_controller = AnimationController(
31+
// The duration is only used when `_controller.reverse()` is called,
32+
// i.e.: when the drag is released and the marker gets collapsed.
33+
duration: const Duration(milliseconds: 200),
34+
lowerBound: _EditStateMarkerPill.widthCollapsed,
35+
upperBound: _EditStateMarkerPill.widthExpanded,
36+
vsync: this)
37+
..addListener(() => setState((){}));
38+
}
39+
40+
@override
41+
void dispose() {
42+
_controller.dispose();
43+
super.dispose();
44+
}
45+
46+
late AnimationController _controller;
47+
48+
void _handleDragUpdate(DragUpdateDetails details) {
49+
_controller.value += details.delta.dx;
50+
}
51+
52+
void _handleDragEnd(DragEndDetails details) {
53+
_controller.reverse();
54+
}
55+
1856
@override
1957
Widget build(BuildContext context) {
20-
final hasMarker = message.editState != MessageEditState.none;
58+
final hasMarker = widget.message.editState != MessageEditState.none;
2159

22-
return Row(
23-
crossAxisAlignment: CrossAxisAlignment.baseline,
24-
textBaseline: localizedTextBaseline(context),
25-
children: [
26-
hasMarker
27-
? _EditStateMarkerPill(editState: message.editState)
28-
: const SizedBox(width: _EditStateMarkerPill.widthCollapsed),
29-
...children,
30-
],
60+
final content = LayoutBuilder(
61+
builder: (context, constraints) => OverflowBox(
62+
fit: OverflowBoxFit.deferToChild,
63+
alignment: Alignment.topLeft,
64+
maxWidth: double.infinity,
65+
child: Row(
66+
crossAxisAlignment: CrossAxisAlignment.baseline,
67+
textBaseline: localizedTextBaseline(context),
68+
children: [
69+
hasMarker
70+
? _EditStateMarkerPill(
71+
editState: widget.message.editState,
72+
animation: _controller)
73+
: const SizedBox(width: _EditStateMarkerPill.widthCollapsed),
74+
SizedBox(
75+
width: constraints.maxWidth - _EditStateMarkerPill.widthCollapsed,
76+
child: Row(
77+
crossAxisAlignment: CrossAxisAlignment.baseline,
78+
textBaseline: localizedTextBaseline(context),
79+
children: widget.children),
80+
),
81+
])),
3182
);
32-
}
83+
84+
if (!hasMarker) return content;
85+
86+
return GestureDetector(
87+
onHorizontalDragEnd: _handleDragEnd,
88+
onHorizontalDragUpdate: _handleDragUpdate,
89+
child: content,
90+
);
91+
}
3392
}
3493

3594
class _EditStateMarkerPill extends StatelessWidget {
36-
const _EditStateMarkerPill({required this.editState});
95+
const _EditStateMarkerPill({required this.editState, required this.animation});
3796

3897
final MessageEditState editState;
98+
final Animation<double> animation;
3999

40100
/// The minimum width of the marker.
41-
// Currently, only the collapsed state of the marker has been implemented,
42-
// where only the marker icon, not the marker text, is visible.
101+
///
102+
/// This is when no drag has been performed on the message row
103+
/// where only the moved/edited icon, not the text, is visible.
43104
static const double widthCollapsed = 16;
44105

106+
/// The maximum width of the marker.
107+
///
108+
/// This is typically wider than the colored pill when the marker is fully
109+
/// expanded. At that point only the blank space to the right of the colored
110+
/// block will grow until the marker reaches this width.
111+
static const double widthExpanded = 100;
112+
113+
double get _animationProgress => (animation.value - widthCollapsed) / widthExpanded;
114+
45115
@override
46116
Widget build(BuildContext context) {
47117
final designVariables = DesignVariables.of(context);
118+
final zulipLocalizations = ZulipLocalizations.of(context);
48119

49120
final IconData icon;
121+
final String markerText;
50122
switch (editState) {
51123
case MessageEditState.none:
52124
assert(false);
53125
return const SizedBox(width: widthCollapsed);
54126
case MessageEditState.edited:
55127
icon = ZulipIcons.edited;
128+
markerText = zulipLocalizations.messageIsEdited;
129+
break;
56130
case MessageEditState.moved:
57131
icon = ZulipIcons.message_moved;
132+
markerText = zulipLocalizations.messageIsMoved;
133+
break;
58134
}
59135

136+
var marker = Row(
137+
mainAxisAlignment: MainAxisAlignment.end,
138+
mainAxisSize: MainAxisSize.min,
139+
children: [
140+
Flexible(
141+
fit: FlexFit.loose,
142+
child: Text(markerText,
143+
overflow: TextOverflow.clip,
144+
softWrap: false,
145+
textAlign: TextAlign.center,
146+
style: TextStyle(fontSize: 15, color: Color.lerp(
147+
designVariables.editedMovedMarkerExpanded.withAlpha(0),
148+
designVariables.editedMovedMarkerExpanded,
149+
_animationProgress)))),
150+
SizedBox(width: lerpDouble(0, 5, _animationProgress)),
151+
// To match the Figma design, we cannot make the collapsed width of the
152+
// marker larger. We need to explicitly allow the icon to overflow.
153+
OverflowBox(
154+
fit: OverflowBoxFit.deferToChild,
155+
maxWidth: 8,
156+
child: Icon(icon, size: 14, color: Color.lerp(
157+
designVariables.editedMovedMarkerCollapsed,
158+
designVariables.editedMovedMarkerExpanded,
159+
_animationProgress)),
160+
),
161+
],
162+
);
163+
60164
return ConstrainedBox(
61-
constraints: const BoxConstraints(maxWidth: widthCollapsed),
62-
child: Padding(
63-
padding: const EdgeInsetsDirectional.only(start: 2),
64-
child: Icon(icon, size: 14,
65-
color: designVariables.editedMovedMarkerCollapsed)));
165+
constraints: BoxConstraints(maxWidth: animation.value),
166+
child: Container(
167+
margin: EdgeInsets.only(left: lerpDouble(2, 10, _animationProgress)!, right: 0),
168+
clipBehavior: Clip.hardEdge,
169+
decoration: BoxDecoration(
170+
borderRadius: BorderRadius.circular(3),
171+
color: Color.lerp(
172+
designVariables.editedMovedMarkerBg.withAlpha(0),
173+
designVariables.editedMovedMarkerBg,
174+
_animationProgress)),
175+
child: Padding(
176+
padding: EdgeInsets.symmetric(horizontal: lerpDouble(0, 3, _animationProgress)!),
177+
child: marker),
178+
),
179+
);
66180
}
67181
}

lib/widgets/theme.dart

+14
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ class DesignVariables extends ThemeExtension<DesignVariables> {
8585
// TODO(#95) unchanged in dark theme?
8686
star = const HSLColor.fromAHSL(0.5, 47, 1, 0.41).toColor(),
8787
// TODO(#95) need dark-theme color
88+
editedMovedMarkerBg = const Color(0xffddecf6),
89+
// TODO(#95) need dark-theme color
90+
editedMovedMarkerExpanded = const Color(0xff26516e),
91+
// TODO(#95) need dark-theme color
8892
editedMovedMarkerCollapsed = const Color.fromARGB(128, 146, 167, 182);
8993

9094
DesignVariables._({
@@ -95,6 +99,8 @@ class DesignVariables extends ThemeExtension<DesignVariables> {
9599
required this.title,
96100
required this.streamColorSwatches,
97101
required this.star,
102+
required this.editedMovedMarkerBg,
103+
required this.editedMovedMarkerExpanded,
98104
required this.editedMovedMarkerCollapsed,
99105
});
100106

@@ -119,6 +125,8 @@ class DesignVariables extends ThemeExtension<DesignVariables> {
119125

120126
// Not named variables in Figma; taken from older Figma drafts, or elsewhere.
121127
final Color star;
128+
final Color editedMovedMarkerBg;
129+
final Color editedMovedMarkerExpanded;
122130
final Color editedMovedMarkerCollapsed;
123131

124132
@override
@@ -130,6 +138,8 @@ class DesignVariables extends ThemeExtension<DesignVariables> {
130138
Color? title,
131139
StreamColorSwatches? streamColorSwatches,
132140
Color? star,
141+
Color? editedMovedMarkerBg,
142+
Color? editedMovedMarkerExpanded,
133143
Color? editedMovedMarkerCollapsed,
134144
}) {
135145
return DesignVariables._(
@@ -140,6 +150,8 @@ class DesignVariables extends ThemeExtension<DesignVariables> {
140150
title: title ?? this.title,
141151
streamColorSwatches: streamColorSwatches ?? this.streamColorSwatches,
142152
star: star ?? this.star,
153+
editedMovedMarkerBg: editedMovedMarkerBg ?? this.editedMovedMarkerBg,
154+
editedMovedMarkerExpanded: editedMovedMarkerExpanded ?? this.editedMovedMarkerExpanded,
143155
editedMovedMarkerCollapsed: editedMovedMarkerCollapsed ?? this.editedMovedMarkerCollapsed,
144156
);
145157
}
@@ -157,6 +169,8 @@ class DesignVariables extends ThemeExtension<DesignVariables> {
157169
title: Color.lerp(title, other.title, t)!,
158170
streamColorSwatches: StreamColorSwatches.lerp(streamColorSwatches, other.streamColorSwatches, t),
159171
star: Color.lerp(star, other.star, t)!,
172+
editedMovedMarkerBg: Color.lerp(editedMovedMarkerBg, other.editedMovedMarkerBg, t)!,
173+
editedMovedMarkerExpanded: Color.lerp(editedMovedMarkerExpanded, other.editedMovedMarkerExpanded, t)!,
160174
editedMovedMarkerCollapsed: Color.lerp(editedMovedMarkerCollapsed, other.editedMovedMarkerCollapsed, t)!,
161175
);
162176
}

0 commit comments

Comments
 (0)