gpt4 book ai didi

listview - flutter 中的 radio list 问题

转载 作者:IT王子 更新时间:2023-10-29 07:22:33 27 4
gpt4 key购买 nike

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

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

这是我的代码:

//radiolisttile 单选按钮 ***/// 返回新列( 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,

),

最佳答案

我对您的代码进行了一些更改,在此您需要一个项目列表,您可以在其中存储模型中的选定值,请检查此代码,在此添加 ModelVO,在其中一个字符串被添加为 selectedValue,它将用于设置正确的选定 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,
};
}

关于listview - flutter 中的 radio list 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54897592/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com