Hi all,
I'm trying to implement a slider that add a certain value to the initial value when checking out. But I'm not able to pass that extra value to my checkout API Call, that is in the initSate. Any idea how to update the value inside initState?
Here an example:
https://imgur.com/a/mp5ytzW
<pre>class DonateScreen extends StatefulWidget {
final int itemId;
final int conversationId;
var extra;
DonateScreen(@pathParam this.itemId, @pathParam this.conversationId, {Key? key})
: super(key: key) {
extra = 0;
}
@override
_DonateScreenState createState() => _DonateScreenState();
}
class _DonateScreenState extends State<DonateScreen> {
late Future<StripePaymentIntent> _stripePaymentIntent;
int finalAmount = 0;
@override
void initState() {
super.initState();
setState(() {
_stripePaymentIntent = Provider.of<PaymentIntentProvider>(context, listen: false)
.fetchStripePaymentIntent(widget.itemId, widget.extra);
print('old value: ${widget.extra}');
});
}
@override
Widget build(BuildContext context) {
const sizedBoxSpace = SizedBox(height: 24);
return BlocBuilder<ItemBloc, ItemState>(
builder: (context, state) {
if (state is ItemLoadedState) {
Item item = state.item;
return Scaffold(
appBar: AppBar(
title: Text(
"Pay".tr(),
),
),
body: SingleChildScrollView(
child: BlocBuilder<ProfileBloc, ProfileState>(
builder: ((context, state) {
if (state is ProfileLoadedState) {
UserProfile userProfile = state.userProfile;
return FutureBuilder<StripePaymentIntent>(
future: _stripePaymentIntent,
builder:
(BuildContext context, AsyncSnapshot<StripePaymentIntent> snapshot) {
List<Widget> children;
if (snapshot.hasData) {
Item theItem = item;
children = <Widget>[
BlocBuilder<NgosBloc, NgosState>(
builder: (context, state) {
if (state is NgosLoadedState) {
return Column(
children: [
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 15, vertical: 15),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
sizedBoxSpace,
UserProfileWidget(userProfile),
sizedBoxSpace,
Text(
"donate1".tr(args: [userProfile.firstname]),
style: MyTheme.black,
),
Text("donate2".tr(), style: MyTheme.black),
const SizedBox(height: 10),
Text("donate3".tr(), style: MyTheme.orange600),
sizedBoxSpace,
RichText(
text: TextSpan(
style: MyTheme.mediumBlack,
children: <TextSpan>[
TextSpan(text: 'donate4'.tr()),
TextSpan(
text: Utils.getDisplayPrice(finalAmount),
style: const TextStyle(
color: MyTheme.orange)),
TextSpan(text: 'donate5'.tr()),
TextSpan(
text: state.ngos[theItem.ngoId]!.name,
style: const TextStyle(
color: MyTheme.primary)),
TextSpan(text: 'donate6'.tr()),
],
),
),
Slider(
min: 0,
max: 2000,
label: widget.extra.toString(),
value: widget.extra.toDouble(),
onChanged: ((value) {
setState(() {
widget.extra = value.toInt();
print(widget.extra);
finalAmount = theItem.amountInCent +
widget.extra as int;
});
}),
),
PaymentsWidget(
snapshot.data!,
widget.conversationId,
),
],
))
],
);
}
return Container();
},
),
];
What I have tried:
I tried this from
https://stackoverflow.com/questions/55021127/pass-parameter-to-initstate but it's not working :(
@override
void didUpdateWidget(DonateScreen oldWidget) {
if (oldWidget.extra != widget.extra) {
_stripePaymentIntent = Provider.of<PaymentIntentProvider>(context, listen: false)
.fetchStripePaymentIntent(widget.itemId, widget.extra);
}
print('new value: ${widget.extra}');
super.didUpdateWidget(oldWidget);
}