我遇到了一个问题,乱七八糟

发布于 2025-01-22 06:52:03 字数 2074 浏览 0 评论 0原文

我在最大课程中,遇到了一个问题。我有4个由Builder创建的开关:

//here are variables used later to pass data into builder
  bool _glutenFree = false;
  bool _vegetarian = false;
  bool _vegan = false;
  bool _lactoseFree = false;

Widget _buildSwitchListTile(
String title,
String description,
bool currentValue,
Function(bool) updateValue,


) {
    return SwitchListTile(
      title: Text(title),
      value: _glutenFree,
      subtitle: Text(description),
      onChanged: updateValue,
    );
  }

当我尝试单击第二,第三,第4个开关时,什么也不会发生。 但是,当我尝试单击第一个开关时,所有四个开关。

这是_buildSwitchlisttile的使用方式:

_buildSwitchListTile(
                'Gluten-free',
                'Only include gluten-free meals.',
                _glutenFree,
                (newValue) {
                  setState(
                    () {
                      _glutenFree = newValue;
                    },
                  );
                },
              ),
              _buildSwitchListTile(
                'Lactose-free',
                'Only include Lactose-free meals.',
                _lactoseFree,
                (newValue) {
                  setState(
                    () {
                      _lactoseFree = newValue;
                    },
                  );
                },
              ),
              _buildSwitchListTile(
                'Vegetarian',
                'Only include Vegetarian meals.',
                _vegetarian,
                (newValue) {
                  setState(
                    () {
                      _vegetarian = newValue;
                    },
                  );
                },
              ),
              _buildSwitchListTile(
                'Vegan',
                'Only include Vegan meals.',
                _vegan,
                (newValue) {
                  setState(
                    () {
                      _vegan = newValue;
                    },
                  );
                },
              ),

我知道Max的课程现在已经过时了,我通常会自己分析问题,但是这里的一切似乎都是合乎逻辑的。有人遇到过类似问题吗?

I'm on the max course, and ran into a problem. I have 4 switches which are created by builder:

//here are variables used later to pass data into builder
  bool _glutenFree = false;
  bool _vegetarian = false;
  bool _vegan = false;
  bool _lactoseFree = false;

Widget _buildSwitchListTile(
String title,
String description,
bool currentValue,
Function(bool) updateValue,


) {
    return SwitchListTile(
      title: Text(title),
      value: _glutenFree,
      subtitle: Text(description),
      onChanged: updateValue,
    );
  }

When I try to click the 2nd, 3rd, 4th switch, nothing happens.
But when I try to click the first one, all four switch.

here is how _buildSwitchListTile is used:

_buildSwitchListTile(
                'Gluten-free',
                'Only include gluten-free meals.',
                _glutenFree,
                (newValue) {
                  setState(
                    () {
                      _glutenFree = newValue;
                    },
                  );
                },
              ),
              _buildSwitchListTile(
                'Lactose-free',
                'Only include Lactose-free meals.',
                _lactoseFree,
                (newValue) {
                  setState(
                    () {
                      _lactoseFree = newValue;
                    },
                  );
                },
              ),
              _buildSwitchListTile(
                'Vegetarian',
                'Only include Vegetarian meals.',
                _vegetarian,
                (newValue) {
                  setState(
                    () {
                      _vegetarian = newValue;
                    },
                  );
                },
              ),
              _buildSwitchListTile(
                'Vegan',
                'Only include Vegan meals.',
                _vegan,
                (newValue) {
                  setState(
                    () {
                      _vegan = newValue;
                    },
                  );
                },
              ),

I know that the Max's course is a bit outdated now,I usually analyze the problems myself, but everything seems logical here. Has anyone ever had a similar problem?

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

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

发布评论

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

评论(1

勿忘初心 2025-01-29 06:52:03

将_glutenfree更改为CurrentValue

    //here are variables used later to pass data into builder
    bool _glutenFree = false;
    bool _vegetarian = false;
    bool _vegan = false;
    bool _lactoseFree = false;

    Widget _buildSwitchListTile(
    String title,
    String description,
    bool currentValue,
    Function(bool) updateValue,


    ) {
     return SwitchListTile(
      title: Text(title),
      value: currentValue,    // change _glutenFree to currentValue
      subtitle: Text(description),
      onChanged: updateValue,
    );
  }

change _glutenFree to currentValue

    //here are variables used later to pass data into builder
    bool _glutenFree = false;
    bool _vegetarian = false;
    bool _vegan = false;
    bool _lactoseFree = false;

    Widget _buildSwitchListTile(
    String title,
    String description,
    bool currentValue,
    Function(bool) updateValue,


    ) {
     return SwitchListTile(
      title: Text(title),
      value: currentValue,    // change _glutenFree to currentValue
      subtitle: Text(description),
      onChanged: updateValue,
    );
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文