手势检测:ElevatedButton 内的 SelectableText

发布于 2025-01-17 08:13:19 字数 1384 浏览 0 评论 0原文

我在 Flutter Web 中遇到问题,其组件可点击(ElevatedButtonGestureDetector 等),同时包含 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,
              ),
            ),
          ),
        ),
      ),
    );
  }
}

在此处输入图像描述

请注意 ElevatedButtonSelectableText 之间的 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,
              ),
            ),
          ),
        ),
      ),
    );
  }
}

enter image description here

Note the padding between the ElevatedButton and the SelectableText

Any tips? ideas?
Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

季末如歌 2025-01-24 08:13:19

我知道您正在尽力模仿“本机网络体验”,其中所有文本都是可选的,并且就像您所说的“点击和拖动开始是不同的”,但它们的差异可能只有 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 on SelectableText widget as well, so you can fire the same callback as the button, so it doesn't matter where user clicked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文