|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter/rendering.dart'; |
| 3 | +import 'package:flutter_gen/gen_l10n/zulip_localizations.dart'; |
| 4 | + |
| 5 | +import '../api/model/model.dart'; |
| 6 | +import 'icons.dart'; |
| 7 | +import 'text.dart'; |
| 8 | +import 'theme.dart'; |
| 9 | + |
| 10 | +class SwipableMessageRow extends StatefulWidget { |
| 11 | + const SwipableMessageRow({ |
| 12 | + super.key, |
| 13 | + required this.children, |
| 14 | + required this.message, |
| 15 | + }); |
| 16 | + |
| 17 | + final List<Widget> children; |
| 18 | + final Message message; |
| 19 | + |
| 20 | + @override |
| 21 | + State<StatefulWidget> createState() => _SwipableMessageRowState(); |
| 22 | +} |
| 23 | + |
| 24 | +class _SwipableMessageRowState extends State<SwipableMessageRow> { |
| 25 | + @override |
| 26 | + Widget build(BuildContext context) { |
| 27 | + final hasMarker = widget.message.editState != MessageEditState.none; |
| 28 | + |
| 29 | + return LayoutBuilder( |
| 30 | + builder: (context, constraints) => Row( |
| 31 | + crossAxisAlignment: CrossAxisAlignment.baseline, |
| 32 | + textBaseline: localizedTextBaseline(context), |
| 33 | + children: [ |
| 34 | + hasMarker |
| 35 | + ? EditStateMarker(editState: widget.message.editState) |
| 36 | + : const SizedBox(width: 16), |
| 37 | + ...widget.children, |
| 38 | + ], |
| 39 | + ), |
| 40 | + ); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +class EditStateMarker extends StatelessWidget { |
| 45 | + /// The minimum width of the marker. |
| 46 | + /// |
| 47 | + /// Currently, only the collapsed state of the marker has been implemented, |
| 48 | + /// where only the marker icon, not the marker text, is visible. |
| 49 | + static const double widthCollapsed = 16; |
| 50 | + |
| 51 | + const EditStateMarker({ |
| 52 | + super.key, |
| 53 | + required MessageEditState editState, |
| 54 | + }) : _editState = editState; |
| 55 | + |
| 56 | + final MessageEditState _editState; |
| 57 | + |
| 58 | + @override |
| 59 | + Widget build(BuildContext context) { |
| 60 | + final theme = DesignVariables.of(context); |
| 61 | + final zulipLocalizations = ZulipLocalizations.of(context); |
| 62 | + |
| 63 | + final IconData icon; |
| 64 | + final double iconSize; |
| 65 | + final String markerText; |
| 66 | + |
| 67 | + switch (_editState) { |
| 68 | + case MessageEditState.none: |
| 69 | + return const SizedBox(width: widthCollapsed); |
| 70 | + case MessageEditState.edited: |
| 71 | + icon = ZulipIcons.edited; |
| 72 | + iconSize = 14; |
| 73 | + markerText = zulipLocalizations.messageIsEdited; |
| 74 | + break; |
| 75 | + case MessageEditState.moved: |
| 76 | + icon = ZulipIcons.message_moved; |
| 77 | + iconSize = 8; |
| 78 | + markerText = zulipLocalizations.messageIsMoved; |
| 79 | + break; |
| 80 | + } |
| 81 | + |
| 82 | + var marker = Row( |
| 83 | + mainAxisAlignment: MainAxisAlignment.end, |
| 84 | + mainAxisSize: MainAxisSize.min, |
| 85 | + children: [ |
| 86 | + Flexible( |
| 87 | + fit: FlexFit.loose, |
| 88 | + // For now, [markerText] is not displayed because it is transparent and |
| 89 | + // there is not enough space in the parent ConstrainedBox. |
| 90 | + child: Text('$markerText ', |
| 91 | + overflow: TextOverflow.clip, |
| 92 | + softWrap: false, |
| 93 | + textAlign: TextAlign.center, |
| 94 | + style: TextStyle(fontSize: 15, color: theme.textMarker.withAlpha(0)))), |
| 95 | + // To match the Figma design, we cannot make the collapsed width of the |
| 96 | + // marker larger. We need to explicitly allow the icon to overflow. |
| 97 | + OverflowBox( |
| 98 | + fit: OverflowBoxFit.deferToChild, |
| 99 | + maxWidth: 10, |
| 100 | + child: Icon(icon, size: iconSize, color: theme.textMarkerLight), |
| 101 | + ), |
| 102 | + ], |
| 103 | + ); |
| 104 | + |
| 105 | + return ConstrainedBox( |
| 106 | + constraints: const BoxConstraints(maxWidth: widthCollapsed), |
| 107 | + child: Container( |
| 108 | + margin: const EdgeInsets.only(top: 4, left: 0), |
| 109 | + clipBehavior: Clip.hardEdge, |
| 110 | + decoration: BoxDecoration( |
| 111 | + borderRadius: BorderRadius.circular(2), |
| 112 | + color: theme.bgMarker.withAlpha(0)), |
| 113 | + child: Padding( |
| 114 | + padding: const EdgeInsets.all(1.0), |
| 115 | + child: marker), |
| 116 | + ), |
| 117 | + ); |
| 118 | + } |
| 119 | +} |
0 commit comments