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;
});
},
),
],
);
}));
}