手势检测:ElevatedButton 内的 SelectableText
我在 Flutter Web 中遇到问题,其组件可点击(ElevatedButton
、GestureDetector
等),同时包含 SelectableText
小部件内部。
主要问题是,单击 SelectableText
顶部不会触发 onPressed
按钮回调。相反,它看起来像是“启动”文本选择。
从我的角度来看,只有在文本上双击、拖动开始或长按时才应该初始化文本选择,而不是在单击时初始化。
我的问题的一个非常简化的版本:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: Center(
child: ElevatedButton(
onPressed: () {
print('onTap');
},
child: Container(
padding: const EdgeInsets.all(36),
child: const SelectableText(
'Select me, but I also want to be clickable!',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 24,
),
),
),
),
),
);
}
}
请注意 ElevatedButton
和 SelectableText
之间的 padding
有任何提示吗?想法? 提前致谢
I have a problem in Flutter Web with components that are Clickable
(ElevatedButton
, GestureDetector
, etc) and at the same time contains a SelectableText
widget inside.
The main problem is that a single click on top of the SelectableText
does not trigger the onPressed
button callback. Instead, it looks like 'initiates' a text selection.
From my point of view, text selection should only be initialized if a double tap, drag start or long tap happens on the text, but not on single tap.
A very simplified version of my problem:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: Center(
child: ElevatedButton(
onPressed: () {
print('onTap');
},
child: Container(
padding: const EdgeInsets.all(36),
child: const SelectableText(
'Select me, but I also want to be clickable!',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 24,
),
),
),
),
),
);
}
}
Note the padding
between the ElevatedButton
and the SelectableText
Any tips? ideas?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我知道您正在尽力模仿“本机网络体验”,其中所有文本都是可选的,并且就像您所说的“点击和拖动开始是不同的”,但它们的差异可能只有 1 像素。
如果你看这个网页(堆栈溢出),我的用户名是可点击的,尝试拖动并选择它,你不能。现在向上滚动,在你的问题下方,标签是可点击的,尝试拖动并选择“flutter”一词,你不能......
所以说实话,对于按钮上的那些文本,也许只是不要让它们可供选择:D
但如果你坚持的话,
SelectableText
小部件上也有onTap
事件,因此你可以触发与按钮相同的回调,因此用户单击位置并不重要。I understand you are trying your best to mimic the "native web experience" where all texts are selectable, and like you said, "tap and drag start are different", but their differences might be as little as 1 pixel.
If you look at this web page (stack overflow), my user name is clickable, try to drag and select it, you can't. Now scroll up, below your question, the tags are clickable, try to drag and select on the word "flutter", you can't...
So honestly, for those texts on buttons, maybe just don't let them be selectable :D
But if you insist, there's
onTap
event onSelectableText
widget as well, so you can fire the same callback as the button, so it doesn't matter where user clicked.