简体   繁体   English

如何在 flutter 中使用列表视图生成器制作动态单选按钮?

[英]how to make dynamic radio button with listview builder in flutter?

I want to make it like attendance by giving attendance status to students.. the concept that I created was a listview builder with name data and then row by looping the number of radio buttons.我想通过向学生提供出勤状态来使其像出勤一样。我创建的概念是一个带有名称数据的列表视图构建器,然后通过循环单选按钮的数量来进行行。 However, I am quite confused about this problem但是,我对这个问题感到很困惑

Section Radio部分收音机

 Row( children: [ for(var i = 0; i < 2; i++) Radio<int>( value: i, groupValue: radioValue, activeColor: Colors.blueAccent, onChanged: handleRadioValueChanged ), ], ),

https://stackoverflow.com/questions/67385094/dynamically-create-radio-button-group-in-listview-builder-flutter

when I select a part of the list over the other parts follow and I am using listview builder for the above code当我 select 是列表的一部分而不是其他部分时,我正在为上述代码使用 listview builder

In the code below, I have dynamically shown how to create a list of radio buttons.在下面的代码中,我动态地展示了如何创建单选按钮列表。

With the following code, you can create a unique style for radio button.使用以下代码,您可以为单选按钮创建独特的样式。

class RootPage extends StatefulWidget {
  const RootPage({Key key}) : super(key: key);

  @override
  State<RootPage> createState() => _RootPageState();
}

class _RootPageState extends State<RootPage> {
  List<RadioItem> items = <RadioItem>[];
  int groupValue = 0; 

  @override
  void initState() {
     for (int i = 0; i < 10; i++) {
       items.add(RadioItem(index: i, name: 'radio ' + i.toString()));
     }
     super.initState();
  }
  
  @override Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
      children: items
          .map(
            (data) => Card(
              elevation: 3,
              shadowColor: const Color(0xFFAAAAAA),
              margin: const EdgeInsets.only(left: 30, right: 30, top: 15),
              color: Colors.white,
              shape: RoundedRectangleBorder(
                side: const BorderSide(color: Colors.transparent, width: 0),
                borderRadius: BorderRadius.circular(20),
              ),
              child: InkWell(
                borderRadius: BorderRadius.circular(20),
                onTap: () {
                  setState(() {
                    groupValue = data.index;
                  });
                },
                child: Container(
                  padding: const EdgeInsets.symmetric(horizontal: 10.0),
                  child: Directionality(
                    textDirection: TextDirection.rtl,
                    child: Row(
                      children: <Widget>[
                        Radio(
                          groupValue: groupValue,
                          value: data.index,
                          onChanged: (index) {
                            setState(() {
                              groupValue = index;
                            });
                          },
                        ),
                        Expanded(child: Text(data.name)),
                      ],
                    ),
                  ),
                ),
              ),
            ),
          )
          .toList(),
       ),
    );
  }
}

Then add the following class然后添加以下class

class RadioItem {
  String name;
  int index;
  RadioItem({this.name, this.index});
}

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

相关问题 关于 ListView.builder() flutter 中的单选按钮 - About Radio Button in ListView.builder() flutter 在 ListView.builder() flutter 中动态创建单选按钮组 - Dynamically create radio button Group in ListView.builder() flutter 如何选择特定的单选按钮并在 ListView.builder Flutter 中获取其值 - How to select specific radio button and get its value in ListView.builder Flutter 在 Listview Builder 中动态制作 RadiobButton 但 select 在 flutter 中一次一个按钮 - Make RadiobButton Dynamically in Listview Builder in but select one button at a time in flutter flutter:如何制作卡片单选按钮? - flutter : How to make Card Radio Button? Flutter - 如何使用动态 ListView 使 Container 增长? - Flutter - How to make Container grow with a dynamic ListView? 如何在 flutter 中对 listview.builder 进行列表视图? - How to make list view to listview.builder in flutter? Flutter:如何使用 Listview.builder 使每张卡片都独一无二? - Flutter : How to make every card unique using Listview.builder? Flutter,如何使Listview.builder锚定到底部 - Flutter, how to make a Listview.builder anchored to the bottom 如何在 Dart Flutter 中使 ListView.builder 或 List 索引静态化 - How to make an ListView.builder or List index static in Dart Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM