简体   繁体   English

如何在自定义 TextField 中设置 controller 和 focusnode - Flutter

[英]How to set controller and focusnode in custom TextField - Flutter

I'm trying to create my custom TextField widget with a dropdown list.我正在尝试使用下拉列表创建我的自定义 TextField 小部件。 I want to give it arguments of controller and focusnode.我想给它controller和focusnode的arguments。 I don't want them to be required, but if they are not, I can't set them for Controller() and FocusNode() .我不希望它们是必需的,但如果它们不是,我不能为Controller()FocusNode()设置它们。 And I can't have them with null value cause I already work with them inside the class.而且我不能让它们具有null值,因为我已经在 class 中使用它们。 I'm not sure how to do this.我不知道该怎么做。 Here is what I have:这是我所拥有的:

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

class DropdownText extends StatefulWidget {
  final TextEditingController controller;
  final FocusNode focusNode;

  const DropdownText({Key key, this.controller, this.focusNode}) : super(key: key);
  @override
  _DropdownTextState createState() => _DropdownTextState();
}

class _DropdownTextState extends State<DropdownText> {

  bool _show = false;

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

    widget.focusNode.addListener(listener);
  }

  void listener(){
    if(widget.focusNode.hasFocus){
      setState(() {
        _show = true;
      });
    }else{
      setState(() {
        _show = false;
      });
    }
  }

}

And I already work with the text of my TextField (using the controller.text ) inside this class when someone touches one of the options for changing whats written on it.当有人触摸用于更改其上所写内容的选项之一时,我已经在此 class 中使用了我的 TextField 的文本(使用controller.text )。 Is there any way for me to give the user of my widget access for the controller and the focusNode just as TextField does?有什么方法可以让我的小部件用户访问controllerfocusNode ,就像 TextField 一样?

PS: English is not my first language and I'm starting to programm with OOP with flutter. PS:英语不是我的第一语言,我开始使用 OOP 和 flutter 进行编程。

Edit:编辑:

After the solution here is the link for the github repository.此处的解决方案之后是 github 存储库的链接。

You can use two separate objects in _DropdownTextState and initialize them in initState() .您可以在 _DropdownTextState 中使用两个单独的对象并在_DropdownTextState initState()中对其进行初始化。

Checkout out below code.签出下面的代码。

class _DropdownTextState extends State<DropdownText> {
  TextEditingController _controller;
  FocusNode _focusNode;

  bool _show = false;

  @override
  void initState() {
    super.initState();
    if (widget.controller != null)
      _controller = widget.controller;
    else
      _controller = TextEditingController();
    if (widget.focusNode != null)
      _focusNode = widget.focusNode;
    else
      _focusNode = FocusNode();

    _focusNode.addListener(listener);
  }

  @override
  void dispose() {
    super.dispose();
    _controller?.dispose();
    _focusNode?.dispose();
  }

  void listener() {
    if (widget.focusNode.hasFocus) {
      setState(() {
        _show = true;
      });
    } else {
      setState(() {
        _show = false;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return TextField(
      focusNode: _focusNode,
      controller: _controller,
    );
  }
}

Hope it helps:)希望能帮助到你:)

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

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