Fluent passes values to any widget (when widgets need to be nested)

If we have such an application scenario:

After widgeta executes the click, the data is passed to the widget C under it through widget B.

Generally, you can pass the corresponding parameters to the specified widget tree by setting the constructor, as described in the following code:

Indicates that the click change content in widget a needs to be transferred to widget C in widget B for display;

You need to set the constructor of widget B, receive the corresponding parameters, and then pass them to widget C for display;

class Inheritedwidget extends StatefulWidget {
 @override
 _InheritedWidgetState createState() => _InheritedWidgetState();
}
class _InheritedWidgetState extends State<Inheritedwidget> {
 int count=0;
 @override
 void initState() {
  // TODO: implement initState
  super.initState();
 }
 @override
 Widget build(BuildContext context) {
  print(count);
  return Scaffold(
   appBar: AppBar(title: Text("inherited widget"),),body: Container(
   child: Center(
    child: Column(
     children: <Widget>[
      Text("class0"),class1(count),],floatingActionButton: FloatingActionButton(onPressed: (){
    return addCount();
   },child: Text("add"),);
 }
 void addCount() {
  setState(() {
   count=1+count;
  });
 }
}

WidgetB:

class class1 extends StatelessWidget {
 int count;
 class1(this.count);
 @override
 Widget build(BuildContext context) {
  return Container(
   child: Column(
     children: <Widget>[
      Text("class1"),class2(count),);
 }
}

widgetC:

class class2 extends StatelessWidget {
 int count;
 class2(this.count);

 @override
 Widget build(BuildContext context) {
  return Container(
   child: Center(
    child: Text("$count"),);
 }
}

Of course, the above methods can achieve the desired effects, but when there are multiple levels of widget nesting, the code readability is reduced. You can pass values to the specified widget through the following methods;

An intermediate class is provided through a ContentProvider similar to that in Android to transfer the data to be transferred to the specified widget through the intermediate class.

Intermediate class:

//countProvider类 提供count属性和child属性 用于与原widget相关联,
class CountProvider extends InheritedWidget{
 final int count;
 final Widget child;
 //构造方法
 CountProvider({this.count,this.child}):super(child:child);
 //提供方法获取到countprovider类对象
static CountProvider of(BuildContext context){
 return context.inheritFromWidgetOfExactType(CountProvider);
}
 @override
 bool updateShouldNotify(InheritedWidget oldWidget) {
  // TODO: implement updateShouldNotify
  return false;
 }
}

Wrap the widget to be displayed through the counter provider and pass in the value to be changed;

class Inheritedwidget extends StatefulWidget {
 @override
 _InheritedWidgetState createState() => _InheritedWidgetState();
}
class _InheritedWidgetState extends State<Inheritedwidget> {
 int count=0;
 @override
 Widget build(BuildContext context) {
  print(count);
  return CountProvider(
   count:count,child: Scaffold(
    backgroundColor: Colors.blue,appBar: AppBar(title: Text("inherited widget"),body: Container(
    child: Center(
     child: Column(
      children: <Widget>[
       Text("class0"),class1(),floatingActionButton: FloatingActionButton(onPressed: (){
     return addCount();
    },);
 }
 void addCount() {
  setState(() {
   count=1+count;
  });
 }
}

Update the corresponding widget using the data provided by the intermediate class.

class class2 extends StatelessWidget {
 @override
 Widget build(BuildContext context) {
  int count = CountProvider.of(context).count;
  return Container(
   child: Center(
    child: Text("$count"),);
 }
}

Through the above methods, you can pass the values that need to be changed in different widgets.

summary

The above is the flutter introduced by Xiaobian to you to transfer values to any widget (when it is necessary to transfer values for nested use of widgets). I hope it will be helpful to you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support to our website! If you think this article is helpful to you, welcome to reprint, please indicate the source, thank you!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>