Flutter 如何确定文本字段的高度?
正如你所看到的,高度是不同的 左边的图片显示应用程序在键盘未弹出时正在运行。右边的图片显示了当键盘弹出并点击“颤振热加载”(Android Studio)时,应用程序正在运行 我希望文本字段像右边的图片一样,没有键盘弹出窗口 我怎样才能解决这个问题 AppbarFlutter 如何确定文本字段的高度?,flutter,flutter-layout,Flutter,Flutter Layout,正如你所看到的,高度是不同的 左边的图片显示应用程序在键盘未弹出时正在运行。右边的图片显示了当键盘弹出并点击“颤振热加载”(Android Studio)时,应用程序正在运行 我希望文本字段像右边的图片一样,没有键盘弹出窗口 我怎样才能解决这个问题 Appbar class CustomAppbar extends StatelessWidget with PreferredSizeWidget{ @override final Size preferredSize; @overr
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 CustomAppbar使用PreferredSizeWidget扩展无状态小部件{
@凌驾
最终尺寸首选尺寸;
@凌驾
静态双高=AppBar().preferredSize.height;
CustomAppbar():preferredSize=Size.fromHeight(高度);
@凌驾
小部件构建(构建上下文){
@凌驾
final double statusbarHeight=MediaQuery.of(context).padding.top;
返回容器(
子:堆栈(
儿童:[
AppBar(
背景颜色:Colors.white,
海拔:0,
iconTheme:IconThemeData(颜色:Colors.black,),
),
容器(
装饰:盒子装饰(
颜色:颜色。灰色,
边界半径:边界半径。全部(半径。圆形(10))
),
边距:仅限边缘设置(左侧:60,顶部:statusbarHeight+5,底部:5,右侧:5),
子项: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()
)
);
}
}
class InputBox扩展StatefulWidget{
@凌驾
_InputBoxState createState();
}
类_InputBoxState扩展状态{
TextEditingController_SearchController=TextEditingController();
FocusNode_FocusNode=FocusNode();
字符串_SearchText=“”;
_InputBoxState(){
_SearchController.addListener((){
设置状态(){
_SearchText=\u SearchController.text;
});
});
}
@凌驾
小部件构建(构建上下文){
返回文本字段(
focusNode:_focusNode,
样式:TextStyle(字体大小:19),
控制器:\ u搜索控制器,
装饰:输入装饰(
hintText:“搜索”,
边框:InputBorder.none,
前缀:图标(Icons.search,颜色:Colors.white,),
后缀:_focusNode.hasFocus?图标按钮(
图标:图标(Icons.cancel,颜色:Colors.white,),
已按下:(){
设置状态(){
_SearchController.clear();
_SearchText=“”;
_focusNode.unfocus();
});
},
):Container()
)
);
}
}
如果希望在文本字段内填充内容,可以使用ContentPadding
style:TextStyle(height: 20), //custome height
decoration: InputDecoration(
isDense: true) // remove padding inside textfield