Flutter 启用/禁用CupertinoDialogAction取决于CUPERTINOTEXT字段是否为空

Flutter 启用/禁用CupertinoDialogAction取决于CUPERTINOTEXT字段是否为空,flutter,flutter-cupertino,Flutter,Flutter Cupertino,如果CupertinoTextField不为空,我想将CUPERTINODEALOGACTION设置为启用,否则默认情况下它应该被禁用,我还设置了“isDefaultAction:false”,但它仍然可以单击 showDialog( context: context, builder: (BuildContext context) => CupertinoAlertDialog( actions: [

如果CupertinoTextField不为空,我想将CUPERTINODEALOGACTION设置为启用,否则默认情况下它应该被禁用,我还设置了“isDefaultAction:false”,但它仍然可以单击

showDialog(
          context: context,
          builder: (BuildContext context) => CupertinoAlertDialog(
            actions: [
              CupertinoDialogAction(
                onPressed: () => (Navigator.of(context).pop()),
                child: Text("Cancel"),
              ),
              CupertinoDialogAction(
                child: Text("Save"),
                isDefaultAction: false,
              ),
            ],
            title: Text("New Folder"),
            content: Column(
              children: <Widget>[
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Text("Enter a name for this folder"),
                ),
                Container(
                  height: 30,
                  child: CupertinoTextField(
                    controller: folderName,
                    placeholder: "Name",
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(8),
                    ),
                  ),
                )
              ],
            ),
          ),
        );
showDialog(
上下文:上下文,
生成器:(BuildContext上下文)=>CupertinoAlertDialog(
行动:[
铜中毒(
按下时:()=>(Navigator.of(context.pop()),
子项:文本(“取消”),
),
铜中毒(
子项:文本(“保存”),
isDefaultAction:false,
),
],
标题:文本(“新文件夹”),
内容:专栏(
儿童:[
填充物(
填充:常数边集全部(8.0),
子项:文本(“输入此文件夹的名称”),
),
容器(
身高:30,
孩子:CupertinoTextField(
控制器:folderName,
占位符:“名称”,
装饰:盒子装饰(
边界半径:边界半径。圆形(8),
),
),
)
],
),
),
);

如果要禁用
CupertinoDialogAction
,需要将
onPressed
属性设置为
null
。它将如下所示:

       Bool isEnabled = false;

       @override
       void initState() {
         super.initState();

         folderName.addListener(enableButton); // addListened to your TextEditingController!
       }
它将使isEnabled变为true

      enableButton()
      {
        if(folderName.text != "")
        {
          setState(() {
            isEnabled = true;
          });
        }                
      }
然后,您可以使用这个布尔字段

      CupertinoDialogAction(
              onPressed: !isEnabled 
                  ? null
                  : () {
                      // Do what you need!
                      // Save method!
                    },
              child: Text("Save"),
              isDefaultAction: false,
            ),

如果要禁用
CupertinoDialogAction
,需要将
onPressed
属性设置为
null
。它将如下所示:

       Bool isEnabled = false;

       @override
       void initState() {
         super.initState();

         folderName.addListener(enableButton); // addListened to your TextEditingController!
       }
它将使isEnabled变为true

      enableButton()
      {
        if(folderName.text != "")
        {
          setState(() {
            isEnabled = true;
          });
        }                
      }
然后,您可以使用这个布尔字段

      CupertinoDialogAction(
              onPressed: !isEnabled 
                  ? null
                  : () {
                      // Do what you need!
                      // Save method!
                    },
              child: Text("Save"),
              isDefaultAction: false,
            ),
  • 创建一个有状态的小部件,用于构建操作列表并返回带有这些操作的
    CupertinoAlertDialog
    。此小部件应包含一些状态,指示是否应启用保存操作。如果不应启用,请将null放入
    onPressed
    处理程序中

  • 编写一些处理程序,使用
    setState
    根据用户的操作设置此启用/禁用状态

  • showDialog
    builder返回有状态小部件

  • 创建一个有状态的小部件,用于构建操作列表并返回带有这些操作的
    CupertinoAlertDialog
    。此小部件应包含一些状态,指示是否应启用保存操作。如果不应启用,请将null放入
    onPressed
    处理程序中

  • 编写一些处理程序,使用
    setState
    根据用户的操作设置此启用/禁用状态

  • showDialog
    builder返回有状态小部件


  • 它不起作用,“Save”(保存)Cupertinodialogation(CUPERTINOTEXT)操作仍然处于禁用状态,即使我在CupertinoteXT字段中输入了一些文本,这不是我所期望的行为。您可以尝试将侦听器添加到TextEditingController。我编辑了答案。您可以再次检查。它仍然不起作用,Save DialogAction仍处于禁用状态,可能是TextController没有获取TextField中的更改,并且调试控制台显示异常“方法'call'在null上被调用”,“Receiver:null”,“Twired call:call()”。错误可能在调用call()方法的另一个位置。您也可以共享代码的这一部分吗?它不起作用,即使在我在CupertinoTextField中输入了一些文本(这不是我所期望的行为)后,“保存”Cupertinodialogation仍然处于禁用状态。您可以尝试将侦听器添加到TextEditingController。我编辑了答案。您可以再次检查。它仍然不起作用,Save DialogAction仍处于禁用状态,可能是TextController没有获取TextField中的更改,并且调试控制台显示异常“方法'call'在null上被调用”,“Receiver:null”,“Twired call:call()”。错误可能在调用call()方法的另一个位置。你也能分享你的那部分代码吗?