无线电清单中的小问题扑朔迷离

时间:2019-02-27 03:32:13

标签: listview flutter

我正在尝试从已编码的json文件创建a / b选择的列表。到目前为止,我已经设法正确地填充了我的放射性微粒,但是当我选择其中一个磁贴时,它会同时选择它们。我的问题是我的放射性微粒不会独立运行,这是我做错了什么?

编辑: 我想做的是使用var _pick = data [i] .home;将单选按钮选择的值放入列表变量(_pick)中。打印(_pick);

这是我的代码:

//单选按钮*** ///                         返回新列(                           孩子:[

                        new Text("GAME: ${data[i].game} ${data[i].away.toUpperCase()} at ${data[i].home.toUpperCase()} "),

                        RadioListTile<SingleGame>(
                          subtitle: const Text('record 3- 1'),
                          title: new Text("${data[i].home.toUpperCase()}"),
                          controlAffinity: ListTileControlAffinity.trailing,
                          value: SingleGame.$home,
                          groupValue: data[i].selectedValue,
                          activeColor: Colors.green,
                          onChanged: 
                          (SingleGame value) {
                            setState(() {
                              data[i].selectedValue = SingleGame.$home;
                              var _pick =data[i].home; print(_pick);
                              //var _selectedIndex=_pick;
                              //picksList.add(_pick); 
                            });
                          },

                          selected: data[i].selectedValue == SingleGame.$home,
                        ),
                        ////
                        RadioListTile<SingleGame>(
                          subtitle: const Text('record 3- 1'),
                          title: new Text("${data[i].away.toUpperCase()}"),
                          controlAffinity: ListTileControlAffinity.trailing,
                          value: SingleGame.$away,
                          groupValue: data[i].selectedValue, 
                          activeColor: Colors.green,
                          onChanged: (SingleGame value) {
                            setState(() {
                              data[i].selectedValue = SingleGame.$away;
                              var _pick =data[i].away; print(_pick);
                              // _picksList.add(_pick);
                            });
                          },
                          selected: data[i].selectedValue == SingleGame.$away,

                        ),

1 个答案:

答案 0 :(得分:1)

我对您的代码进行了一些更改,为此您需要一个项目列表,可以在其中存储模型中的选定值,请检查此代码,并在此ModelVO中添加其中的字符串被添加为selectedValue,将用于设置正确选择的收音机。

import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;

class Schedule extends StatefulWidget {
  static const String routeName = "/schedule";

  ScheduleState createState() => new ScheduleState();
}

List<ModelVO> data = new List();

enum SingleGame { $home, $away }

class ScheduleState extends State<Schedule> {
  SingleGame _gamePick = SingleGame.$home;

  Future<String> getJson() async {
    var response = await http.get(
        Uri.encodeFull("https://sheetdb.io/api/v1/red9m0vfm55hy"),
        headers: {"Accept": "application/json"});

    this.setState(() {
      List strList = json.decode(response.body) as List;
      data = strList.map((model) => ModelVO.fromJson(model)).toList();
    });

    print("schedule imported successfully!");
  } //

  @override
  void initState() {
    this.getJson();
  }

  @override
  Widget build(BuildContext context) {
    var scaffold = new Scaffold(
      appBar: new AppBar(
        title: new Text('Schedule'),
        backgroundColor: Colors.green,
      ),
      body: new ListView.separated(
        separatorBuilder: (context, index) => Divider(
              color: Colors.green,
            ),
        itemCount: data == null ? 0 : data.length,
        itemBuilder: (BuildContext context, i) {
          // radiolisttile  ***
          return new Column(
            children: <Widget>[
              RadioListTile<SingleGame>(
                title: new Text("${data[i].home.toUpperCase()}"),
                value: SingleGame.$home,
                groupValue: data[i].selectedValue,
                onChanged: (SingleGame value) {
                  setState(() {
                    data[i].selectedValue = SingleGame.$home;
                  });
                },
                selected: data[i].selectedValue == SingleGame.$home,
              ),
              RadioListTile<SingleGame>(
                title: new Text("${data[i].away.toUpperCase()}"),
                value: SingleGame.$away,
                groupValue: data[i].selectedValue,
                onChanged: (SingleGame value) {
                  setState(() {
                    data[i].selectedValue = SingleGame.$away;
                  });
                },
                selected: data[i].selectedValue == SingleGame.$away,
              ),
            ],
          );
        },
      ),
    );
    return scaffold;
  }
}

class ModelVO {
  String game;
  String home;
  String away;
  String winner;
  String score;
  SingleGame selectedValue;

  ModelVO(
      {this.game,
      this.home,
      this.away,
      this.winner,
      this.score,
      this.selectedValue});

  factory ModelVO.fromJson(Map<String, dynamic> parsedJson) {
    return ModelVO(
        game: parsedJson['game'],
        home: parsedJson['home'],
        away: parsedJson['away'],
        winner: parsedJson['winner'],
        score: parsedJson['score']);
  }

  Map toJson() => {
        "game": game,
        "home": home,
        "away": away,
        "winner": winner,
        "score": score,
        "selectedValue": selectedValue,
      };
}