【问题标题】:How to dispose FocusNode and TextEditingController inside a Dialog in Flutter如何在 Flutter 的 Dialog 中配置 FocusNode 和 TextEditingController
【发布时间】:2019-09-14 20:31:00
【问题描述】:

我有一个 Dialog,它从两个文本字段中获取输入,并根据按钮的点击执行一些任务。 为了处理来自这些文本字段的焦点和输入,我创建了 FocusNodeTextEditingControllers

通常在 StatefulWidget 的 State 中,我们有 dispose 方法,我们可以在其中处理 ChangeNotifier,但我认为这在对话框的情况下是不可能直接实现的。

那么,我应该避免使用那些 changeNotifier,还是应该在 showDialog() 中匿名构建小部件,而应该创建一个单独的 StateFulWidget 类?

【问题讨论】:

    标签: flutter


    【解决方案1】:
    1. 创建一个单独的类并使其成为StatefulWidget;
    2. 在 State 类中使用 dispose() 来处理您的控制器。

    注意:避免在匿名函数中包含复杂的代码。

    【讨论】:

      【解决方案2】:

      在我的情况下,我使用简单的 textInput 和一个按钮创建对话框,当用户单击按钮时,以编程方式处理 edittext

      class MyDialogs {
         bool showed = false;
         void dismiss(BuildContext context) {
           if (showed) {
             Navigator.pop(context);
           }
        }
      
       void showCaptcha(
          BuildContext context,
          ) {
         String? errorText;
      showDialog(
          barrierDismissible: false,
          context: context,
          builder: (context) {
            showed = true;
            const Color accentColor = Colors.teal;
      
            TextEditingController editingController = TextEditingController();
            return StatefulBuilder(builder: (context, setState) {
              return Dialog(
                  backgroundColor: Colors.transparent,
                  child: Column(
                        mainAxisSize: MainAxisSize.max,
                        children: [
      
                          Container(
                              width: 360,
                              child: TextField(
                                controller: editingController,
                                decoration: const InputDecoration(
                                  border: InputBorder.none,
                                  hintText: 'Enter seen text',
                                  hintStyle: TextStyle(color: Colors.grey),
                                ))),
      
                          TextButton(
                            style: TextButton.styleFrom(
                              shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(30.0)),
                              backgroundColor: accentColor,
                            ),
                            child: Padding(
                              padding: const EdgeInsets.all(8.0),
                              child: Row(
                                mainAxisSize: MainAxisSize.min,
                                children: const [
                                  Text(
                                    'authorize   ',
                                    style: TextStyle(
                                        color: Colors.white, fontSize: 18),
                                  ),
                                  Icon(
                                    Icons.security,
                                    color: Colors.white,
                                  )
                                ],
                              ),
                            ),
                            onPressed: () {
                              {
                                String text = editingController.text;
      
                                if (text.isEmpty) {
                                  setState(() {
                                    errorText = 'please enter seen text';
                                  });
                                  return;
                                }
      
                                editingController.dispose();
      
                                dismiss(context);
      
                                // if (onClicker != null) {}
                              }
                            },
                          ),
                        ],
                      ));
            });
          });
      

      } }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-01-25
        • 1970-01-01
        • 1970-01-01
        • 2020-12-18
        • 2020-11-20
        • 2019-09-14
        • 1970-01-01
        相关资源
        最近更新 更多