0

我编写了一个警报框,它有两个选项取消和继续,对于继续按钮,我给出了一个名为 _resetcounter 的函数,然后我给出了导航器函数,这样在重置发生后,警报框应该消失,而这并没有发生。我是新手,请帮帮我。提前致谢!

'''

void showAlertDialog(BuildContext context) {
// set up the buttons
Widget cancelButton = TextButton(
    child: Text("Cancel"),
    
    onPressed: () {
      Navigator.of(context).pop();
    });
Widget continueButton = TextButton(
    child: Text("Continue"),
    
    onPressed: () {
      resetCounter();
      Navigator.of(context).pop();
    });

// set up the AlertDialog
AlertDialog alert = AlertDialog(
  title: Text("AlertDialog"),
  content: Text("Would you like to reset the count?"),
  actions: [
    cancelButton,
    continueButton,
  ],
);

// show the dialog
showDialog(
  context: context,
  builder: (BuildContext context) {
    return alert;
  },
);
}

重置计数器类的代码如下:

 class _MyHomePageState extends State<MyHomePage> {
 int _counter = 0;
    void _incrementCounter() {
setState(() {
  _counter++;
});
 }
   void _decrementCounter() {
if (_counter > 0) {
  setState(() {
    _counter--;
  });
 }
 }
  void resetCounter() {
showAlertDialog(context);
setState(() {
  _counter = 0;
});
 }
4

0 回答 0