在下面的代码中,每次我尝试移动滑块时,都会弹出一条消息,询问我是否要移动滑块。如果答案是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();
},
),
],
);
},
);
},
),
),
);
}
}
1条答案
按热度按时间a8jjtwal1#
创建布尔变量以检查权限。