Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
778 views
in Technique[技术] by (71.8m points)

dart - Emit the data to parent Widget in Flutter

I'm trying to set the text from child widget to parent widget. But the text is not reflecting in parent widget.

Tried to use setState() also but still unable to get expected result.

Following is my code:

void main() => runApp(new TestApp());

class TestApp extends StatefulWidget {
  @override
  _TestState createState() => new _TestState();
}

class _TestState extends State<TestApp>{

  String abc = "";

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        body: new Column(
          children: <Widget>[
            new Text("This is text $abc"),
            TestApp2(abc)
          ],
        ),
      ),
    );
  }
}


class TestApp2 extends StatefulWidget {

  String abc;

  TestApp2(this.abc);

  @override
  _TestState2 createState() => new _TestState2();
}

class _TestState2 extends State<TestApp2>{
  @override
  Widget build(BuildContext context) {
    return new Container(
      width: 150.0,
      height: 30.0,
      margin: EdgeInsets.only(top: 50.0),
      child: new FlatButton(
          onPressed: (){
            setState(() {
              widget.abc = "RANDON TEXT";
            });
          },
        child: new Text("BUTTON"),
        color: Colors.red,
      ),
    );
  }
}

Am i missing something ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your example, a few assumptions were made. I will try to remove one by one.

  1. You pass abc from parent to child and you mutated the child value on press on button. As primitive types are pass by value in dart, change in the value of abc in child will not change the value of parent abc. Refer the below snippet.

    void main() {
      String abc = "oldValue";
      changeIt(abc);
      print(abc); // oldValue
    }
    
    void changeIt(String abc) {
      abc = "newValue";
      print(abc); //newValue
    }
    
  2. Let's assume the first one is wrong(for understanding purpose). Then changing the value of abc in child will change the value of abc in parent. But without calling that inside setState of parent, parent will not reflect the change. In your case if you change the code as below, it will change the button text alone on click (as setState of child is called).

      new FlatButton(
        onPressed: () {
          setState(
            () {
              widget.abc = "RANDON TEXT";
            },
          );
        },
        child:
            new Text(widget.abc), // setting the text based on abc
        color: Colors.red,
      ),
    
  3. Instead of using globalState which will be very difficult to maintain/debug as app grows, I would recommend using callbacks. Please refer the below code.

        void main() => runApp(new TestApp());
    
        class TestApp extends StatefulWidget {
          @override
          _TestState createState() => new _TestState();
        }
    
        class _TestState extends State<TestApp> {
          String abc = "bb";
    
          callback(newAbc) {
            setState(() {
              abc = newAbc;
            });
          }
    
          @override
          Widget build(BuildContext context) {
            var column = new Column(
              children: <Widget>[
                new Text("This is text $abc"),
                TestApp2(abc, callback)
              ],
            );
            return new MaterialApp(
              home: new Scaffold(
                body: new Padding(padding: EdgeInsets.all(30.0), child: column),
              ),
            );
          }
        }
    
        class TestApp2 extends StatefulWidget {
          String abc;
          Function(String) callback;
    
          TestApp2(this.abc, this.callback);
    
          @override
          _TestState2 createState() => new _TestState2();
        }
    
        class _TestState2 extends State<TestApp2> {
          @override
          Widget build(BuildContext context) {
            return new Container(
              width: 150.0,
              height: 30.0,
              margin: EdgeInsets.only(top: 50.0),
              child: new FlatButton(
                onPressed: () {
                  widget.callback("RANDON TEXT"); //call to parent
                },
                child: new Text(widget.abc),
                color: Colors.red,
              ),
            );
          }
        }
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...