Reputation: 13
I'm trying to create a list of a/b choices from an encoded json file. So far I've managed to get my radiolisttiles populated correctly but when i select one of the tiles it selects them all simultaneously. My question is what am I doing wrong that my radiolisttiles wont operate independently?
edited: what I'm trying to do is get the value of the radio button selection into a list variable(_pick) with : var _pick =data[i].home; print(_pick);
here's my code:
// radiolisttile radio buttons *** /// return new Column( children: [
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,
),
Upvotes: 0
Views: 2807
Reputation: 1105
I've made some changes in your code, in this you need a list of items in which you can store selected value in model, please check this code, in this ModelVO
is added, in which a string is added as selectedValue
which will be used to set the proper selected radio.
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,
};
}
Upvotes: 2