假设我有一些像这样的Flutter代码:
// ...
body: new Center(
child: new Text(
'Hello, I am some text',
),
),
// ...
Run Code Online (Sandbox Code Playgroud)
如何让屏幕上的文字响应水龙头?(例如,当我点击文本时,只需打印到日志.)
谢谢!
Ale*_*ard 30
如此答案所示,您可以使用InkWell或手势检测器.
例如
InkWell(
child: Text("Hello"),
onTap: () {print("value of your text");},
)
Run Code Online (Sandbox Code Playgroud)
要么
var textValue = "Flutter"
InkWell(
child: Text(textValue),
onTap: () {print(textValue);},
)
Run Code Online (Sandbox Code Playgroud)
编辑:正如Collin Jackson建议的那样,你也可以使用FlatButton
FlatButton(
onPressed: () {print("Hello world");},
child: Text("Hello world"),
);
Run Code Online (Sandbox Code Playgroud)
如果您不需要或不需要材料(FlatButton,InkWell等),您可以使用GestureDetector:
GestureDetector(
onTap: () { print("I was tapped!"); },
child: Text("Hello world"),
)
Run Code Online (Sandbox Code Playgroud)
您可以将富文本用于可点击文本:-
相应地划分 Text Span 中的文本,并在您想要点击的文本中使用点击手势。
TapGestureRecognizer _termsConditionRecognizer;
TapGestureRecognizer _privacyPolicyRecognizer;
@override
void dispose() {
_privacyPolicy.dispose();
_termsCondition.dispose();
super.dispose();
}
@override
void initState() {
super.initState();
_termsConditionRecognizer = TapGestureRecognizer()
..onTap = () {
print("Terms and condition tapped");
};
_privacyPolicyRecognizer = TapGestureRecognizer()
..onTap = () {
print("Provacy Policy tapped");
};
}
@override
Widget build(BuildContext context) {
return Container(
child: Center(
child: RichText(
text: TextSpan(
text: 'By signing up you agree to the ',
children: [
TextSpan(
text: 'Terms And Condition',
recognizer: _termsConditionRecognizer,
),
TextSpan(
text: ' and ',
),
TextSpan(
text: 'Privacy Policy',
recognizer: _privacyPolicyRecognizer,
),
],
),
),
),
);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
7679 次 |
最近记录: |