dart 在移动滑块之前询问用户是否要移动滑块

pnwntuvh  于 2022-12-27  发布在  其他
关注(0)|答案(1)|浏览(154)

在下面的代码中,每次我尝试移动滑块时,都会弹出一条消息,询问我是否要移动滑块。如果答案是Yes,我可以移动滑块。如果答案是No,我不能移动滑块。我的问题是:现在,当我单击滑块并单击“是”时,滑块移动到我在询问之前单击的位置。我不希望滑块自己移动。我只需要能够在单击“是”后移动滑块。
提前感谢你的帮助
遵守准则

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();
                      },
                    ),
                  ],
                );
              },
            );
          },
        ),
      ),
    );
  }
}
a8jjtwal

a8jjtwal1#

创建布尔变量以检查权限。

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();
                        },
                      ),
                    ],
                  );
                },
              );
            }
          },
        ),
      ),
    );
  }
}

相关问题