5

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

Roger Cuesta
  • 902
  • 1
  • 8
  • 12

4 Answers4

23

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: ...
        );
    }
}
tudorprodan
  • 950
  • 8
  • 18
  • Thanks for all , in my case the second widget is in other archive so this works with the same parameters ? – Roger Cuesta Aug 28 '18 at 07:12
  • 1
    What do you mean by "other archive" ? You mean it's in another file/directory ? If yes, then it doesn't matter, it should still work. – tudorprodan Aug 28 '18 at 10:51
  • Im new in Flutter so sorry if I said something wrong . I have in a file a Widget that inside that have other widget that the code is in other file . For this I know its so easy to get a parameter from that widget I call – Roger Cuesta Aug 28 '18 at 13:29
  • No problem, then you can use my proposed solution, it doesn't matter if the widgets are in different files. – tudorprodan Aug 28 '18 at 14:41
  • 1
    that's a great explanation. It really helped me. Thanks! – A.Ktns Nov 27 '21 at 00:46
3

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;
                        });
                      },
                    ),
            ],
          );
        }));
  }
Sana.91
  • 1,999
  • 4
  • 33
  • 52
1

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

Dinesh Balasubramanian
  • 20,532
  • 7
  • 64
  • 57
0

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.

bnayagrawal
  • 1,044
  • 3
  • 12
  • 22