Home > Enterprise >  Before moving a Slider asking the user if he wants to move the slider
Before moving a Slider asking the user if he wants to move the slider

Time:12-27

In the following code, every time i try to move a slider a pop up message appears asking if i want to move the slider. If the answer is Yes, i am able to move the slider. If the answer is No i cannot move the slider. My problem: Right now when i click on the Slider and click Yes the slider moved to the position that i clicked before asking. I do not want that the slider moves by himself. I would need only to be able to move the slider after the Yes click.

Thanks in advance for some help

Follows the code

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  MyHomePageState createState() => MyHomePageState();
}

class MyHomePageState extends State<MyHomePage> {
  double _sliderValue = 0.0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Slider(
          value: _sliderValue,
          onChanged: (double newValue) {
            showDialog(
              context: context,
              builder: (BuildContext context) {
                return AlertDialog(
                  title: const Text("Confirm"),
                  content: const Text("Are you sure you want to move the slider?"),
                  actions: [
                    ElevatedButton(
                      child: const Text("Yes"),
                      onPressed: () {
                        // Set the new value of the slider
                        setState(() {
                          _sliderValue = newValue;
                        });
                        // Dismiss the dialog
                        Navigator.of(context).pop();
                      },
                    ),
                    ElevatedButton(
                      child: const Text("No"),
                      onPressed: () {
                        // Dismiss the dialog
                        Navigator.of(context).pop();
                      },
                    ),
                  ],
                );
              },
            );
          },
        ),
      ),
    );
  }
}

CodePudding user response:

Create a Boolean variable to check the permission.

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  MyHomePageState createState() => MyHomePageState();
}

class MyHomePageState extends State<MyHomePage> {
  double _sliderValue = 0.0; // Boolean variable
  bool silderEnable = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Slider(
          value: _sliderValue,
          onChanged: (double newValue) {
            if (silderEnable) {
              // check if user pressed yes that is if the slider is enalbed or not
              setState(() {
                _sliderValue =
                    newValue; // if it enabled slider value will change
              });
            } else {
              // if its disabled it will ask for permission
              showDialog(
                context: context,
                builder: (BuildContext context) {
                  return AlertDialog(
                    title: const Text("Confirm"),
                    content:
                        const Text("Are you sure you want to move the slider?"),
                    actions: [
                      ElevatedButton(
                        child: const Text("Yes"),
                        onPressed: () {
                          silderEnable = true;
                          Navigator.of(context).pop();
                        },
                      ),
                      ElevatedButton(
                        child: const Text("No"),
                        onPressed: () {
                          Navigator.of(context).pop();
                        },
                      ),
                    ],
                  );
                },
              );
            }
          },
        ),
      ),
    );
  }
}
  • Related