1

I was able to pass the data widget.value from the FirstPage to SecondPage. There's a widget called thirdWidget inside SecondPage. How do I pass widget.value to thirdWidget?

class FirstPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => FirstPageState();
}

class FirstPageState extends State< FirstPage > {
  final myController = TextEditingController();

  @override
  void dispose() {
    myController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        title: Text('First Route'),
      ),
      body: Column(
        children: <Widget>[
          TextField(
            controller: myController,
            decoration: new InputDecoration(labelText: "Enter a number"),
            keyboardType: TextInputType.number,
          ),

          RaisedButton(
            child: Text("show text"),
            onPressed: () {
                    return Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (context) => ThirdRoute(
                              selectedDate: selectedDate,
                              value: myController.text,
                            )),
                  );
              },
             );
            },
          ),
        ],
      ),
    );
  }
}



class SecondPage extends StatefulWidget {
  final String value;

  ThirdRoute({Key key, this.value})
      : super(key: key);

  @override
  SecodpageState createState() => SecodpageState();
}

class SecodpageState extends State< SecondPage > {
  @override
  void initState() {
    super.initState();
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Calendar Page"),
      ),
      body: Column(
        children: <Widget>[
          Text("${widget.value}"),
          Row(
            children: thirdWidget(),
          ),
          Center(
            child: RaisedButton(
              onPressed: () {
                Navigator.pop(context);
              },
              child: Text('Go back!'),
            ),
          ),
        ],
      ),
    );
  }
}


List<Widget> thirdWidget() {
   return Text("${widget.value}”)
}

2 Answers 2

1

Use this in your SecondPage

Row(
  children: thirdWidget(widget.value),
)

And update your thirdWidget like:

List<Widget> thirdWidget(var data) {
  // data is widget.value
  return [];
}
Sign up to request clarification or add additional context in comments.

Comments

0

Just pass that info into the state class. Something like that:

class SecondPage extends StatefulWidget {
  final String value;

  ThirdRoute({Key key, this.value})
      : super(key: key);

  @override
  SecodpageState createState() => SecodpageState(value);
}

class SecodpageState extends State< SecondPage > {
  final String value;

  SecodpageState(this.value);

  @override
  void initState() {
    super.initState();
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Calendar Page"),
      ),
      body: Column(
        children: <Widget>[
          Text("${widget.value}"),
          Row(
            children: thirdWidget(),
          ),
          Center(
            child: RaisedButton(
              onPressed: () {
                Navigator.pop(context);
              },
              child: Text('Go back!'),
            ),
          ),
        ],
      ),
    );
  }
}


List<Widget> thirdWidget() {
   return Text(value);
}

1 Comment

this one isn't working Text(value); is return value is undefined.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.