如何确定Textfield的高度?

时间:2020-08-16 10:30:55

标签: flutter flutter-layout

enter image description here

如您所见,高度是不同的。

左图显示了未弹出键盘时正在运行的应用程序。右图显示了当键盘弹出并单击“ Flutter Hot Reload”(Android Studio)时正在运行的应用程序。

我希望文本字段像右边的图片一样,没有弹出键盘。

我该如何解决?

应用栏

class CustomAppbar extends StatelessWidget with PreferredSizeWidget{
  @override
  final Size preferredSize;

  @override
  static double height = AppBar().preferredSize.height;

  CustomAppbar() : preferredSize = Size.fromHeight(height);

  @override
  Widget build(BuildContext context) {
    @override
    final double statusbarHeight = MediaQuery.of(context).padding.top;

    return Container(
      child: Stack(
        children: <Widget>[
          AppBar(
            backgroundColor: Colors.white,
            elevation: 0,
            iconTheme: IconThemeData(color: Colors.black,),
          ),
          Container(
            decoration: BoxDecoration(
                color: Colors.grey,
                borderRadius: BorderRadius.all(Radius.circular(10))
            ),
            margin: EdgeInsets.only(left: 60, top: statusbarHeight + 5, bottom: 5, right: 5),
            child: InputBox(),
          )
        ],
      ),
    );
  }
}

输入框

class InputBox extends StatefulWidget {
  @override
  _InputBoxState createState() => _InputBoxState();
}

class _InputBoxState extends State<InputBox> {
  TextEditingController _SearchController = TextEditingController();
  FocusNode _focusNode = FocusNode();
  String _SearchText = "";

  _InputBoxState(){
    _SearchController.addListener(() {
      setState((){
        _SearchText = _SearchController.text;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return TextField(
      focusNode: _focusNode,
      style: TextStyle(fontSize: 19),
      controller: _SearchController,
      decoration: InputDecoration(
          hintText: "Search",
          border: InputBorder.none,
          prefixIcon: Icon(Icons.search, color: Colors.white,),
          suffixIcon: _focusNode.hasFocus ? IconButton(
            icon: Icon(Icons.cancel, color: Colors.white,),
            onPressed: (){
              setState((){
                _SearchController.clear();
                _SearchText = "";
                _focusNode.unfocus();
              });
            },
          ) : Container()
      )
    );
  }
}

1 个答案:

答案 0 :(得分:1)

  style:TextStyle(height: 20),     //custome height
  decoration: InputDecoration(
  isDense: true)                    // remove padding inside textfield

如果要在文本字段内填充,可以使用ContentPadding