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
相关产品推荐
相关产品推荐

