【问题标题】:Textfield focus node staying focused文本字段焦点节点保持专注
【发布时间】:2021-01-25 13:07:26
【问题描述】:

我一直在关注这篇文章https://medium.com/saugo360/https-medium-com-saugo360-flutter-using-overlay-to-display-floating-widgets-2e6d0e8decb9 在我的搜索栏下方添加一个自动完成框,除了覆盖框一直保持打开状态之外,即使我关闭了文本字段所在的汉堡菜单,一切似乎都有效。即使进入另一个页面,叠加层也会保持打开状态。这使得人们认为焦点节点没有按应有的方式工作。有谁知道如何解决这个问题?

class TagSearchBox extends StatefulWidget {
  SearchGlobals searchGlobals;
  TextEditingController searchTagsController;
  TagSearchBox(this.searchGlobals, this.searchTagsController);
  @override
  _TagSearchBoxState createState() => _TagSearchBoxState();
}

class _TagSearchBoxState extends State<TagSearchBox> {
  final FocusNode _focusNode = FocusNode();
  OverlayEntry _overlayEntry;
  @override
  void initState() {
    _focusNode.addListener(() {
      if (_focusNode.hasFocus) {
        this._overlayEntry = this._createOverlayEntry();
        Overlay.of(context).insert(this._overlayEntry);
      } else {
        this._overlayEntry.remove();
      }
    });

  }
  OverlayEntry _createOverlayEntry() {
    RenderBox renderBox = context.findRenderObject();
    GelbooruHandler booru = widget.searchGlobals.booruHandler;
    var size = renderBox.size;
    var offset = renderBox.localToGlobal(Offset.zero);

    return OverlayEntry(
        builder: (context) => Positioned(
          left: offset.dx,
          top: offset.dy + size.height + 5.0,
          width: size.width,
          child: Material(
            elevation: 4.0,
            child: FutureBuilder(
                  future: booru.tagSearch(widget.searchTagsController.text),
                    builder: (BuildContext context, AsyncSnapshot snapshot) {
                      if (snapshot.connectionState == ConnectionState.done && snapshot.hasData){
                        return ListView.builder(
                            padding: EdgeInsets.zero,
                            shrinkWrap: true,
                          itemCount: snapshot.data.length,
                            itemBuilder: (BuildContext context, int index) {
                            return ListTile(
                                title: Text(snapshot.data[index]),
                                onTap: (() {
                                  widget.searchTagsController.text = snapshot.data[index];
                                })[![enter image description here][1]][1]
                            );
                          }
                        );
                      } else {
                        return Center(child: CircularProgressIndicator());
                      }
                    }
                ),
            ),
        ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: TextField(
          controller: widget.searchTagsController,
          focusNode: this._focusNode,
          decoration: InputDecoration(
            hintText:"Enter Tags",
            contentPadding: new EdgeInsets.fromLTRB(15,0,0,0), // left,right,top,bottom
            border: new OutlineInputBorder(
              borderRadius: new BorderRadius.circular(50),
              gapPadding: 0,
            ),
          ),
      )
    );
  }
}

【问题讨论】:

    标签: android flutter


    【解决方案1】:

    通过将我的整个家庭脚手架放在手势检测器中来修复它

    onTap: (){
              FocusScope.of(context).unfocus();
            },
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-18
      • 1970-01-01
      • 2010-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多