How I can get data from other Widget?

Viewed 11338

I want to get data from specific widget , I search that exist SQLite or SharePreferences but I guess doesn't needed .

That I want is when I click in event onTap take that in my case a String and in the parent widget get that value of the String . I know how to pass data from parent to child widget but not in reverse .

Example : !1

When I press for example "Journey UNI" , save that String and get that in the parent widget that is :

!2

4 Answers

You can make your parent pass a callback function to your child widget. Then in the child widget when you want to communicate a value back to your parent you call the callback with the value.

...
ParentWidget(
    child: ChildWidget(
        onSelectParam: (String param) {
            // do something with param
        }
    ),
)
...


class ChildWidget extends StatelessWidget {
    ChildWidget({this.onSelectParam});

    String yourParam;
    Function(String) onSelectParam;

    @override
    Widget build(BuildContext context) {
        return InkWell(
            onTap: () {
                onSelectParam(yourParam);
            },
            child: ...
        );
    }
}

Use callbacks to pass/receive data between widgets

for example you declare a call back and a model class that contains data you want to pass to your child widget. Following is the code for child widget

class CustomerInformation extends StatefulWidget {
  CustomerInformation({Key key, this.paymentModel, this.onContinue})
      : super(key: key);

  final VoidCallback onContinue;
  final PullPaymentData paymentModel;

  @override
  State<StatefulWidget> createState() {
    return _CustomerInformationState();
  }
}

    class _CustomerInformationState extends State<CustomerInformation> {
//useless code omitted
            Padding(
              padding: EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
              child: RaisedButton(
                onPressed: () async {
                    widget.onContinue();
                  }
                },
                child: Text(
                  Translations.of(context).sendOtpButton,
                ),
              ),
            ),
//useless code omitted
  }

paymentModel will be passed as Ref, and as the RaisedButton in above code is pressed control is transferred back to caller(parent), which receives the updated model:

Following is the sample code for caller(parent) widget which calls the above widget

    class _PullPaymentScreenState extends State<PullPaymentScreen> {
  PullPaymentData model = PullPaymentData(
      identifier: "", isSendOtpSuccess: false, amount: "", currentStep: 0);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: getAppBar(),
        body: Builder(builder: (BuildContext context) {
          return Column(
            children: <Widget>[
              model.currentStep == 0
                  ? CustomerInformation(
                      paymentModel: model,
                      onContinue: () {
//i recieve updated model HERE
                        setState(() {
                          if (model.isSendOtpSuccess) { 
                            ++model.currentStep;
                            Scaffold.of(context).showSnackBar(SnackBar(
                                  content:
                                      Text(Translations.of(context).otpSuccess),
                                  duration: Duration(seconds: 4),
                                ));
                          }
                        });
                      })
                  : SubmitPayment(
                      identifier: model.identifier,
                      amount: model.amount,
                      onResendPressed: () {
                        setState(() {
                          --model.currentStep;
                        });
                      },
                    ),
            ],
          );
        }));
  }

We can use callbacks for children widgets to talk to parent widget. Please refer this example. Here RootPage is parent and FeedPage is child.

For simple types you can refer to @tudorprodan

If you want to get the value of a widget you created which contains a textField then pass the TextEditingController to your widget's constructor. And so you can use the controller from outside the widget to get the value. Also, remember to dispose the controller when done.

Related