简体   繁体   English

如何在 ListView.builder 中使用 RadioListTile?

[英]How to use RadioListTile inside of a ListView.builder?

I'm trying to make a selectable listview.我正在尝试制作一个可选择的列表视图。

I chose RadioListTile ,but it doesn't need to be with Radios ,I would be happy with the background color changing of a ListTile on tapping the item.我选择了RadioListTile ,但它不需要与Radios一起使用,我会很高兴在点击项目时ListTile的背景颜色发生变化。 So in my current code I have RadioListTiles ,but it doesn't checks on tap,and I think it would let to check more than one,because its in a ListView.builder ,but I don't know since I'm new to flutter,and it's not working.因此,在我当前的代码中,我有RadioListTiles ,但它不会在点击时检查,而且我认为它可以检查多个,因为它在ListView.builder中,但我不知道,因为我是新手flutter,它不工作。

Here is the code:这是代码:

ListView.builder(
  scrollDirection: Axis.vertical,
  itemCount: items.length,
  itemBuilder: (context, index) {
    return RadioListTile<LoadList>(
      selected: isSelected,
      groupValue: radiolist,
      value: items[index],
      onChanged: (LoadList value) {
        radiolist.GetHours(value.hours);
        print("CurrentSelected $index");
        setState(() {
          isSelected = true;
        });
      },
      title: new Text(index),
    );
  },
),

Radios are actually very simple in flutter. flutter 中的收音机实际上非常简单。 Basically, they're a list of items each with a value, and there's a shared selected value.基本上,它们是一个项目列表,每个项目都有一个值,并且有一个共享的选定值。 The state maintains the shared selected value. state 维护共享选定值。

Basically, all you need to do is keep track of that selected value and change it when the list items are pressed.基本上,您需要做的就是跟踪所选值并在按下列表项时更改它。 And for each of the items, you check if the shared selected value equals the item's value.对于每个项目,您检查共享的选定值是否等于项目的值。

The RadioListItem just helps that along a little bit by doing the equality part for you. RadioListItem 只是通过为您做相等部分来帮助一点点。 This should do what you want.这应该做你想要的。

import 'package:flutter/material.dart';

main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  MyAppState createState() {
    return new MyAppState();
  }
}

class MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: RadioListBuilder(
          num: 20,
        ),
      ),
    );
  }
}

class RadioListBuilder extends StatefulWidget {
  final int num;

  const RadioListBuilder({Key key, this.num}) : super(key: key);

  @override
  RadioListBuilderState createState() {
    return RadioListBuilderState();
  }
}

class RadioListBuilderState extends State<RadioListBuilder> {
  int value;

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemBuilder: (context, index) {
        return RadioListTile(
          value: index,
          groupValue: value,
          onChanged: (ind) => setState(() => value = ind),
          title: Text("Number $index"),
        );
      },
      itemCount: widget.num,
    );
  }
}

Check below code sample to use the radio buttons dynamiccaly.检查下面的代码示例以动态使用单选按钮。

Column(
  crossAxisAlignment: CrossAxisAlignment.start,
  children: _options
      .map((e) => new Column(
            children: [
              Container(
                margin: EdgeInsets.symmetric(vertical: 5),
                decoration: BoxDecoration(
                  border: Border.all(
                    width: 2,
                  ),
                  borderRadius: BorderRadius.circular(12.0),
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: e.subOptions
                      .map((x) => RadioListTile(
                            value: x.optionBit, //set this to parent object of the 
                                                //group which determines the selected option.
                            groupValue: e.optionBit,
                            onChanged: (value) {
                              setState(() {
                                e.optionBit = value;
                              });
                            },
                            title: new Text(
                              x.text,
                            ),
                          ))
                      .toList(),
                ),
              )
            ],
          ))
      .toList(),
),

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

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