如何在Flutter / Dart中创建动态Radio-Group(Android)

时间:2018-07-25 09:41:45

标签: java android dart flutter android-radiogroup

下面的代码我曾经在简单的android中创建了Flutter / Dart的功能,而我对flutter / Android完全陌生

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
    let cell: UITableViewCell = super.tableView(tableView, cellForRowAt: indexPath)

    if (isUserLogIn) {
        print("MY Login Yes:\(isUserLogIn)")
        if cell.isEqual(tableViewCell_ImageLogin) || cell.isEqual(tableViewCell_LoginContent) ||  cell.isEqual(tableViewCell_Facebook) ||  cell.isEqual(tableViewCell_OR) ||  cell.isEqual(tableViewCell_Swipe) || cell.isEqual(tableViewCell_LoginEmail) || cell.isEqual(tableViewCell_LoginPassword) || cell.isEqual(tableViewCell_ButtonLogin) || cell.isEqual(tableViewCell_ForgotPassword) || cell.isEqual(tableViewCell_LoginEmptyCell) || cell.isEqual(tableViewCell_Name) || cell.isEqual(tableViewCell_Email) || cell.isEqual(tableViewCell_Mobile) || cell.isEqual(tableViewCell_Password) || cell.isEqual(tableViewCell_Confirm) || cell.isEqual(tableViewCell_ButtonRegister) || cell.isEqual(tableViewCell_RegisterEmptyCell) || cell.isEqual(tableViewCell_LastEmpty)    {
            return 0
        }
        else
        {
            return super.tableView(tableView, heightForRowAt: indexPath)
        }
    }

3 个答案:

答案 0 :(得分:0)

请参见flutter提供了两种创建RadioButton列表的方法,可以使用https://docs.flutter.io/flutter/material/RadioListTile-class.htmlhttps://docs.flutter.io/flutter/material/Radio-class.html。 如果您需要进一步的帮助,请告诉我

答案 1 :(得分:0)

下面是一个示例代码块,使用它来动态创建无线电组列表。

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(),
),

答案 2 :(得分:0)

options_item.dart

class OptionsItem {
  String productOptionCategGuid;
  String categoryName;
  String minSelectionRequired;
  String maxSelectionLimit;
  String selectedOption; //To store selected options
  List<Items> items;

  OptionsItem({
    this.productOptionCategGuid,
    this.categoryName,
    this.minSelectionRequired,
    this.maxSelectionLimit,
    this.selectedOption,
    this.items,
  });

  OptionsItem.fromJson(Map<String, dynamic> json) {
    this.productOptionCategGuid = json["ProductOptionCategGUID"].toString();
    this.categoryName = json["CategoryName"].toString();
    this.minSelectionRequired = json["MinSelectionRequired"].toString();
    this.maxSelectionLimit = json["MaxSelectionLimit"].toString();
    this.selectedOption = json["selectedOption"].toString();
    this.items = json["Items"] == null
        ? []
        : (json["Items"] as List).map((e) => Items.fromJson(e)).toList();
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data["ProductOptionCategGUID"] = this.productOptionCategGuid;
    data["CategoryName"] = this.categoryName;
    data["MinSelectionRequired"] = this.minSelectionRequired;
    data["MaxSelectionLimit"] = this.maxSelectionLimit;
    data["selectedOption"] = this.selectedOption;
    if (this.items != null)
      data["Items"] = this.items.map((e) => e.toJson()).toList();
    return data;
  }
}

items.dart

class Items {
  String optionItemGuid;
  String optionItemName;
  String price;

  Items({this.optionItemGuid, this.optionItemName, this.price});

  Items.fromJson(Map<String, dynamic> json) {
    this.optionItemGuid = json["OptionItemGuid"].toString();
    this.optionItemName = json["OptionItemName"].toString();
    this.price = json["Price"].toString();
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data["OptionItemGuid"] = this.optionItemGuid;
    data["OptionItemName"] = this.optionItemName;
    data["Price"] = this.price;
    return data;
  }
}

widget_item_flavour.dart

class WidgetItemFlavour extends StatefulWidget {
  final OptionsItem item;
  WidgetItemFlavour({this.item});

  @override
  _WidgetItemFlavourState createState() => _WidgetItemFlavourState();
}

class _WidgetItemFlavourState extends State<WidgetItemFlavour> {
  String _selectedFlavour = '';

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.start,
      // WAY 1
      /* children: flavourList
          .map((data) => RadioListTile(
                dense: true,
                activeColor: greenColor,
                contentPadding: EdgeInsets.zero,
                title: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      "${data.flavourName}",
                      style: TextStyle(color: black2, fontSize: 15),
                    ),
                    Text(
                      "${data.flavourPrice}",
                      style: TextStyle(color: black2, fontSize: 15),
                    ),
                  ],
                ),
                groupValue: _selectedFlavour,
                value: data.flavourId,
                onChanged: (val) {
                  setState(() {
                    print('Val: ' + val.toString());
                    _selectedFlavour = val;
                  });
                },
              ))
          .toList(), */
      //WAY 2 
      children: widget.item.items
          .map((data) => InkWell(
                onTap: () {
                  setState(() {
                    //var index = widget.item.items.indexOf(data);
                    //print('Ketan Index: ' + index.toString());
                    widget.item.selectedOption = data.optionItemGuid;
                    _selectedFlavour = data.optionItemGuid;
                  });
                },
                child: Row(
                  children: [
                    Radio(
                      activeColor: greenColor,
                      fillColor: MaterialStateProperty.all<Color>(greenColor),
                      visualDensity:
                          VisualDensity(horizontal: -4, vertical: -3),
                      materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                      value:
                          _selectedFlavour.trim() == data.optionItemGuid.trim()
                              ? true
                              : false,
                      groupValue: true,
                      onChanged: (value) {},
                    ),
                    SizedBox(width: 10),
                    Expanded(
                      child: Text(
                        "${data.optionItemName}",
                        style: TextStyle(color: black2, fontSize: 14),
                      ),
                    ),
                    Text(
                      "$currency_sign${data.price}",
                      style: TextStyle(color: black2, fontSize: 14),
                    ),
                  ],
                ),
              ))
          .toList(),
    );
  }
}