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

bnlyeluc  于 2022-12-14  发布在  Flutter
关注(0)|答案(5)|浏览(155)

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

inkz8wg9

inkz8wg91#

只需将你的标题 Package 在一个GestureDetector中来处理点击。然后调用NavigatorpushNamed来重定向到一个新的路径。

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

64jmpszr2#

使用InkWell
这也会给你带来很好的涟漪React

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"),
        )
ltskdhd1

ltskdhd13#

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

anauzrmj4#

你可以使用TextButton,因为它有一个透明的背景,看起来像一个文本小部件。

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

u7up0aaq5#

使用container将文本换行,然后再次使用小部件**“GestureDetector”将文本换行,并按以下方式使用onTap()**

onTap: () {
                    Navigator.of(context).pushReplacement(
                        MaterialPageRoute(builder: ((context) => *className()*));
                  },

相关问题