获取动态小部件列表的值 - flutter
Get values of dynamic list of widgets - flutter
我已经设法制作了一个将 flutter 与 Firestore 联系起来的动态问卷。
我的自定义 Question 小部件只是一个保存问题字符串的 Text() 和一个 Slider() 以便用户可以给出 1 到 5 的答案
既然问题都显示出来了,我应该怎么取值呢?
这是我的代码:
var questions = <Widget>[];
//Async read to the DB
Future query() async {
var snap = await Firestore.instance
.collection("Forms")
.document(formID)
.get();
var arr = <Widget>[]; //just a temp holder of the widgets
for(String q in list){
arr.add(Question(min: 1.0, max: 5.0, question: q, value: 1.0,));
}
setState(() {
questions = arr;
});
}
然后在我正在渲染的构建中:
Scaffold(
body:Container(
child:Column(
children: <Widget>[
Text("Title"),
Column(
children: questions,
),
RaisedButton(
onPressed: () => sendFeedback(),
color: Colors.redAccent,
textColor: Colors.white,
child: Text("Send")
]
)
sendFeedback() 函数的代码是什么?我想获取我的 children 列表中所有滑块的值,然后只需调用一次数据库就将它们写入 Firestore。
您需要保持所有滑块的状态。我建议您使用 BLoC 模式进行状态管理。
编辑:您可以有一个 List<int>
来保存您的 bloc 中的值,并且在每个滑块中您将实现函数 onChangeEnd: bloc.addValue
并将滑块值发送到 bloc,该 bloc 将把它们添加到名单。
然后在按钮上,您将有类似 onPressed: () => bloc.sendFeedback()
的内容,它将从列表中获取值并将它们写入 Firestore。
请记住,这是我现在想到的解决方案,以便您理解这个概念
为了这个应用程序的目的,有一个全局变量就足够了
Map<String, double> responses = Map();
当我导航到问卷时,它会被重置,然后每个滑块都必须像这样更新地图 (onChange):
Slider(
value: value,
min: widget.min,
max: widget.max,
divisions: (widget.max - 1).round(),
label: value.ceil().toString(),
activeColor: color,
onChanged: (double val1) {
//Force a re-render
setState(() {
//Update the value of the Slider
value = val1;
//Do some operations, assign a color based on the result
double p = value / widget.max;
if (p > 0 && p <= 0.3) {
color = Colors.red;
} else if (p > 0.3 && p <= 0.6) {
color = Colors.orangeAccent;
} else {
color = Colors.green;
}
//Update the value of the parent Widget
widget.value = value;
ratings[widget.question] = value;
//Done, print in console for debugging
print(value);
});
})
我在 BLoC 模式上投入了相当多的时间,但我无法让它按预期工作。也许我会很快再试一次 post 支持解决方案。
我已经设法制作了一个将 flutter 与 Firestore 联系起来的动态问卷。
我的自定义 Question 小部件只是一个保存问题字符串的 Text() 和一个 Slider() 以便用户可以给出 1 到 5 的答案
既然问题都显示出来了,我应该怎么取值呢?
这是我的代码:
var questions = <Widget>[];
//Async read to the DB
Future query() async {
var snap = await Firestore.instance
.collection("Forms")
.document(formID)
.get();
var arr = <Widget>[]; //just a temp holder of the widgets
for(String q in list){
arr.add(Question(min: 1.0, max: 5.0, question: q, value: 1.0,));
}
setState(() {
questions = arr;
});
}
然后在我正在渲染的构建中:
Scaffold(
body:Container(
child:Column(
children: <Widget>[
Text("Title"),
Column(
children: questions,
),
RaisedButton(
onPressed: () => sendFeedback(),
color: Colors.redAccent,
textColor: Colors.white,
child: Text("Send")
]
)
sendFeedback() 函数的代码是什么?我想获取我的 children 列表中所有滑块的值,然后只需调用一次数据库就将它们写入 Firestore。
您需要保持所有滑块的状态。我建议您使用 BLoC 模式进行状态管理。
编辑:您可以有一个 List<int>
来保存您的 bloc 中的值,并且在每个滑块中您将实现函数 onChangeEnd: bloc.addValue
并将滑块值发送到 bloc,该 bloc 将把它们添加到名单。
然后在按钮上,您将有类似 onPressed: () => bloc.sendFeedback()
的内容,它将从列表中获取值并将它们写入 Firestore。
请记住,这是我现在想到的解决方案,以便您理解这个概念
为了这个应用程序的目的,有一个全局变量就足够了
Map<String, double> responses = Map();
当我导航到问卷时,它会被重置,然后每个滑块都必须像这样更新地图 (onChange):
Slider(
value: value,
min: widget.min,
max: widget.max,
divisions: (widget.max - 1).round(),
label: value.ceil().toString(),
activeColor: color,
onChanged: (double val1) {
//Force a re-render
setState(() {
//Update the value of the Slider
value = val1;
//Do some operations, assign a color based on the result
double p = value / widget.max;
if (p > 0 && p <= 0.3) {
color = Colors.red;
} else if (p > 0.3 && p <= 0.6) {
color = Colors.orangeAccent;
} else {
color = Colors.green;
}
//Update the value of the parent Widget
widget.value = value;
ratings[widget.question] = value;
//Done, print in console for debugging
print(value);
});
})
我在 BLoC 模式上投入了相当多的时间,但我无法让它按预期工作。也许我会很快再试一次 post 支持解决方案。