我有一个变量,我想在按下按钮时显示一个对话框 flutter dart

I have a variable and I want to display a dialog box when press the button flutter dart

我有一个变量,我想在按下 flutter dart 按钮时显示对话框,但是对话框的内容会根据变量的值而改变,或者创建多个对话框和当我按下按钮时,其中一个根据变量显示。

我想求代码,有没有人能帮帮我;我的意思是飞镖代码。

变量被称为 som 并计算 10 个值的中位数。

您可以通过多种方式实现这一目标。

  1. 您可以创建多个对话框,并使用 switch 或 if 对按钮点击进行条件检查。在下面的代码中,我比较了点击按钮时的值,然后 return 基于该值的函数。

       class CustomAlerts extends StatelessWidget {
         final int som = 1;
         @override
         Widget build(BuildContext context) {
           return Scaffold(
             appBar: AppBar(
               title: Text("Custom Dialog"),
             ),
             body: Container(
               child: Column(
                 children: [
                   ElevatedButton(
                     onPressed: () {
                       if (som == 0) {
                         showFirstDialog(context, som);
                       } else {
                         showSecondDialog(context, som);
                       }
                     },
                     child: Text("Button 1"),
                   ),
                 ],
               ),
             ),
           );
         }
    
         showFirstDialog(BuildContext context, int value) {
           return showDialog(
               context: context,
               builder: (context) {
                 return AlertDialog(
                   title: Text("value is $value"),
                 );
               });
         }
    
         showSecondDialog(BuildContext context, int value) {
           return showDialog(
               context: context,
               builder: (context) {
                 return AlertDialog(
                   title: Text("value is not equal to 0"),
                   content: Text("value is $value"),
                 );
               });
         }
       }
    
  2. 您可以在对话框方法中检查值,然后return根据值从那里得到各种警告框。

     class CustomAlerts extends StatelessWidget {
     final int som = 1;
     @override
     Widget build(BuildContext context) {
        return Scaffold(
           appBar: AppBar(
         title: Text("Custom Dialog"),
       ),
       body: Container(
         child: Column(
           children: [
             ElevatedButton(
               onPressed: () {
                 showCustomDialog(context, som);
               },
               child: Text("Button 1"),
             ),
           ],
         ),
       ),
     );
     }
    
    showCustomDialog(BuildContext context, int value) {
     if (value == 0) {
       return showDialog(
           context: context,
           builder: (context) {
             return AlertDialog(
               title: Text("value is $value"),
             );
           });
     } else {
       return showDialog(
           context: context,
           builder: (context) {
             return AlertDialog(
               title: Text("value is not equal to 0"),
               content: Text("value is $value"),
             );
           });
         }
       }
      }
    

这里我比较了一个函数里面的值