[英]Flutter differentiating between multiple instances of widget
I'm writing an app where I'm using multiple instance of a custom widget, and i want to call a method of a specific instance.我正在编写一个应用程序,我在其中使用自定义小部件的多个实例,并且我想调用特定实例的方法。
Sample of the code:代码示例:
Widget build(BuildContext context) {
return Scaffold(
drawer: new DrawerOnly(storage: CounterStorage()),
appBar: AppBar(),
body: Stack(
children: <Widget>[
new Center(
child: new Object3D(
),
),
new Positioned(
top: 20,
left: 0,
child: new Object3D(
),
),
new Positioned(
top: 20,
left: 100,
child: new Object3D(
),
),
new Positioned(
top: 120,
left: 0,
child: new Object3D(
),
),
],
)
);
}
mymethod() {
//call first Object3D's method from here
}
}
how do i call the first Object3D's method from their parent?我如何从他们的父级调用第一个 Object3D 的方法?
You store your children in a list like this:您将孩子存储在这样的列表中:
final object3Ds = List<Object3D>.generate(4, (_) => Object3D());
Now, you store that reference in your parent and then you can pass the elements of that list in your build
method:现在,您将该引用存储在您的父级中,然后您可以在您的build
方法中传递该列表的元素:
...
Stack(
children: <Widget>[
Positioned(
child: object3Ds[0],
...
Finally, you can now access your objects using the list you created:最后,您现在可以使用您创建的列表访问您的对象:
object3Ds[0].callMethod();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.