简体   繁体   English

Flutter 切换收音机小部件

[英]Flutter toggle Radio widget

I'm using Radio widgets to set the user gender.我正在使用Radio小部件来设置用户性别。 These Radio button widgets are designed very well to handle the selection changes act accordingly, but I does not find a solution to somehow deselect one already selected option.这些Radio按钮小部件设计得非常好,可以相应地处理选择更改,但我没有找到解决方案以某种方式取消选择一个已选择的选项。 I would like the gender radio group to be optional.我希望性别广播组是可选的。 But after user selects let's say female, he/she can change the selection to male, but can't deselect the female/male option.但是在用户选择让我们说女性之后,他/她可以将选择更改为男性,但不能取消选择女性/男性选项。 (So that none of the option is selected.) (因此没有选择任何选项。)

There is only an onChanged property and it only changes if the gender changes.只有一个onChanged属性,并且只有在性别发生变化时才会发生变化。 No such thing like onTap or something similar.没有像onTap这样的东西或类似的东西。 So no matter if I check that gender if it's the same, onChanged won't be called.因此,无论我是否检查该性别是否相同,都不会调用onChanged

For that reason I tried to use a GestureDetector which should solve this issue (with this I should be able to deselect the already selected option) but in case of radio widgets it does not work.出于这个原因,我尝试使用应该解决这个问题的GestureDetector (有了这个我应该能够取消选择已经选择的选项),但如果是无线电小部件,它就不起作用。 I also tried to change it's behavior property but it did not help either.我也试图改变它的behavior属性,但它也没有帮助。


Here is the function which I use to create gender radio option with a text in a Row .这是 function ,我用它来创建带有文本的性别单选选项Row

Widget _buildRadioWithText(final genderChangeNotifier, final Gender genderParam, final String genderText) {
    return Row(
      children: <Widget>[
        GestureDetector(
          behavior: HitTestBehavior.translucent,
          child: Radio<Gender>(
            visualDensity: VisualDensity.compact,
            value: genderParam,
            groupValue: genderChangeNotifier.gender,
            onChanged: (Gender gender) {
              genderChangeNotifier.gender = gender;
              print(gender.toString());
            },
          ),
          onTap: () {
            if (genderChangeNotifier.gender == genderParam) {
              genderChangeNotifier.gender = Gender.NA;
              print("Not answered!");
            }
          },
        ),
        GestureDetector(
          onTap: () {
            if (genderChangeNotifier.gender == genderParam) {
              genderChangeNotifier.gender = Gender.NA;
              print("Not answered!");
            } else {
              genderChangeNotifier.gender = genderParam;
              print(genderParam.toString());
            }
          },
          child: Text(
            genderText,
            style: TextStyle(
              fontSize: _fontSize,
            ),
          ),
        ),
      ],
    );
  }


Function call: Function 致电:

_buildRadioWithText(genderChangeNotifier, Gender.FEMALE, "Female"),
_buildRadioWithText(genderChangeNotifier, Gender.MALE, "Male"),


genderChangeNotifieris just a provider to set and get the Gender value and notify listeners when a Gender is set. genderChangeNotifier 只是一个提供者,用于设置和获取 Gender 值并在设置 Gender 时通知侦听器。

final GenderNotifier genderChangeNotifier= Provider.of<GenderNotifier>(context);

GestureDetector 's onTap works well when I tap on the Text widget.当我点击Text小部件时, GestureDetectoronTap效果很好。 It selects then deselects the option just as I'd like to but in case of the Radio widget onTap is never called.它会像我想要的那样选择然后取消选择该选项,但如果 R Radio小部件永远不会调用onTap


Any idea how to achieve the deselection when I tap/click on the Radio widget itself?当我点击/单击Radio小部件本身时,知道如何取消选择吗? And why the GestureDetector that wraps the Radio does not register the tap events?为什么包装RadioGestureDetector不注册点击事件?

The GestureDetector's onTap is not being called because the GestureDetector inside the Radio widget is taking priority.没有调用 GestureDetector 的onTap ,因为 Radio 小部件内的 GestureDetector 具有优先权。 By default, when two GestureDetectors are competing for input, only one will "win".默认情况下,当两个 GestureDetector 竞争输入时,只有一个会“获胜”。 In this case, the winner is the one in the Radio.在这种情况下,获胜者是电台中的获胜者。 This article discusses "GestureArenas" and how you can allow both GestureDetectors to process input. 本文讨论“GestureArenas”以及如何允许两个 GestureDetector 处理输入。

However, consider whether allowing the user to deselect the radio button is the correct solution.但是,请考虑允许用户取消选择单选按钮是否是正确的解决方案。 Radio buttons are designed not to be deselected once the user has selected an option.一旦用户选择了一个选项,单选按钮就不会被取消选择。 You might instead:您可以改为:

  • Offer a third, "I'd rather not say" option提供第三个“我不想说”选项
  • Use toggleable buttons instead of a radio group使用可切换按钮而不是单选组
  • Use a dropdown menu instead of a radio group使用下拉菜单而不是单选组

See this answer for more info on the usability aspect of this.有关此可用性方面的更多信息,请参阅此答案

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM