简体   繁体   English

如何将一个小部件的值发送到同一屏幕上的另一个小部件? (扑)

[英]how to send a value from a widget to another on the same screen ? (flutter)

I'm trying to implement a slider that add a certain value to the initial value when checking out.我正在尝试实现一个 slider,它在结帐时为初始值添加一个特定的值。 But I'm not able to pass that extra value to my checkout API Call.但我无法将额外的价值传递给我的结帐 API 调用。

Here an example: https://imgur.com/a/mp5ytzW这里是一个例子: https://imgur.com/a/mp5ytzW

My slider is implemented inside a StatefullWidget called DonationView like this:我的 slider 在一个名为 DonationView 的 StatefullWidget 中实现,如下所示:

Widget getSlider(BuildContext context) {
    return Slider(
      min: 0,
      max: 2000,
      value: widget.extra.toDouble(),
      onChanged: ((value) {
        setState(() {
          widget.extra = value.toInt();
        });
      }),
    );
  }
}

I want to pass the value "extra" to the api call fetchStripePaymentIntent我想将值“额外”传递给 api 调用 fetchStripePaymentIntent

class DonateScreen extends StatefulWidget {
  final int itemId;
  final int conversationId;
  final int extraInCent;

  DonateScreen(@pathParam this.itemId, @pathParam this.conversationId, @pathParam this.extraInCent,
      {Key? key})
      : super(key: key);

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

class _DonateScreenState extends State<DonateScreen> {
  late Future<StripePaymentIntent> _stripePaymentIntent;
  @override
  void initState() {
    super.initState();
    setState(() {
      _stripePaymentIntent = Provider.of<PaymentIntentProvider>(context, listen: false)
          .fetchStripePaymentIntent(widget.itemId, widget.extraInCent);
    });
  }
@override
  Widget build(BuildContext context) {
    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: [
                                        DonateView(
                                          snapshot.data!,
                                          theItem,
                                          userProfile,
                                          widget.conversationId,
                                          state.ngos[item.ngoId]!,
                                          widget.extraInCent,
                                        ),
                                      ],
                                    );
                                  }
                                  return Container();
                                },
                              ),
                            ];

Thank you in advance for your help预先感谢您的帮助

setState(() {
    widget.extra = value.toInt();
});

This seems weird.这似乎很奇怪。 Widget members should be final .小部件成员应该是final In setState() , you usually update members of the enclosing State class.setState()中,您通常更新封闭State class 的成员。

To share values between widgets outside of the same State , you should check out state management .要在同一State之外的小部件之间共享值,您应该查看state 管理 It's essential.这是必不可少的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 onPressed 值发送到另一个小部件 Flutter - How to send onPressed value to another widget Flutter 将数据从小部件发送到 flutter 中的另一个小部件 - send data from widget to another widget in flutter 如何将 void 函数从同一个小部件传递给另一个小部件,然后将相同的 void 函数从另一个小部件返回到同一个小部件中颤振 - how to pass the void function from same widget to another widget and then return the same void function from another widget to same widget in flutter Flutter:从另一个小部件或屏幕清除文本字段 - Flutter : clear textfield from another widget or screen 如何从同一屏幕上的另一个有状态小部件接收数据? - How to receive data from another statefull widget on the same screen? 如何将图像变量从一个屏幕发送回另一个屏幕 - Flutter - How to send back an image variable from one screen to another - Flutter 如何从 Flutter 中的另一个小部件滚动小部件 - How to scroll a widget from another widget in Flutter 如何将列表中的文本字段数据发送到 flutter 中的另一个屏幕 - How to send text fields data from list to another screen in flutter Flutter 如何在屏幕上相对于另一个小部件放置一个小部件? - Flutter how to place a widget relative to another widget on screen? 如何在 Flutter 中将文本小部件数据从一个屏幕传递到另一个屏幕? - How to pass text widget data from one screen to another screen in Flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM