【发布时间】:2020-06-21 00:37:42
【问题描述】:
我需要帮助,我是新来的 Flutter。我想从我的函数中获取返回值 String 并在我的 Text Widget 中获取它。那不是我真正的代码,但我希望它像那样工作。而且每次我按下按钮时,文本小部件上的字符串值也会发生变化。谢谢
String textReturn(String value){
String text = "value";
return text.toString();
}
class SamplePage extends StatefulWidget {
@override
_SamplePageState createState() => _SamplePageState();
}
class _SamplePageState extends State<SamplePage> {
@override
Widget build(BuildContext context) {
return Center(
child: Container(
child: Column(
children: <Widget>[
FlatButton(
onPressed: (){
textReturn("This is the value");
},
child: Text("Button")
),
Text(
textReturn
)
],
),
),
);
}
}
【问题讨论】:
-
使用
setState(() {someStringVariable = textReturn("This is the value");});和Text(someStringVariable)但实际上你不需要textReturn- 只需使用setState(() {someStringVariable = "This is the value";});或setState(() => someStringVariable = "This is the value"); -
@pskink 我认为这应该是答案
-
@epsilon 所以把它写成一个自我回答