繁体   English   中英

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

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

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

如果有人可以帮助我,我想询问代码; 我的意思是 dart 代码。

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

  • 如果 sum == 0 -> 那么按钮应该显示第一个对话框
  • else -> 按钮显示第二个对话框

有多种方法可以实现这一目标。

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

     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. 您可以检查对话框方法中的值,然后根据值从那里返回各种警报框。

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

在这里,我比较了 function 内部的值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM