[英]Flutter passing data to children widgets
Passing data from parent to child can be messy the more widgets are used, child to a child to a child.使用的小部件越多,从父到子传递数据可能会很混乱,从子到子再到子。 Is there a way to use the data of the first father without keep passing it, provider is used to pass data between widgets on the same level, between screens, but i mean cases there is many children for one screen.
有没有办法使用第一个父亲的数据而不继续传递它,提供程序用于在同一级别的小部件之间,屏幕之间传递数据,但我的意思是一个屏幕有很多孩子的情况。
import 'package:flutter/material.dart';
class FirstFather extends StatelessWidget {
const FirstFather({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Child(
prop: "exmaple",
);
}
}
class Child extends StatelessWidget {
final String prop;
const Child({Key key, this.prop}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
child: Grandson(
prop: prop,
),
);
}
}
class Grandson extends StatelessWidget {
final String prop;
const Grandson({Key key, this.prop}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
child: Text(prop),
);
}
}
Use InheritedWidget:使用 InheritedWidget:
https://api.flutter.dev/flutter/widgets/InheritedWidget-class.html https://api.flutter.dev/flutter/widgets/InheritedWidget-class.html
When you put it at the top of your Widget tree, all the children can access the data.当您将它放在 Widget 树的顶部时,所有的孩子都可以访问数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.