【发布时间】:2019-07-20 11:53:21
【问题描述】:
我正在尝试在标题旁边创建一个按钮,但不是在开头传递内容,而是在尝试动态执行它,因此我使用了构造函数,而不是在调用构造函数时获取传递的值我一直为空。那么如何使用构造函数或任何其他方式动态传递参数呢?这是我正在使用的类
class Header extends StatefulWidget {
String buttonTitle;
String title;
Header(String buttonTitle , String title) //the constructor I'm gonna
call
{
this.buttonTitle = buttonTitle;
this.title = title;
}
@override
HeaderState createState() => HeaderState(buttonTitle , title);
}
class HeaderState extends State<Header> {
String buttonTitle;
String title;
HeaderState(String buttonTitle , String title)
{
this.buttonTitle = buttonTitle;
this.title = title;
}
@override
Widget build(BuildContext context) {
//some code
Text("$title", //the first parameter passed dynamically),
RaisedButton(
child: Text(
"$buttonTitle", //the second parameter
),
}
class FirstScreen extends StatefulWidget {
@override
FirstScreenState createState() => FirstScreenState();
}
class FirstScreenState extends State<FirstScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
Header("OK", "Page1"),//constructor is called here
);
}
}
【问题讨论】:
标签: dynamic flutter dart widget