簡體   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