【问题标题】:how to make my radio button to be only one of them can be selected如何使我的单选按钮只能选择其中一个
【发布时间】:2019-11-17 22:01:39
【问题描述】:

我希望一次只选择一个单选按钮(从该组中)。但是我的所有单选按钮都可以选择。我应该怎么做才能解决这个问题?

这是我的主要代码

    body: Column(
      children: <Widget>[
        Expanded(
          child: ListView.builder(
              itemCount: quizData.length,
              padding: EdgeInsets.all(8.0),
              itemBuilder: (context, index) {
                Results results = quizData[index];
                List<String> allAnswer = results.incorrectAnswer;
                allAnswer.add(results.correctAnswer);

                return ListTile(
                    title: Text("${index + 1}. " + results.question,
                        style: TextStyle(
                          fontSize: 15.0,
                        )),
                    subtitle: Container(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: allAnswer.map((choice) {
                          return AnswerChoice(
                              allAnswer,
                              allAnswer.indexOf(choice),
                              choice,
                              allAnswer.indexOf(choice));
                        }).toList(),
                      ),
                    ));
              }),
        ),
      ],
    ));

这是创建单选按钮的代码

class AnswerChoice extends StatefulWidget {
  final List<String> results;
  final int index;
  final String choice;
  final int abjadIndex;
  AnswerChoice(this.results, this.index, this.choice, this.abjadIndex);

  @override
  _AnswerChoiceState createState() =>  _AnswerChoiceState(this.choice, this.abjadIndex);
}

class _AnswerChoiceState extends State<AnswerChoice> {
  int selectedRadio = -1;
  final int abjadIndex;
  List abjadList = ['a', 'b', 'c', 'd'];
  Color colorPressed() {
    return Colors.green;
  }

  final String choice;
  _AnswerChoiceState(this.choice, this.abjadIndex);
  @override
  Widget build(BuildContext context) {
    return RadioListTile(
      value: widget.index,
      groupValue: selectedRadio,
      onChanged: (val) {
        setState(() {
          selectedRadio = val;
          print("jawaban: " + choice);
          print(widget.index);
          print(selectedRadio);
        });
      },
      title: Text(
        "${abjadList[abjadIndex]}. " + choice,
        style: TextStyle(fontSize: 14.0),
      ),
    );
  }
}

我在Trouble with flutter radio button这个问题上尝试过类似的问题,但没有任何改变

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您的问题是变量selectedRadio 的位置。

    您为每台收音机创建了一个selectedRadio。因此,在您的onChanged 中,您只需从点击的收音机中更改selectedRadio

    AnswerChoicegroupValue 之外使用一个变量,当您更改它时,它将被复制到该组的所有无线电。

    import 'package:flutter/material.dart';
    import 'dart:ui';
    
    class AnswerChoice extends StatefulWidget {
      //final List<String> results; Unused
      final int index;
      final String choice;
      final int groupValue;
      final ValueChanged<int> onChanged;
    
    
      //final int abjadIndex; equal Index
      AnswerChoice(this.index, this.choice, this.groupValue, this.onChanged);
    
      @override
      _AnswerChoiceState createState() =>  _AnswerChoiceState(this.index, this.choice, this.groupValue);
    }
    
    class _AnswerChoiceState extends State<AnswerChoice> {
      int selectedRadio = -1;
      final int index;
      final int groupValue;
      List abjadList = ['a', 'b', 'c', 'd'];
      Color colorPressed() {
        return Colors.green;
      }
    
      final String choice;
      _AnswerChoiceState(this.index, this.choice, this.groupValue);
      @override
      Widget build(BuildContext context) {
        return RadioListTile(
          value: widget.index,
          groupValue: groupValue,
          onChanged: widget.onChanged,
          title: Text(
            "${abjadList[index]}. " + choice,
            style: TextStyle(fontSize: 14.0),
          ),
        );
      }
    }
    

    在你的 main 中创建一个类似的函数:

         void changeOption(int val) {
            setState(() {
              _groupValue = val;
            });
          } 
    

    在您的主目录中,您必须有一个变量 _groupValue,您的地图将类似于:

        allAnswer.map((choice) {
          return AnswerChoice(
          allAnswer.indexOf(choice),
          choice,
          _groupValue,
          changeOption);
        }).toList()
    

    【讨论】:

    • 很抱歉,您能详细解释一下我应该在代码中更改哪些内容吗?我真的不明白,我是颤振的新手。顺便说一句,感谢您回答我的问题
    • @MuhammadDhai'ul,我想就是这样。
    • 代码不起作用,每次单击按钮时应用程序都变成this,这里是my full code,我错过了什么吗?
    • 错误出现在来自_AnswerChoiceStatebuild 方法上,groupValue: selectedRadio, 应该是groupValue: groupValue,。很抱歉,我在回答中更正了。
    • 当您从 JSON 迭代数据时,您似乎遇到了问题。最后一个项目被多次生成。但我在提供的代码中看不到原因。
    猜你喜欢
    • 1970-01-01
    • 2017-06-28
    • 1970-01-01
    • 2015-08-30
    • 2018-06-27
    • 2016-06-08
    • 2018-06-17
    • 2011-08-13
    • 1970-01-01
    相关资源
    最近更新 更多