【问题标题】:how to use condition in this button to show alertdialog in flutter?如何使用此按钮中的条件在颤动中显示警报对话框?
【发布时间】:2021-08-15 18:21:38
【问题描述】:
  1. 产品详情
child: ButtonTheme(
                                    child: (TextButton(
                                      child: Text(
                                        'Demande de prix',
                                        style: TextStyle(
                                            color: Colors.white,
                                            fontSize: 12,
                                            fontWeight: FontWeight.w600),
                                      ),
                                      style: TextButton.styleFrom(
                                        primary: Colors.white,
                                        backgroundColor: Color(0xFF2664B5),
                                        onSurface: Colors.white,
                                      ),
                                      onPressed: () {
                                        Navigator.push(
                                          context,
                                          MaterialPageRoute(
                                            builder: (context) => DemandeDevis(
                                              productName:
                                                  (selectedProduitslist[index]
                                                          .titre)
                                                      .toUpperCase(),
                                            ),
                                          ),
                                        );
                                      },
                                    )),
                                  ),

2.requiredevis

AlertDialog(
  backgroundColor: Colors.white,
  elevation: 20,
  content: SingleChildScrollView(
    child: Form(
      key: _formKey,
      child: ListBody(
        children: <Widget>[
          Container(
            child: Text(
              "Demande de prix",
              textAlign: TextAlign.center,
              style: TextStyle(
                  color: Colors.blue[900],
                  fontSize: 20,
                  fontWeight: FontWeight.bold),
            ),
          ),
          Container(
            width: ResponsiveFlutter.of(context).wp(50),
            padding: EdgeInsets.all(3),
            child: TextFormField(
              controller: largeurController,
              style: TextStyle(color: Colors.black),
              keyboardType: TextInputType.phone,
              // validator: (text) {
              //   if (text == null || text.isEmpty) {
              //     return "Champ obligatoire";
              //   }
              //   return null;
              // },
              decoration: InputDecoration(
                fillColor: Colors.white,
                filled: true,
                hintText: 'Largeur (m)',
                hintStyle: TextStyle(
                  color: Colors.blue[900],
                  fontSize: 10,
                ),
                focusedBorder: OutlineInputBorder(
                  borderSide:
                      BorderSide(color: Colors.blue[900], width: 0.5),
                  borderRadius: BorderRadius.circular(3.0),
                ),
                contentPadding:
                    const EdgeInsets.symmetric(horizontal: 20, vertical: 0),
                border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(3.0),
                ),
              ),
            ),
          ),
          Container(
            width: ResponsiveFlutter.of(context).wp(50),
            padding: EdgeInsets.all(3),
            child: TextFormField(
              controller: longeurController,
              style: TextStyle(color: Colors.black),
              keyboardType: TextInputType.phone,
              // validator: (text) {
              //   if (text == null || text.isEmpty) {
              //     return "Champ obligatoire";
              //   }
              //   return null;
              // },
              decoration: InputDecoration(
                fillColor: Colors.white,
                filled: true,
                hintText: 'Longeur (m)',
                hintStyle: TextStyle(
                  color: Colors.blue[900],
                  fontSize: 10,
                ),

【问题讨论】:

  • 哪个按钮?你能指定吗?

标签: flutter dart


【解决方案1】:

我假设您在询问如何在按下按钮时显示对话框。

每个按钮都有一个onPressed : 参数,在该onPressed 函数中,您可以执行showDialog() 函数以在UI 中显示一个对话框。下面给出的是代码 sn-p。

TextButton(
 child: Text(
             'Yes!',
              style: TextStyle(color: Theme.of(context).accentColor),),
              onPressed: () => 
               {

                //This is the function that will execute when the button is pressed

                showDialog(
                     context : context,
                     builder : (context) => AlertDialog()
                          );                
               },
            ),
         );
                 

【讨论】:

  • thank you for your answer, I have other issues I need to use condition because I have some category, so when x category is selected it is necessary that the Alert Dialog showing a specific input...
  • 如果你解释清楚也许我能帮上忙。
【解决方案2】:

我相信您想根据条件执行一些 onPressed 功能,例如 if(condition) 执行此操作,否则执行此操作...

  1. 你可以通过内联条件来做到这一点,也称为三元运算符。 例如:

    onPressed: (condition != null) ? () => Navigator.push() : () => showAlertDialog()
    

    你会这样读:

    (your condition) ? [if true ->] do that : [else ->] do that.
    

    你也可以嵌套这个表达式。

  2. 作为替代方案,您可以添加一个调用其他条件函数的函数。

    ...
    onPressed: _decisionFunction,
    ...
    
    void _decisionFunction(){
       if(condition == true){
          Navigator.push(...);
       } else {
          showDialog(...);
       }
    }
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-12
    • 2020-11-22
    • 2020-10-08
    • 2019-11-15
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    相关资源
    最近更新 更多