Flutter:如何从子小部件更新子(和父)小部件的状态(父小部件获取更新,子小部件不更新)

发布于 2025-01-09 01:53:16 字数 3026 浏览 1 评论 0原文

我为自己创建了一个小部件,它可以创建带有文本的表单开关,也可以仅返回该开关。现在我想在我的过滤器(父级)中多次使用这个名为 PlatformSwitch (子级)的小部件。

为了纠正过滤器中选定的值,我需要将选定值(truefalse)从每个开关传递回过滤器。我已经尝试解决这个问题,但我根本无法将谷歌的解决方案适应我的代码。

即使我选择了正确的值,onChange 方法也会被触发,但是再次传递给子级的值是旧值。怎么会这样呢?

我已经尝试将我的 PlatformSwitch 包装在 GetXObx 中,但这会导致完全相同的问题。有谁知道如何再次更新子值?

打印值:

flutter: Selected: false <-- Initialized
flutter: State update: true <-- Updated
flutter: Selected: false <-- Reinitialized with wrong value!

Filter(父):

class FilterModal extends StatefulWidget {
  const FilterModal({Key? key}) : super(key: key);

  @override
  State<FilterModal> createState() => _FilterModalState();
}

class _FilterModalState extends State<FilterModal> {
  bool showExpiredProducts = false;

  _FilterModalState();

  final FilterController filterController = Get.put(FilterController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PlatformSwitch(
          selected: showExpiredProducts,
          onlySwitch: false,
          prefix: "Abgelaufen Angebote anzeigen",
          onChanged: (value) {
            setState(() {
              print("State update: " + value.toString());
              showExpiredProducts = value;
            });
          }),
    );
  }
}

PlatformSwitch(子):

class PlatformSwitch extends StatefulWidget {
  String? prefix;
  bool onlySwitch;
  bool selected;
  ValueChanged onChanged;

  PlatformSwitch(
      {Key? key,
      this.selected = false,
      this.onlySwitch = true,
      this.prefix,
      required this.onChanged})
      : super(key: key);

  @override
  // ignore: no_logic_in_create_state
  _PlatformSwitchState createState() => _PlatformSwitchState(
      selected,
      onlySwitch,
      prefix,
      onChanged);
}

class _PlatformSwitchState extends State<PlatformSwitch> {
  String? prefix;
  bool onlySwitch;
  bool selected;
  ValueChanged onChanged;

  _PlatformSwitchState(
      this.selected,
      this.onlySwitch,
      this.prefix,
      this.onChanged);

  @override
  Widget build(BuildContext context) {
    print("Selected: " + selected.toString());

    if (!onlySwitch) {
      return platformFormRowSwitch();
    }

    return platformSwitch();
  }

  platformFormRowSwitch() {
    return Container(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Column(
              children: [Text(prefix!)],
            ),
            Column(
              children: [platformSwitch()],
            )
          ],
        ));
  }

  platformSwitch() {
    if (defaultTargetPlatform == TargetPlatform.android) {
      return Switch(
        value: selected,
        onChanged: onChanged
      );
    }

    return CupertinoSwitch(
      value: selected,
      onChanged: onChanged,
    );
  }
}

亲切的问候,谢谢!

I have created myself a widget which creates either a form switch with text or just returns the switch. Now I want to use this widget called PlatformSwitch (child) multiple times in my filter (parent).

In order to have to correct selected values in my filter I need to pass the selected values (either true or false) back to the filter from each switch. I have already tried to solve this but I am simply not able to adapt the solutions from google to my code.

The onChange method gets fired even with the correct value I have selected, however the value passed to the child again is the old value. How can this be?

I already tried to wrap my PlatformSwitch in an Obx from GetX but this leads to the exact same problem. Does anyone know how to update the child value again?

Printed values:

flutter: Selected: false <-- Initialized
flutter: State update: true <-- Updated
flutter: Selected: false <-- Reinitialized with wrong value!

Filter (parent):

class FilterModal extends StatefulWidget {
  const FilterModal({Key? key}) : super(key: key);

  @override
  State<FilterModal> createState() => _FilterModalState();
}

class _FilterModalState extends State<FilterModal> {
  bool showExpiredProducts = false;

  _FilterModalState();

  final FilterController filterController = Get.put(FilterController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PlatformSwitch(
          selected: showExpiredProducts,
          onlySwitch: false,
          prefix: "Abgelaufen Angebote anzeigen",
          onChanged: (value) {
            setState(() {
              print("State update: " + value.toString());
              showExpiredProducts = value;
            });
          }),
    );
  }
}

PlatformSwitch (child):

class PlatformSwitch extends StatefulWidget {
  String? prefix;
  bool onlySwitch;
  bool selected;
  ValueChanged onChanged;

  PlatformSwitch(
      {Key? key,
      this.selected = false,
      this.onlySwitch = true,
      this.prefix,
      required this.onChanged})
      : super(key: key);

  @override
  // ignore: no_logic_in_create_state
  _PlatformSwitchState createState() => _PlatformSwitchState(
      selected,
      onlySwitch,
      prefix,
      onChanged);
}

class _PlatformSwitchState extends State<PlatformSwitch> {
  String? prefix;
  bool onlySwitch;
  bool selected;
  ValueChanged onChanged;

  _PlatformSwitchState(
      this.selected,
      this.onlySwitch,
      this.prefix,
      this.onChanged);

  @override
  Widget build(BuildContext context) {
    print("Selected: " + selected.toString());

    if (!onlySwitch) {
      return platformFormRowSwitch();
    }

    return platformSwitch();
  }

  platformFormRowSwitch() {
    return Container(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Column(
              children: [Text(prefix!)],
            ),
            Column(
              children: [platformSwitch()],
            )
          ],
        ));
  }

  platformSwitch() {
    if (defaultTargetPlatform == TargetPlatform.android) {
      return Switch(
        value: selected,
        onChanged: onChanged
      );
    }

    return CupertinoSwitch(
      value: selected,
      onChanged: onChanged,
    );
  }
}

Kind regards and thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一花一树开 2025-01-16 01:53:16

StateStatefulWidget 更长寿。当使用新参数重新创建小部件实例时,旧的状态实例将被保留。

要获取状态中的更新数据,您有两种选择:

State outlives StatefulWidget. When widget instance is recreated with new parameters, old state instance is kept.

To get updated data in state, you have two options:

  • access data from widget using widget.something
  • update state's fields in didUpdateWidget
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文