【发布时间】:2018-08-10 13:54:50
【问题描述】:
我有一个Text widget,按下它必须显示另一个Route。但是我看不到Text widget 的任何 onPressed() 方法。请帮忙。
【问题讨论】:
我有一个Text widget,按下它必须显示另一个Route。但是我看不到Text widget 的任何 onPressed() 方法。请帮忙。
【问题讨论】:
只需将您的标题包装在 GestureDetector 中即可处理点击。然后调用Navigator 的pushNamed 重定向到新路由。
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
),
),
【讨论】: