【问题标题】:How can I implement OnPressed callback for Text widget, Flutter如何为 Text 小部件 Flutter 实现 OnPressed 回调
【发布时间】:2018-08-10 13:54:50
【问题描述】:

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

【问题讨论】:

    标签: text widget dart flutter


    【解决方案1】:

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

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

    【讨论】:

    • 上周我开始使用 Flutter。这实际上是我遇到过的最令人惊叹的开发人员体验。太棒了。
    【解决方案2】:

    使用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"),
            )
    

    【讨论】:

      【解决方案3】:

      对于 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"),
       );
      

      【讨论】:

      • 所以您将两个答案结合起来并编写自己的答案?我喜欢这个
      • 不,我改进了上述答案并编写了自己的@iDecode
      • 解释的要点。
      【解决方案4】:

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

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

      【讨论】:

        猜你喜欢
        • 2021-11-11
        • 2021-01-12
        • 1970-01-01
        • 2021-12-12
        • 2021-07-05
        • 2020-08-21
        • 1970-01-01
        • 2021-12-29
        • 2018-12-09
        相关资源
        最近更新 更多