flutter - 滑块 onChangeEnd 属性无法正常工作
问题描述
我想在更改后将最后一个滑块值保存到 Firestore。我尝试使用 onChangeEnd 来做到这一点,但我得到的不是最后一个值,而是第一个选择的值。
例子
我想得到什么: 当前滑块位置是 3。我想在滑块上按 3 并将其滑动到 5,因为我的答案是 5。我想保存在 Firestore 中,我的答案是 5。在那之后,不可能移动滑块并给出新的答案。
我得到的#1: 当前滑块位置是 3。我在滑块上按 3,我想将其滑动到 5,因为我希望我的答案是 5。在 Firestore 中保存我的答案是 3。在那之后,它不是可以移动滑块并给出新的答案。
我得到的#2: 当前滑块位置是 3。我在滑块上按 5(不滑动),我希望我的答案是 5。在 Firestore 中保存我的答案是 5。之后,无法移动滑块并给出新的答案。
import 'package:flutter/material.dart';
import 'package:gamiforms/services/database.dart';
class LinearScale extends StatefulWidget {
String question, formId;
LinearScale(this.question, this.formId);
@override
LinearScaleState createState() => LinearScaleState(question);
}
class LinearScaleState extends State<LinearScale> {
String question;
LinearScaleState(this.question);
double overall = 3.0;
String overallStatus = "Good";
static final formKey = new GlobalKey<FormState>();
DatabaseService databaseService = DatabaseService();
String answer = "";
bool isLoading = false;
bool enabled = true;
uploadFormData(overall) {
//if (formKey.currentState.validate()) {
setState(() {
isLoading = true;
});
print('overall $overall');
if (overall == 1.0) answer = "Fail";
if (overall == 2.0) answer = "Acceptable";
if (overall == 3.0) answer = "Good";
if (overall == 4.0) answer = "Very good";
if (overall == 5.0) answer = "Excellent";
print('answer $answer');
Map<String, String> answerMap = {
"question": this.question,
"answer": answer,
};
print("${widget.formId}");
databaseService.addAnswerData(answerMap, widget.formId).then((value) {
answer = "";
question = this.question;
setState(() {
isLoading = false;
});
}).catchError((e) {
print(e);
});
}
@override
Widget build(BuildContext context) {
var screenSize = MediaQuery.of(context).size;
var width = screenSize.width;
var height = screenSize.height;
print('TEST $question');
return SizedBox(
width: width,
height: height - 100,
child: Container(
margin: EdgeInsets.only(top: 30.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(
margin: EdgeInsets.only(left: 16.0),
child: Text(
question,
style: TextStyle(fontSize: 20),
)),
Container(
margin: EdgeInsets.symmetric(vertical: 50.0),
child: Text(
overallStatus,
style: TextStyle(
color: Colors.teal[800],
fontWeight: FontWeight.bold,
fontSize: 30.0),
textAlign: TextAlign.center,
),
),
Expanded(
child: Center(
child: Slider(
value: overall,
onChanged: (value) {
setState(() {
overall = value.round().toDouble();
_getOverallStatus(overall);
});
},
onChangeEnd: enabled
? (value) {
enabled = false;
setState(() {
overall = value.round().toDouble();
uploadFormData(overall);
});
}
: null,
label: '${overall.toInt()}',
divisions: 4,
min: 1.0,
max: 5.0,
),
),
)
],
),
),
);
}
_getOverallStatus(double overall) {
switch (overall.toInt()) {
case 1:
overallStatus = 'Fail';
break;
case 2:
overallStatus = 'Acceptable';
break;
case 3:
overallStatus = 'Good';
break;
case 4:
overallStatus = 'Very good';
break;
default:
overallStatus = 'Excellent';
break;
}
}
}
解决方案
推荐阅读
- python - 从嵌入列中的列表中提取元素
- python - 我的 python 气泵代码有什么问题?
- ios - 如何在 Swift 中向自定义 DateComponent 添加分钟
- python - 将列表中的唯一元素附加到新列表
- wordpress - AMP 上的 Google 表单是否需要 HTTPS?
- extreact - Sencha extJs/exreact 6/7 Tree 获取选中记录
- python - 排序算法不会产生正确的输出
- c++ - 我试图让它从文件中打印一些东西,它正在打印这些奇怪的东西
- python - Python - 如何替换列表列表中每个列表的第一个元素
- ruby - Ruby:使用适当的组合从两个二维数组编译散列