要取消SelectableText的上下文菜单而不点击菜单项,可以使用LongPressGestureRecognizer来检测长按手势,并在手势识别器回调中禁用默认的上下文菜单。
下面是一个示例代码:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Disable SelectableText Context Menu'),
),
body: Center(
child: GestureDetector(
onLongPress: () {}, // 空回调函数,什么也不做
child: SelectableText(
'Long press here to disable context menu',
enableInteractiveSelection: true,
// 设置为true以启用可选文本
showCursor: true,
// 显示光标
cursorWidth: 2.0,
// 光标宽度
cursorRadius: Radius.circular(5),
// 光标半径
style: TextStyle(fontSize: 20),
),
),
),
),
);
}
}
在这个示例中,我们使用了一个空的onLongPress回调函数来禁用默认的上下文菜单。当用户长按SelectableText时,不会显示任何菜单项。
下一篇:不点击即可突出显示当前路线项