Flutter 取消选择 RadioListTile 按钮

Gor*_*ora 5 mobile dart flutter

几天来我一直试图找出我的错误在哪里,但仍然无法解决。Flutter 中的绝对新手,我构建了多项选择测验应用程序。我有多项选择问题屏幕,它从如下所示的 radio_button.dart 文件导入 radiolisttile 选择。

一切正常,除了选择按钮后我无法取消选择该按钮。换句话说,如果我单击每个按钮,所有按钮都会被选中。

 import "package:flutter/material.dart";

 class RB extends StatefulWidget {
 RB({this.choice, this.value});

 final String choice;
 final String value;

@override
_RBState createState() => _RBState();

}
class _RBState extends State<RB> {
bool dense=false;
 String _selection = '';
@override
Widget build(BuildContext context) {
return   Padding(
  padding: const EdgeInsets.all(8.0),
  child: Material(
    elevation: 5.0,
    child: RadioListTile(
      title: Text(widget.choice,  style: TextStyle(color: Colors.blue,),),
      value: widget.choice,
      groupValue: _selection,
      dense: dense,
      activeColor: Colors.green,
      onChanged:( val) { setState(() { _selection = val; print(val);}); },

    ),
  ),
 );
 }
}

Here the question_screen snippet
 Column(
 children: <Widget>[
 Container(child: RB(choice:'A',value:'All of above',)),
 Container(child: RB(choice:'B',value:'None of the above'))
Run Code Online (Sandbox Code Playgroud)

小智 6

现在,2020 年 9 月,正确答案应该是使用toggleable属性。

RadioListTile(
            title: Text("xxx"),
            value: index,
            groupValue: _seletedIndex,
            toggleable: true,
            onChanged: (value) {
              onAlertChange(value, alert);
            },
          )
Run Code Online (Sandbox Code Playgroud)


Kah*_*hou 1

包裹你RadioListTileGestureDetector

          GestureDetector(
            onTap: () {
              setState(() {
                _selection = '';
              });
            },
            child: RadioListTile(
            //...
Run Code Online (Sandbox Code Playgroud)