当我将它存储在 List<Widget> 中时,RadioListTile 指示器没有显示,但它有值并在 flutter 中更改值(我通过打印知道)
RadioListTile indicator is not showing when i store it in List<Widget> but it have values and change values ( i know it via print) in flutter
所以我正在制作一个测验应用程序,它将随机播放它的无线电选项。我将 radioListTile 存储在列表(小部件)中并使其随机播放。但是我的 radioListTile 不能改变它的指标,但它有值和改变的值(我通过打印知道)
如果我不将它与 List(Widget) 一起存储,它就可以正常工作。
当我将它存储在 List(Widget)
中时不正常
这是我的代码:
import 'package:flutter/material.dart';
enum ListJawaban{
answer1,
answer2,
answer3,
answer4,
answer5
}
class QuizTemplate extends StatefulWidget {
const QuizTemplate({
Key? key,
required this.question,
required this.option1,
required this.option2,
required this.option3,
required this.option4,
required this.option5,
required this.qNumber
}) : super(key: key);
final String question;
final String option1;
final String option2;
final String option3;
final String option4;
final String option5;
final int qNumber;
@override
State<QuizTemplate> createState() => _QuizTemplateState();
}
class _QuizTemplateState extends State<QuizTemplate> {
ListJawaban? _value = null;
Widget radioListTile({required String text, required ListJawaban jawaban}){
return RadioListTile<ListJawaban>(
title: Text("$text"),
value: jawaban,
groupValue: _value,
onChanged: (ListJawaban? newValue){
setState(() {
print("jawaban : $newValue");
print("group value : $_value");
_value = newValue;
});
},
);
}
List<Widget> listRadioOption(){
return [
//this should be shuffeled 1 time after build (in init state)
radioListTile(text: widget.option1, jawaban: ListJawaban.answer1),
radioListTile(text: widget.option2, jawaban: ListJawaban.answer2),
radioListTile(text: widget.option3, jawaban: ListJawaban.answer3),
radioListTile(text: widget.option4, jawaban: ListJawaban.answer4),
radioListTile(text: widget.option5, jawaban: ListJawaban.answer5),
];
}
late List<Widget> list2; //list for checking my second tot..
List<Widget> option(){
return list2;
}
@override
void initState() {
super.initState();
print("---- INIT STATE ----");
// listRadioOption().shuffle(); //this doesn't shuffle the option, but radio option works perfectly
list2 = listRadioOption(); //this work if i use this as children,
list2.shuffle(); //but it won't changing radio option value when user click in it (i dont know why)
}
@override
Widget build(BuildContext context) {
print("---- BUILD ----");
return Container(
child: Column(
children: [
Text("${widget.qNumber}. ${widget.question}",style: TextStyle(
fontSize: 20,
),),
Builder(
//i actually didn't need this builder, but i used it just for checking variables
builder: (context){
print("---- BUILDER ----");
print("VALUE : $_value");
return Column(
children: option(),
);
},
)
],
),
);
}
}
如果您只需要在第一次加载时洗牌一次,请使用级联符号 (..) 运算符 return 洗牌列表。
Note: this list should be loaded as an initial and avoid rebuild this on setState
.
List<Widget> listRadioOption(){
return [
radioListTile(text: widget.option1, jawaban: ListJawaban.answer1),
radioListTile(text: widget.option2, jawaban: ListJawaban.answer2),
radioListTile(text: widget.option3, jawaban: ListJawaban.answer3),
radioListTile(text: widget.option4, jawaban: ListJawaban.answer4),
radioListTile(text: widget.option5, jawaban: ListJawaban.answer5),
]..shuffle(); // <- This return shuffled list.
}
所以我正在制作一个测验应用程序,它将随机播放它的无线电选项。我将 radioListTile 存储在列表(小部件)中并使其随机播放。但是我的 radioListTile 不能改变它的指标,但它有值和改变的值(我通过打印知道)
如果我不将它与 List(Widget) 一起存储,它就可以正常工作。 当我将它存储在 List(Widget)
中时不正常这是我的代码:
import 'package:flutter/material.dart';
enum ListJawaban{
answer1,
answer2,
answer3,
answer4,
answer5
}
class QuizTemplate extends StatefulWidget {
const QuizTemplate({
Key? key,
required this.question,
required this.option1,
required this.option2,
required this.option3,
required this.option4,
required this.option5,
required this.qNumber
}) : super(key: key);
final String question;
final String option1;
final String option2;
final String option3;
final String option4;
final String option5;
final int qNumber;
@override
State<QuizTemplate> createState() => _QuizTemplateState();
}
class _QuizTemplateState extends State<QuizTemplate> {
ListJawaban? _value = null;
Widget radioListTile({required String text, required ListJawaban jawaban}){
return RadioListTile<ListJawaban>(
title: Text("$text"),
value: jawaban,
groupValue: _value,
onChanged: (ListJawaban? newValue){
setState(() {
print("jawaban : $newValue");
print("group value : $_value");
_value = newValue;
});
},
);
}
List<Widget> listRadioOption(){
return [
//this should be shuffeled 1 time after build (in init state)
radioListTile(text: widget.option1, jawaban: ListJawaban.answer1),
radioListTile(text: widget.option2, jawaban: ListJawaban.answer2),
radioListTile(text: widget.option3, jawaban: ListJawaban.answer3),
radioListTile(text: widget.option4, jawaban: ListJawaban.answer4),
radioListTile(text: widget.option5, jawaban: ListJawaban.answer5),
];
}
late List<Widget> list2; //list for checking my second tot..
List<Widget> option(){
return list2;
}
@override
void initState() {
super.initState();
print("---- INIT STATE ----");
// listRadioOption().shuffle(); //this doesn't shuffle the option, but radio option works perfectly
list2 = listRadioOption(); //this work if i use this as children,
list2.shuffle(); //but it won't changing radio option value when user click in it (i dont know why)
}
@override
Widget build(BuildContext context) {
print("---- BUILD ----");
return Container(
child: Column(
children: [
Text("${widget.qNumber}. ${widget.question}",style: TextStyle(
fontSize: 20,
),),
Builder(
//i actually didn't need this builder, but i used it just for checking variables
builder: (context){
print("---- BUILDER ----");
print("VALUE : $_value");
return Column(
children: option(),
);
},
)
],
),
);
}
}
如果您只需要在第一次加载时洗牌一次,请使用级联符号 (..) 运算符 return 洗牌列表。
Note: this list should be loaded as an initial and avoid rebuild this on
setState
.
List<Widget> listRadioOption(){
return [
radioListTile(text: widget.option1, jawaban: ListJawaban.answer1),
radioListTile(text: widget.option2, jawaban: ListJawaban.answer2),
radioListTile(text: widget.option3, jawaban: ListJawaban.answer3),
radioListTile(text: widget.option4, jawaban: ListJawaban.answer4),
radioListTile(text: widget.option5, jawaban: ListJawaban.answer5),
]..shuffle(); // <- This return shuffled list.
}