Flutter:使用Builder方法作为在小部件内声明变量的一种方式

huangapple go评论62阅读模式
英文:

Flutter: Using a Builder method as a way to declare variables inside a widget

问题

考虑以下小部件:

    class MainWidget extends StatelessWidget {
      const MainWidget({super.key});

      @override
      Widget build(BuildContext context) {
        bool condition1 = true;

        return Column(
          children: [
            // 根据condition1插入widget1
            if (condition1) ...[
              Builder(
                // 创建一个Builder小部件以声明'condition2'
                builder: (context) {
                  bool condition2 = true; // 仅作为示例,不是实际值
                  return condition2 ? const MyWidget2() : const MyWidget3();
                },
              )
            ]
          ],
        );
      }
    }

我试图根据条件显示不同的小部件。我将小部件包装在Builder中,因为我希望能够在嵌套小部件中声明变量(不,它们不能在小部件树的更高层声明)。

问题是,这是否是正确的方法?因为如果你例如有一个BlocBuilder,你监听一个名为'success'的特定状态,在该状态下,你可以显示项目或其他内容。我想不出在这种情况下声明变量的另一种方法。

英文:

Consider the following widget:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

class MainWidget extends StatelessWidget {
  const MainWidget({super.key});

  @override
  Widget build(BuildContext context) {
    bool condition1 = true;

    return Column(
      children: [
        // inserting widget1 based on condition1
        if (condition1) ...[
          Builder(
            // creating a Builder widget in order to decalre &#39;condition2&#39;
            builder: (context) {
              bool condition2 = true; // just an example, not actual value
              return condition2 ? const MyWidget2() : const MyWidget3();
            },
          )
        ]
      ],
    );
  }
}

<!-- end snippet -->

I'm trying to display different widgets based on conditions. I wrap the widgets inside a Builder since I want to be able to declare the variables inside the nested widget (No, they can't be declared higher up in the widget tree).

The question is, whether this is the right way to do that? Because what if you have a BlocBuilder for example, and you listen for a certain state of 'success', BUT within than state you could either show the items or something else? I can't think of another way to declare variables in such a scenario.

答案1

得分: 1

是的,你的想法完全没问题。如果你想让你的代码更清晰,你可以创建一个新的小部件,然后将条件放在构建方法中。
例如

class ConditionWidget extends StatelessWidget {
  const MainWidget({super.key});

  @override
  Widget build(BuildContext context) {
    bool condition2 = true;
    return condition2 ? const MyWidget2() : const MyWidget3();
  }

然后在MainWidget中

class MainWidget extends StatelessWidget {
  const MainWidget({super.key});

  @override
  Widget build(BuildContext context) {
    bool condition1 = true;

    return Column(
      children: [
        // 根据condition1插入widget1
        if (condition1) ...[
          ConditionWidget()
        ]
      ],
    );
  }
}
英文:

Yes, it's totally fine with your idea. If you want your code to be clearer, you can create a new widget and put the condition in the in the build method.
For example

class ConditionWidget extends StatelessWidget {
  const MainWidget({super.key});

  @override
  Widget build(BuildContext context) {
    bool condition2 = true;
    return condition2 ? const MyWidget2() : const MyWidget3();
  }

and then in the MainWidget

class MainWidget extends StatelessWidget {
  const MainWidget({super.key});

  @override
  Widget build(BuildContext context) {
    bool condition1 = true;

    return Column(
      children: [
        // inserting widget1 based on condition1
        if (condition1) ...[
          ConditionWidget()
        ]
      ],
    );
  }
}

huangapple
  • 本文由 发表于 2023年7月12日 22:24:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76671634.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定