Slider onChangeEnd property doesn't work properly

279 views Asked by At

I want to save to Firestore the last slider value after changes. I tried to do that using onChangeEnd, but instead of last value, I get first picked value.

Examples

What I want to get: Current slider position is 3. I want to press 3 on slider and slide it to 5 because my answer is 5. I want to be saved in Firestore that my answer is 5. After that, it's not possible to move slider and give new answer.

What I am getting #1: Current slider position is 3. I press 3 on slider and I want to slide it to 5 because I want my answer to be 5. In Firestore is saved that my answer is 3. After that, it's not possible to move slider and give new answer.

What I am getting #2: Current slider position is 3. I press 5 on slider (without sliding) and I want my answer to be 5. In Firestore is saved that my answer is 5. After that, it's not possible to move slider and give new answer.

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;
    }
  }
}
0

There are 0 answers