我有一个用于创建按钮的模板小部件和一个家庭成员类,例如,我可以将所述类的对象作为参数传递给模板,以便按钮显示该家庭成员的名字吗?
class Template extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
height: 70,
width: 120,
child: Stack(
alignment: Alignment.center,
children: [
FlatButton(
onPressed: (){print('hey');},
child: Text(members.name,
textAlign: TextAlign.justify)
),
模板的其余部分无关紧要,所以我将其剪掉
答案 0 :(得分:0)
就像常规对象一样,只需声明您的变量并在构造函数中引用它即可,
class Template extends StatelessWidget {
final members;
Template(this.members) : super();
...
}
答案 1 :(得分:0)
是的,
class Template extends StatelessWidget {
Family members;
Template(this.members);
@override
Widget build(BuildContext context) {
return Container(
height: 70,
width: 120,
child: Stack(
alignment: Alignment.center,
children: [
FlatButton(
onPressed: (){print('hey');},
child: Text(members.name,
textAlign: TextAlign.justify)
),