flutter - 在ListView.builder() flutter中动态创建单选按钮Group

标签 flutter dart flutter-layout radio-button radio-group

我想创建这样一个用户界面

最后,我得到了所有选定单选按钮的详细信息。

这是我的完整代码

当我尝试这样做时,所有单选按钮都转向一侧。

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class Affiche_grille extends StatefulWidget {
  @override
  _QuestionWidgetState createState() => _QuestionWidgetState();
}

int selectedRadio;
int _groupValue = -1;

class _QuestionWidgetState extends State<Affiche_grille> {
  List<RadioModel> sampleData = new List<RadioModel>();

  @override
  void initState() {
    super.initState();
    sampleData.add(new RadioModel(false, 'A', 'April 18', 'text1'));
    sampleData.add(new RadioModel(false, 'B', 'April 17', 'text2'));
    sampleData.add(new RadioModel(false, 'C', 'April 16', 'text3'));
    sampleData.add(new RadioModel(false, 'D', 'April 15', 'text4'));
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("ListItem"),
      ),
      body: ListView.builder(
        itemCount: sampleData.length,
        itemBuilder: (context, index) => ButtonBar(
          alignment: MainAxisAlignment.center,
          children: <Widget>[
            _myRadioButton(
              title: "Checkbox 0",
              value: 0,
              onChanged: (newValue) => setState(() => _groupValue = newValue),
            ),
            _myRadioButton(
              title: "Checkbox 1",
              value: 1,
              onChanged: (newValue) => setState(() => _groupValue = newValue),
            ),
          ],
        ),
      ),
    );
  }
}

Widget _myRadioButton({String title, int value, Function onChanged}) {
  return Radio(
    value: value,
    groupValue: _groupValue,
    onChanged: onChanged,
  );
}

class RadioModel {
  bool isSelected;
  final String buttonText;
  final String text;
  final String text1;

  RadioModel(this.isSelected, this.buttonText, this.text, this.text1);
}

此应用程序 UI 是根据此代码制作的。

enter image description here

最佳答案

当 radio 值 == groupValue 时启用 在您的情况下,所有 8 个 radio 都共享一个 groupValue,并且对于所有行,第一个的值为 0,第二个的值为 1。

然后,当您单击左侧Radio时,全局值设置为0,然后所有左侧Radio(它们的值都为0)启用,反之亦然。

要创建您想要执行的操作,您应该为每个Radio设置唯一值,并为每个ROW设置唯一的groupValue

这是一个有效的示例。

class Affiche_grille extends StatefulWidget {
  @override
  _QuestionWidgetState createState() => _QuestionWidgetState();
}

class _QuestionWidgetState extends State<Affiche_grille> {
  List<RadioModel> sampleData = [];
  List<int> groupValue = [];
  List<List<int>> value = [];

  @override
  void initState() {
    super.initState();
    sampleData.add(new RadioModel(false, 'A', 'April 18', 'text1'));
    sampleData.add(new RadioModel(false, 'B', 'April 17', 'text2'));
    sampleData.add(new RadioModel(false, 'C', 'April 16', 'text3'));
    sampleData.add(new RadioModel(false, 'D', 'April 15', 'text4'));

    groupValue.add(0);
    groupValue.add(2);
    groupValue.add(4);
    groupValue.add(6);

    value.add([0,1]);
    value.add([2,3]);
    value.add([4,5]);
    value.add([6,7]);
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("ListItem"),
      ),
      body: ListView.builder(
        itemCount: sampleData.length,
        itemBuilder: (context, index) => ButtonBar(
          alignment: MainAxisAlignment.center,
          children: <Widget>[
            Radio(
              groupValue: groupValue[index],
              value: value[index][0], 
              onChanged: (newValue) => setState(() => groupValue[index] = newValue),  
            ),
            Radio(
              groupValue: groupValue[index],
              value: value[index][1],
              onChanged: (newValue) => setState(() => groupValue[index] = newValue),  
            ),
          ],
        ),
      ),
    );
  }
}

关于flutter - 在ListView.builder() flutter中动态创建单选按钮Group,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67385094/

相关文章:

flutter - 参数类型 'ModalRoute<Object?>?' 不能分配给参数类型 'PageRoute<dynamic>'

dart - GestureDetector 内的 ScrollView : Dispatch Touch Events

dart - 如何在Dart中打开并关注文件(如tail -f)?

flutter - 为什么要扩大而不是扩大高度

android - flutter工具无法访问文件

flutter - 如何在 flutter 中水平扩展抽屉导航内的折叠项目

dart - 如何在 Flutter 上为 Material 小部件设置宽度?

flutter - 转换成整数? int flutter

flutter - 如何在 flutter 中部分显示图像以获得幕后效果?

flutter - 如何在 Flutter 中创建圆形图标按钮?