[英]I have a variable and I want to display a dialog box when press the button flutter dart
I have a variable and I want to display dialog box when press the button flutter dart, but the content of the dialog box it will be changed according of the value of the variable, or create more then one dialog box and when I press the button one of them displayed according of the variable.我有一个变量,我想在按下按钮 flutter dart 时显示对话框,但是对话框的内容会根据变量的值而改变,或者创建多个对话框,当我按下按钮时其中之一根据变量显示。
I want to ask for code if anyone can help me;如果有人可以帮助我,我想询问代码; the dart code I mean.我的意思是 dart 代码。
The variable is called som
and calculates the median of 10 values.该变量称为som
并计算 10 个值的中位数。
There are multiple ways you can achieve this.有多种方法可以实现这一目标。
You can create multiple dialog boxes and do the condition checking on button tap by using switch or if.您可以创建多个对话框,并使用 switch 或 if 对按钮点击进行条件检查。 In the below code I have compared the values on the button tap and then return a function based on the value.在下面的代码中,我比较了按钮点击的值,然后根据该值返回 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"), ); }); } }
You can check the value in the dialog box method and then return various alertboxes from there based on value.您可以检查对话框方法中的值,然后根据值从那里返回各种警报框。
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"), ); }); } } }
Here I have compared the value inside a function.在这里,我比较了 function 内部的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.