showPopupMenu function
Future<String?>
showPopupMenu(
- BuildContext context,
- PopupType type, {
- List<
PopupMenuEntry< ? items,String> > - bool? isPin,
- bool? isMute,
- bool? canRecall,
- bool? canQuote,
- bool? canCopy,
- bool? canEdit,
- String editLabel = '编辑',
- bool? canForward,
- bool canMulti = true,
- bool? canSpeechToText,
- bool? canCancelSpeechToText,
- bool canMessageReaction = false,
- String messageReactionLabel = '表情回应',
- bool conversationIsSystem = false,
- Offset? globalPosition,
- BuildContext? panelContext,
- RouteSettings? routeSettings,
- bool desktopConvoMenu = false,
Implementation
Future<String?> showPopupMenu(
BuildContext context,
PopupType type, {
List<PopupMenuEntry<String>>? items,
bool? isPin,
bool? isMute,
bool? canRecall,
bool? canQuote,
bool? canCopy,
bool? canEdit,
String editLabel = '编辑',
/// 消息是否允许转发。未发送成功的消息(发送中/失败/已取消)不提供该入口,
/// 避免把未生效的内容复制到其它会话。
bool? canForward,
bool canMulti = true,
bool? canSpeechToText,
bool? canCancelSpeechToText,
bool canMessageReaction = false,
String messageReactionLabel = '表情回应',
bool conversationIsSystem = false,
// 桌面端:长按位置与所属面板,用于把菜单限制在聊天面板内
Offset? globalPosition,
BuildContext? panelContext,
RouteSettings? routeSettings,
/// 会话列表(PC 右键)改用设计稿 123:1620「对话list弹层菜单」的样式:
/// 104 起步的自适应宽度、6 圆角、0.5 描边、「0 4 15.9」投影、29.5 行高、
/// 28 高内缩悬浮块。其它菜单与移动端会话菜单不受影响。
bool desktopConvoMenu = false,
}) {
// 桌面会话菜单与移动端/聊天菜单在行高、内边距、行内高亮上都不一样,
// 这里先定下来,后面构建菜单项和容器时统一使用。
final desktopConvo = desktopConvoMenu && type == PopupType.convo;
final itemHeight = desktopConvo
? RCKDesktopLayoutConfig.convoMenuRowHeight
: _popupMenuItemHeight;
// 菜单行是 PopupMenuItem 的子节点,排版会继承它注入的 DefaultTextStyle;
// 量宽必须复用同一份样式,否则算出来的宽度放不下文案。
final convoLabelStyle = desktopConvo
? _desktopConvoMenuLabelStyle(context)
: null;
// 默认菜单项列表
List<PopupMenuEntry<String>> buildChatItems() {
final result = [
if (((canSpeechToText ?? false) || (canCancelSpeechToText ?? false)) &&
!conversationIsSystem)
PopupMenuItem<String>(
value: 'speechToText',
height: _popupMenuItemHeight,
padding: EdgeInsets.zero,
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
ImageUtil.getImageWidget(
RCKThemeProvider().themeIcon.speechToText ?? '',
width: kPopupIconSize,
height: kPopupIconSize,
color: RCKThemeProvider().themeColor.textPrimary,
),
Text(
(canCancelSpeechToText ?? false) ? '取消转文字' : '转文字',
style: TextStyle(
fontSize: convoPopupFontSize,
color: RCKThemeProvider().themeColor.textPrimary,
),
),
],
),
),
),
if (canMessageReaction && !conversationIsSystem)
PopupMenuItem<String>(
value: 'messageReaction',
height: _popupMenuItemHeight,
padding: EdgeInsets.zero,
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
ImageUtil.getImageWidget(
RCKThemeProvider().themeIcon.emoji ?? '',
width: kPopupIconSize,
height: kPopupIconSize,
color: RCKThemeProvider().themeColor.textPrimary,
),
const SizedBox(width: 8),
Flexible(
child: Text(
messageReactionLabel,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: convoPopupFontSize,
color: RCKThemeProvider().themeColor.textPrimary,
),
),
),
],
),
),
),
if (canCopy ?? true)
PopupMenuItem<String>(
value: 'copy',
height: _popupMenuItemHeight,
padding: EdgeInsets.zero,
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
ImageUtil.getImageWidget(
RCKThemeProvider().themeIcon.copy ?? '',
width: kPopupIconSize,
height: kPopupIconSize,
),
Text(
'复制',
style: TextStyle(
fontSize: convoPopupFontSize,
color: RCKThemeProvider().themeColor.textPrimary,
),
),
],
),
),
),
if ((canEdit ?? false) && !conversationIsSystem)
PopupMenuItem<String>(
value: 'edit',
height: _popupMenuItemHeight,
padding: EdgeInsets.zero,
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
ImageUtil.getImageWidget(
RCKThemeProvider().themeIcon.edit ?? '',
width: kPopupIconSize,
height: kPopupIconSize,
color: RCKThemeProvider().themeColor.textPrimary,
),
Text(
editLabel,
style: TextStyle(
fontSize: convoPopupFontSize,
color: RCKThemeProvider().themeColor.textPrimary,
),
),
],
),
),
),
// 未发送成功的消息(发送中/失败/已取消)不提供「转发」:内容还没真正发出去,
// 转发只会把一份未生效的内容复制到目标会话。
if ((canForward ?? true) && !conversationIsSystem)
PopupMenuItem<String>(
value: 'forward',
height: _popupMenuItemHeight,
padding: EdgeInsets.zero,
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
ImageUtil.getImageWidget(
RCKThemeProvider().themeIcon.forwardSingle ?? '',
width: kPopupIconSize,
height: kPopupIconSize,
),
Text(
'转发',
style: TextStyle(
fontSize: convoPopupFontSize,
color: RCKThemeProvider().themeColor.textPrimary,
),
),
],
),
),
),
PopupMenuItem<String>(
value: 'delete',
height: _popupMenuItemHeight,
padding: EdgeInsets.zero,
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
ImageUtil.getImageWidget(
RCKThemeProvider().themeIcon.delete ?? '',
width: kPopupIconSize,
height: kPopupIconSize,
),
Text(
'删除',
style: TextStyle(
fontSize: convoPopupFontSize,
color: RCKThemeProvider().themeColor.textPrimary,
),
),
],
),
),
),
if (canMulti && !conversationIsSystem)
PopupMenuItem<String>(
value: 'multi',
height: _popupMenuItemHeight,
padding: EdgeInsets.zero,
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
ImageUtil.getImageWidget(
RCKThemeProvider().themeIcon.multiSelect ?? '',
width: kPopupIconSize,
height: kPopupIconSize,
color: RCKThemeProvider().themeColor.textPrimary,
),
Text(
'多选',
style: TextStyle(
fontSize: convoPopupFontSize,
color: RCKThemeProvider().themeColor.textPrimary,
),
),
],
),
),
),
if ((canQuote ?? true) && !conversationIsSystem)
PopupMenuItem<String>(
value: 'quote',
height: _popupMenuItemHeight,
padding: EdgeInsets.zero,
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
ImageUtil.getImageWidget(
RCKThemeProvider().themeIcon.reply ?? '',
width: kPopupIconSize,
height: kPopupIconSize,
color: RCKThemeProvider().themeColor.textPrimary,
),
Text(
'引用',
style: TextStyle(
fontSize: convoPopupFontSize,
color: RCKThemeProvider().themeColor.textPrimary,
),
),
],
),
),
),
if ((canRecall ?? false) && !conversationIsSystem)
PopupMenuItem<String>(
value: 'recall',
height: _popupMenuItemHeight,
padding: EdgeInsets.zero,
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
ImageUtil.getImageWidget(
RCKThemeProvider().themeIcon.recall ?? '',
width: kPopupIconSize,
height: kPopupIconSize,
color: RCKThemeProvider().themeColor.textPrimary,
),
Text(
'撤回',
style: TextStyle(
fontSize: convoPopupFontSize,
color: RCKThemeProvider().themeColor.textPrimary,
),
),
],
),
),
),
];
const chatMenuOrder = <String, int>{
'speechToText': 0,
'messageReaction': 1,
'copy': 2,
'delete': 3,
'recall': 4,
'quote': 5,
'edit': 6,
'forward': 7,
'multi': 8,
};
result.sort((left, right) {
final leftOrder = chatMenuOrder[left.value] ?? chatMenuOrder.length;
final rightOrder = chatMenuOrder[right.value] ?? chatMenuOrder.length;
return leftOrder.compareTo(rightOrder);
});
return result;
}
// 附加气泡菜单项列表
List<PopupMenuEntry<String>> buildBubbleAppendItems() => [
if ((canCancelSpeechToText ?? false) && !conversationIsSystem)
PopupMenuItem<String>(
value: 'speechToText',
height: _popupMenuItemHeight,
padding: EdgeInsets.zero,
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
ImageUtil.getImageWidget(
RCKThemeProvider().themeIcon.speechToText ?? '',
width: kPopupIconSize,
height: kPopupIconSize,
color: RCKThemeProvider().themeColor.textPrimary,
),
Text(
'取消转文字',
style: TextStyle(
fontSize: convoPopupFontSize,
color: RCKThemeProvider().themeColor.textPrimary,
),
),
],
),
),
),
PopupMenuItem<String>(
value: 'copy',
height: _popupMenuItemHeight,
padding: EdgeInsets.zero,
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
ImageUtil.getImageWidget(
RCKThemeProvider().themeIcon.copy ?? '',
width: kPopupIconSize,
height: kPopupIconSize,
),
Text(
'复制',
style: TextStyle(
fontSize: convoPopupFontSize,
color: RCKThemeProvider().themeColor.textPrimary,
),
),
],
),
),
),
];
// 会话列表菜单文案:桌面端要按最长文案算菜单宽度,先把文案提取出来复用。
final convoPinLabel = isPin ?? false ? '取消置顶' : '置顶';
final convoMuteLabel = isMute ?? false ? '取消免打扰' : '免打扰';
const convoDeleteLabel = '删除';
// 默认菜单项列表
List<PopupMenuEntry<String>> buildConvoItems() => [
PopupMenuItem<String>(
value: 'pin',
height: itemHeight,
padding: EdgeInsets.zero,
child: _convoMenuItemRow(
desktop: desktopConvo,
desktopLabelStyle: convoLabelStyle,
iconPath: isPin ?? false
? RCKThemeProvider().themeIcon.unpin ?? ''
: RCKThemeProvider().themeIcon.pin ?? '',
iconColor: RCKThemeProvider().themeColor.textPrimary,
label: convoPinLabel,
),
),
PopupMenuItem<String>(
value: 'mute',
height: itemHeight,
padding: EdgeInsets.zero,
child: _convoMenuItemRow(
desktop: desktopConvo,
desktopLabelStyle: convoLabelStyle,
iconPath: isMute ?? false
? RCKThemeProvider().themeIcon.allowNotification ?? ''
: RCKThemeProvider().themeIcon.doNotDisturb1 ?? '',
label: convoMuteLabel,
),
),
PopupMenuItem<String>(
value: 'delete',
height: itemHeight,
padding: EdgeInsets.zero,
child: _convoMenuItemRow(
desktop: desktopConvo,
desktopLabelStyle: convoLabelStyle,
iconPath: RCKThemeProvider().themeIcon.delete ?? '',
label: convoDeleteLabel,
),
),
];
final List<PopupMenuEntry<String>> contentItems =
items ??
switch (type) {
PopupType.convo => buildConvoItems(),
PopupType.chat => buildChatItems(),
PopupType.bubbleAppend => buildBubbleAppendItems(),
};
// 上下各 4 像素的占位项是给 showMenu 的内边距做补偿的;
// 桌面会话菜单自己按设计稿留 6.5,不需要占位项。
final menuItems = <PopupMenuEntry<String>>[
if (!desktopConvo)
const PopupMenuItem<String>(
enabled: false,
height: 4,
padding: EdgeInsets.zero,
child: SizedBox(),
),
...contentItems,
if (!desktopConvo)
const PopupMenuItem<String>(
enabled: false,
height: 4,
padding: EdgeInsets.zero,
child: SizedBox(),
),
];
// 未传鼠标位置的移动长按沿用原定位。
final RenderBox renderBox = context.findRenderObject() as RenderBox;
final Offset offset = renderBox.localToGlobal(
renderBox.size.center(Offset.zero),
);
final overlay =
Navigator.of(context).overlay!.context.findRenderObject() as RenderBox;
if (desktopConvo) {
return _showDesktopConvoMenu(
context,
items: contentItems,
globalPosition: globalPosition ?? offset,
// 传了自定义 items 时文案由调用方决定,宽度退回设计稿基准值。
width: _desktopConvoMenuWidth(
context,
items == null
? [convoPinLabel, convoMuteLabel, convoDeleteLabel]
: const <String>[],
),
);
}
if (panelContext != null) {
// The root overlay spans all panes; clamp using the laid-out menu size.
final panel = panelContext.findRenderObject() as RenderBox;
final anchor = panel.globalToLocal(globalPosition ?? offset);
final themes = InheritedTheme.capture(
from: context,
to: Navigator.of(context).context,
);
return showGeneralDialog<String>(
context: context,
useRootNavigator: false,
routeSettings: routeSettings,
barrierDismissible: true,
barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
barrierColor: Colors.transparent,
requestFocus: true,
pageBuilder: (routeContext, _, _) => LayoutBuilder(
builder: (routeContext, constraints) {
if (!panel.attached) return const SizedBox.shrink();
final origin = overlay.globalToLocal(
panel.localToGlobal(Offset.zero),
);
final bounds = (origin & panel.size)
.intersect(Offset.zero & constraints.biggest)
.deflate(8);
return CustomSingleChildLayout(
delegate: _PanelMenuLayout(bounds, origin + anchor),
child: themes.wrap(
Material(
elevation: 8,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
color: RCKThemeProvider().themeColor.bgAuxiliary1,
surfaceTintColor: Colors.transparent,
shadowColor: const Color(0x33000000),
child: Semantics(
role: SemanticsRole.menu,
scopesRoute: true,
namesRoute: true,
explicitChildNodes: true,
label: MaterialLocalizations.of(routeContext).popupMenuLabel,
child: SingleChildScrollView(
padding: EdgeInsets.zero,
child: ListBody(children: menuItems),
),
),
),
),
);
},
),
);
}
// PopupRoute 的布局边界覆盖整个窗口,不会自动把菜单限制在软键盘上方。
// 长按位置可能已经落入键盘区域,先把锚点移到可见区域底部,让 showMenu
// 选择上方空间布局,避免菜单被键盘遮挡。
final localAnchor = overlay.globalToLocal(globalPosition ?? offset);
final navigatorContext = Navigator.of(context).context;
final mediaQuery = MediaQuery.of(navigatorContext);
final keyboardTop = (mediaQuery.size.height - mediaQuery.viewInsets.bottom)
.clamp(0.0, overlay.size.height);
final menuHeight = menuItems.fold<double>(
0,
(height, item) => height + item.height,
);
final maxAnchorY = math.max(0.0, keyboardTop - menuHeight - 8);
final clampedAnchor = Offset(
localAnchor.dx,
localAnchor.dy.clamp(0.0, maxAnchorY),
);
final RelativeRect position = globalPosition == null
? RelativeRect.fromLTRB(
clampedAnchor.dx,
clampedAnchor.dy,
clampedAnchor.dx,
clampedAnchor.dy,
)
: RelativeRect.fromRect(
clampedAnchor & Size.zero,
Offset.zero & overlay.size,
);
// 使用自定义主题和样式设置
return showMenu<String>(
routeSettings: routeSettings,
menuPadding: EdgeInsets.zero,
context: context,
position: position,
// 弹出消息菜单时保留输入框焦点,避免键盘被 PopupRoute 收起。
requestFocus: false,
elevation: 8.0, // 阴影高度
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10), // 圆角为10
),
constraints: BoxConstraints(
minWidth: 160,
maxWidth: 160, // 固定宽度为160
),
color: RCKThemeProvider().themeColor.bgAuxiliary1, // 背景色
items: menuItems,
// 自定义内边距和项目间距
surfaceTintColor: Colors.transparent,
shadowColor: const Color(0x33000000), // 阴影颜色对应 #00000033
);
}