如何为文本小部件 Flutter 实现 OnPressed 回调

How can I implement OnPressed callback for Text widget, Flutter

我有一个 Text widget 按下必须显示另一个 Route。但是我看不到 Text widget 的任何 onPressed() 方法。请帮助。

只需将您的标题包裹在 GestureDetector 中即可处理点击。然后调用 NavigatorpushNamed 重定向到新路由。

new GestureDetector(
  onTap: () {
    Navigator.pushNamed(context, "myRoute");
  },
  child: new Text("my Title"),
);

使用InkWell

这也会给你带来不错的连锁反应

 new InkWell(
          onTap: () {
            Navigator.pushNamed(context, "YourRoute");
          },
          child: new Padding(
            padding: new EdgeInsets.all(10.0),
            child: new Text("Tap Here"),
          ),
        );

new FlatButton(
          onPressed: () {
            Navigator.pushNamed(context, "YourRoute");
          },
          child: new Text("Tap Here"),
        )

对于Flutter 的所有小部件,您可以使用这些小部件

实现onPressed

1. InkWell() : 使用此小部件,您可以在单击时添加涟漪效果

InkWell(
     onTap: () {
         Navigator.pushNamed(context, "write your route");
     },
     child: new Text("Click Here"),
 );


2。 GestureDetector() : 使用此小部件,您可以实现 onTap、onDoubleTap、onLongPress 等等

GestureDetector(
     onTap: () {
         Navigator.pushNamed(context, "write your route");
     },
     onLongPress: (){
        // open dialog OR navigate OR do what you want
     }
     child: new Text("Save"),
 );

您可以使用 TextButton。由于它具有透明背景,因此看起来像一个文本小部件。

            TextButton(
              onPressed: () {
                //action
              },
              child: Text(
                'Title Text', //title
                textAlign: TextAlign.end, //aligment
              ),
            ),