当前位置:   article > 正文

flutter输入框底部下拉框_flutter textfield实现下拉并选择

flutter textfield实现下拉并选择
final GlobalKey _noticeKey = GlobalKey();
  OverlayEntry _overlayEntry;
  final LayerLink _layerLink = LayerLink();
 
  void initState() {
  _linkFocusNode.addListener(() {
      if (_linkFocusNode.hasFocus ) {
        _showOverlay(context);
      } else {
        _dismissOverlay();
      }
    });
}
_showOverlay(BuildContext context) {
    final overlay = Overlay.of(context);
    _overlayEntry = OverlayEntry(builder: (ctx) {
      final renderBox =
      _noticeKey?.currentContext?.findRenderObject() as RenderBox;
      final size = renderBox.size;
      final offset = renderBox.localToGlobal(Offset.zero);

      //使用CompositedTransformFollower,可以不对Positioned 设置left,top
      return Positioned(
        // left: offset.dx,
        // top: offset.dy + size.height,
        width: size.width,
        child: CompositedTransformFollower(
          link: _layerLink,
          showWhenUnlinked: false,
          offset: Offset(0, size.height + 8),
          child: Material(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Text("第一条"),
                Text("第而条"),
                Text("第三条"),
              ],
            ),
          ),
        ),
      );
    });
    overlay?.insert(_overlayEntry);
  }

_dismissOverlay() {
    if (_overlayEntry != null) {
      _overlayEntry?.remove();
      _overlayEntry = null;
    }
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

使用

CompositedTransformTarget(
        link: _layerLink,
        child: Container(
          key: _noticeKey,
          height: 60.h,
          decoration: BoxDecoration(
            color: JadeColors.grey_5,
            borderRadius: BorderRadius.circular(5),
          ),
          child: Row(
            children: [
              GestureDetector(
                child: Container(
                  height: double.infinity,
                  padding: EdgeInsets.only(left: 20.w,right: 10.w),
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Text(_inStation?'站内':'站外',style: TextStyle(color: JadeColors.grey_2,fontSize: setFontSize(24)),),
                      Icon(Icons.keyboard_arrow_down,size: 16,color: JadeColors.grey_2,)
                    ],
                  ),
                ),
                onTap: (){
                  setState(() {
                    _inStation = !_inStation;
                  });
                },
              ),
              Container(
                width: 1.5,
                height: double.infinity,
                color: JadeColors.grey_6,
              ),
              Expanded(child:
              TextField(
                textAlign: TextAlign.start,
                controller: _linkTextEditingController,
                focusNode: _linkFocusNode,
                style: TextStyle(fontSize: 35.sp, color: JadeColors.grey),
                keyboardType: TextInputType.text,
                textInputAction: TextInputAction.done,
                maxLines: 1,
                decoration: InputDecoration(
                  filled: true,
                  fillColor: JadeColors.lightGrey,
                  contentPadding: EdgeInsets.only(
                      left: 20.w,
                      right: 20.w,
                      top: 10.w,
                      bottom: 10.w
                  ),
                  hintText:'请输入跳转的商品链接',
                  hintStyle: TextStyle(
                    fontSize: 28.sp,
                    color: JadeColors.grey,
                  ),
                  border: OutlineInputBorder(
                      borderSide: BorderSide.none,
                      borderRadius: BorderRadius.circular(8.w)),
                  // enabledBorder: OutlineInputBorder(
                  //    // borderSide: BorderSide.none,
                  //     borderRadius: BorderRadius.circular(8.w)),
                  focusedBorder: OutlineInputBorder(
                      borderSide: BorderSide.none,
                      borderRadius: BorderRadius.circular(8.w)),
                ),
                cursorColor: JadeColors.grey,
                onEditingComplete: (){
                  if(_inStation){
                    FocusScope.of(context).requestFocus(_goodsNameFocusNode);
                  }else{
                    FocusScope.of(context).requestFocus(_contactsFocusNode);
                  }

                },
              )
              )
            ],
          ),
        ),
        )
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/736409
推荐阅读
相关标签
  

闽ICP备14008679号