简体   繁体   English

如何实现可点击的文本小部件 Flutter?

[英]How can I implement tap-able Text widget, Flutter?

I have a Text widget on pressing which another Route has to be shown.我有一个文本小部件,按下它必须显示另一条路线。 But I could not see any onPressed() method for the Text widget.但是我看不到 Text 小部件的任何 onPressed() 方法。 Please Help.请帮忙。

In your case there are multiple routes where you can go.在您的情况下,您可以使用多条路线 go。

  1. Using material TextButton:使用材质 TextButton:

    TextButton(onPressed: () {}, child: Text('Your child'))

  2. Using InkWell Widget, which covers your widget with 'as button' properties, splashes etc, which allows you to have more customisable buttons taps:使用 InkWell 小部件,它使用“作为按钮”属性、飞溅等覆盖您的小部件,这使您可以拥有更多可自定义的按钮点击:

    InkWell(onTap: () {}, onDoubleTap: () {}, onLongPress: () {}, onHover: () {}, ..etc, child: Text('Your child widget'))

  3. Using GestureDetector, is the most customisable solution for any gestures you can provide (from its name), can handle taps, long presses, double taps, drags etc. Syntax is similar:使用 GestureDetector,是您可以提供的任何手势的最可定制的解决方案(从它的名称来看),可以处理点击、长按、双击、拖动等。语法类似:

    GestureDetector(onTap: () {}, child: Text('Your child widget'))

Try using TextButton widget:尝试使用 TextButton 小部件:

TextButton(
            onPressed: () {},
            child: Text('Simple Button'),
          ),

The best way to make any widget tappable is to use GestrureDetector you just put it outside of the widget/widgets you want and you can use dozen of properties .使任何小部件可点击的最佳方法是使用GestrureDetector ,您只需将其放在您想要的小部件/小部件之外,您就可以使用十几个属性 For example例如

GestureDetector(child:

 onPressed: () {},

 Text('Your text')

)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM