简体   繁体   English

如何在 Flutter 的 Dialog 中配置 FocusNode 和 TextEditingController

[英]How to dispose FocusNode and TextEditingController inside a Dialog in Flutter

I have a Dialog which takes input from two Text-Fields and do some task based on the click of Button.我有一个对话框,它从两个文本字段中获取输入,并根据单击按钮执行一些任务。 To handle the focus and input from those text-fields i have created FocusNode and TextEditingControllers .为了处理来自这些文本字段的焦点和输入,我创建了FocusNodeTextEditingControllers

Normally in State of StatefulWidget, we have dispose method in which we can dispose the ChangeNotifier , But i think thats not directly possible in case of dialogs.通常在 StatefulWidget 的状态中,我们有 dispose 方法,我们可以在其中处理ChangeNotifier ,但我认为在对话的情况下这是不可能的。

So, should I avoid diposing those changeNotifiers or instead of building widget anonymously inside of showDialog() i should create a seperate class of StateFulWidget?那么,我应该避免处理这些changeNotifiers,还是应该在showDialog() 内部匿名构建小部件,而应该创建一个单独的StateFulWidget 类?

  1. Create a separate class and make it StatefulWidget ;创建一个单独的类并使其成为StatefulWidget
  2. in the State class use dispose() to dispose your controllers.在 State 类中使用dispose()来处理您的控制器。

Note: avoid having complex code inside of an anonymous functions.注意:避免在匿名函数中使用复杂的代码。

in my case i create dialog with simple textInput and one button ,when user click on button dispose edittext programitically在我的情况下,我使用简单的 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) {}
                        }
                      },
                    ),
                  ],
                ));
      });
    });

} } } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM