How can I implement OnPressed callback for Text widget, Flutter

前端 未结 3 1565
北海茫月
北海茫月 2021-02-05 00:22

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. Plea

相关标签:
3条回答
  • 2021-02-05 00:35

    For All the widget of Flutter you can implement onPressed using these widget

    1. InkWell() : Using this widget you can add ripple effect on clicking

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


    2. GestureDetector() : Using this widget you can implement, onTap, onDoubleTap, onLongPress and many more

    GestureDetector(
         onTap: () {
             Navigator.pushNamed(context, "write your route");
         },
         onLongPress: (){
            // open dialog OR navigate OR do what you want
         }
         child: new Text("Save"),
     );
    
    0 讨论(0)
  • 2021-02-05 00:36

    Just wrap your title in a GestureDetector to handle clicks. Then call Navigator's pushNamed to redirect to a new route.

    new GestureDetector(
      onTap: () {
        Navigator.pushNamed(context, "myRoute");
      },
      child: new Text("my Title"),
    );
    
    0 讨论(0)
  • 2021-02-05 00:49

    Use InkWell

    this gives you nice ripple effect as well

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

    or

    new FlatButton(
              onPressed: () {
                Navigator.pushNamed(context, "YourRoute");
              },
              child: new Text("Tap Here"),
            )
    
    0 讨论(0)
提交回复
热议问题