简体   繁体   English

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

[英]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 个值的中位数。

  • if the sum == 0 -> then the button should display the first dialog box如果 sum == 0 -> 那么按钮应该显示第一个对话框
  • else -> the button display the second dialog box else -> 按钮显示第二个对话框

There are multiple ways you can achieve this.有多种方法可以实现这一目标。

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

相关问题 我想在对话框 flutter DART 上显示结果 - i WANT TO display the RESULT on DIALOG box flutter DART 需要您的支持:当我按下收藏按钮时,我想在我的 Flutter 数据库中获取产品 ID - Need your HELD : I want to get the product ID in my Flutter database when I press the favorite button 在调试时按下 Dart DevTools 中的调试按钮之前,无法按下 Flutter 应用程序中的按钮 - Cannot press buttons in my Flutter app until I press the Debug button in Dart DevTools while debugging 我想在按下按钮时创建小部件 - I want to create widget when I press button 按下 ElevatedButton 时,我想打开路由应用程序。 Flutter - when on press on ElevatedButton I want open Routing applications. Flutter 在 Flutter 中,我想通过按下按钮获取当前日期并显示在文本字段中,我该怎么做? - In Flutter, I want to get current date with button press and show in a Textfield, How can I do this? Dart, Flutter 我该如何克服这个错误? 我想从 map 获取信息以在此处显示 - Dart , Flutter How can I overcome this error? I want to get info from map to display it here 当我想在 Flutter 中调试我的应用程序时遇到问题 - I have problem when I want debug my app in Flutter 为什么我不能在 flutter dart 中显示图像? - Why i cant display the image in flutter dart? 当我在颤抖中按下RaisedButton时,我想从TextFormField中获取值 - I want to get the value from a TextFormField when i press a RaisedButton in flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM