Flutter Slider onChangeEnd属性不存在';我不能正常工作

Flutter Slider onChangeEnd属性不存在';我不能正常工作,flutter,dart,google-cloud-firestore,flutter-slider,Flutter,Dart,Google Cloud Firestore,Flutter Slider,我想保存以存储更改后的最后一个滑块值。我尝试使用onChangeEnd来实现这一点,但我得到的不是最后一个值,而是第一个选择的值 示例 我想要的是: 当前滑块位置为3。我想按滑块上的3并将其滑动到5,因为我的答案是5。我想被保存在Firestore,我的答案是5。之后,不可能移动滑块并给出新答案 import 'package:flutter/material.dart'; import 'package:gamiforms/services/database.dart'; class Lin

我想保存以存储更改后的最后一个滑块值。我尝试使用onChangeEnd来实现这一点,但我得到的不是最后一个值,而是第一个选择的值

示例

我想要的是: 当前滑块位置为3。我想按滑块上的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;
    }
  }
}
我得到的#1: 当前滑块位置为3。我在滑块上按3,我想把它滑到5,因为我想我的答案是5。在Firestore中,我的答案是3。之后,不可能移动滑块并给出新答案

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;
    }
  }
}
我得到的#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;
    }
  }
}
导入“包装:颤振/材料.省道”;
导入“包:gamiforms/services/database.dart”;
类LinearScale扩展StatefulWidget{
字符串问题,formId;
线性标度(this.question,this.formId);
@凌驾
LinearScaleState createState()=>LinearScaleState(问题);
}
类LinearScaleState扩展了状态{
字符串问题;
线性标度状态(本问题);
双总=3.0;
字符串整体状态=“良好”;
静态final formKey=new GlobalKey();
DatabaseService DatabaseService=DatabaseService();
字符串答案=”;
bool isLoading=false;
bool enabled=true;
上传FormData(总体){
//if(formKey.currentState.validate()){
设置状态(){
isLoading=true;
});
打印(“总金额$总金额”);
如果(总体==1.0)回答=“失败”;
如果(总体==2.0)回答=“可接受”;
如果(总体==3.0)回答=“良好”;
如果(总体==4.0)回答=“非常好”;
如果(总体==5.0)回答为“非常好”;
打印(‘应答$应答’);
映射应答映射={
“问题”:这个问题,
答:答,,
};
打印(${widget.formId});
databaseService.addAnswerData(answerMap,widget.formId)。然后((值){
答案=”;
问题=这个问题;
设置状态(){
isLoading=false;
});
}).catchError((e){
印刷品(e);
});
}
@凌驾
小部件构建(构建上下文){
var screenSize=MediaQuery.of(context).size;
var width=屏幕大小.width;
var height=屏幕大小.height;
打印(“测试$question”);
返回大小框(
宽度:宽度,
高度:高度-100,
子:容器(
边距:仅限边缘集(顶部:30.0),
子:列(
crossAxisAlignment:crossAxisAlignment.stretch,
儿童:[
容器(
边距:仅限边缘设置(左:16.0),
子:文本(
问题:,
样式:TextStyle(字体大小:20),
)),
容器(
边缘:边缘组。对称(垂直:50.0),
子:文本(
总体状况,
样式:TextStyle(
颜色:Colors.teal[800],
fontWeight:fontWeight.bold,
字体大小:30.0),
textAlign:textAlign.center,
),
),
扩大(
儿童:中心(
子:滑块(
价值:总体而言,
一旦更改:(值){
设置状态(){
总体=value.round().toDouble();
_获取总体状态(总体);
});
},
onChangeEnd:已启用
?(价值){
启用=错误;
设置状态(){
总体=value.round().toDouble();
上传FormData(整体);
});
}
:null,
标签:“${total.toInt()}”,
分部:4,
最低:1.0,
最高:5.0,
),
),
)
],
),
),
);
}
_getOverallStatus(总体加倍){
开关(total.toInt()){
案例1:
总体状态=‘失败’;
打破
案例2:
总体状态=‘可接受’;
打破
案例3:
总体状态=‘良好’;
打破
案例4:
总体状态=‘非常好’;
打破
违约:
总体状态=‘优秀’;
打破
}
}
}

我猜,在场景1中,您获得第一个拾取值的原因是,当您点击3时,您的
onChangeEnd
首先被调用,值为3,然后它会重建小部件,将滑块值更新为5。因此,您点击的值会被保存。此外,您的代码的哪一部分会从中更新滑块值3到5?@AbdurRafaySaleem如何解决这个问题?我将编辑并添加完整的代码到问题中,也许比它更清楚。是的,这将使问题更容易解决。@llama_glama@AbdurRafaySaleem已编辑。请你现在看一下代码好吗?