You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter中SnackBar文本无法选中复制,如何开启文本选择功能?

解决Flutter SnackBar文本无法选中复制的问题

默认的Text组件不支持选中复制,要让SnackBar内的文本能被鼠标点击或触摸选中复制,直接用SelectableText替代Text组件作为content即可。

修改后的代码示例

SnackBar snackBar = SnackBar(
  showCloseIcon: true,
  duration: const Duration(milliseconds: 100), // 原代码的100需转为Duration类型,这里以毫秒为例
  content: const SelectableText('Hello World'),
);

自定义选中样式(可选)

如果需要调整选中文本的外观,可给SelectableText添加相关参数:

SnackBar snackBar = SnackBar(
  showCloseIcon: true,
  duration: const Duration(milliseconds: 100),
  content: SelectableText(
    'Hello World',
    style: const TextStyle(color: Colors.white),
    cursorColor: Colors.blue,
    selectionColor: Colors.grey.withOpacity(0.5),
    selectionControls: MaterialTextSelectionControls(),
  ),
);

修改完成后,SnackBar内的文本就能正常被选中并复制了。

内容的提问来源于stack exchange,提问作者eastwater

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 04:42:32