Android 如何将键盘连接到textfield并使textfield始终保持在前台?如何包装ListView.builder内容?
所以,我正在尝试编写一个简单的Notes应用程序。 它应该在应用程序启动时将键盘打开到文本字段上,当我单击后退按钮时,它应该将键盘最小化,但使文本字段保持在屏幕底部的前景上Android 如何将键盘连接到textfield并使textfield始终保持在前台?如何包装ListView.builder内容?,android,flutter,Android,Flutter,所以,我正在尝试编写一个简单的Notes应用程序。 它应该在应用程序启动时将键盘打开到文本字段上,当我单击后退按钮时,它应该将键盘最小化,但使文本字段保持在屏幕底部的前景上 如何在ListView.builder中包装内容,使其只占用notes所需的空间量 如何使文本字段始终位于屏幕底部的前景中 是否可以连接键盘布局和文本字段?因此,当我点击文本字段时,它会从底部打开键盘,并用它向上推文本字段,而不是仅仅打开文本字段上方的键盘,从而将其隐藏在背景中 class\u HomeState扩展状态{
class\u HomeState扩展状态{
列表注释=[“注释1”、“注释2”、“注释4”、“注释5”];
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:正文(
“Notizen应用程序”,
),
背景颜色:颜色。灰色[700],
标题:对,
),
正文:专栏(
儿童:[
容器(
高度:MediaQuery.of(context).size.height,
子项:ListView.builder(
itemCount:notes.length,
itemBuilder:(上下文,索引){
回程卡(
子:列(
儿童:[
列表砖(
onTap:(){},
标题:文本(注释[索引]),
拖尾:扁平按钮(
已按下:(){
设置状态(){
注:删除(索引);
});
},
子项:文本(“删除”),
颜色:颜色,红色,
),
),
],
));
}),
),
文本字段(
键盘类型:TextInputType.multiline,
maxLines:2,
装饰:输入装饰(hintText:“输入精彩创意”),
自动对焦:对,
)
],
),
);
}
}
在与@Abion讨论之后,我已经测试了以下代码,它正在工作
class _MyHomePageState extends State<Home> {
int _counter = 0;
List<String> notes = ["Note1", "Note2", "Note4", "Note5","Note6"];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
"Notizen App",
),
backgroundColor: Colors.grey[700],
centerTitle: true,
),
body: Column(
children: [
Flexible(
child: ListView.builder(
itemCount: notes.length,
itemBuilder: (context, index) {
return Card(
child: ListTile(
onTap: () {},
title: Text(notes[index]),
trailing: FlatButton(
onPressed: () {
setState(() {
notes.removeAt(index);
});
},
child: Text("Delete"),
color: Colors.red,
),
));
}),
),
Expanded(
flex: 0,
child: Align(
alignment: FractionalOffset.bottomCenter,
child: Container(
child: TextField(
keyboardType: TextInputType.multiline,
maxLines: 2,
decoration: InputDecoration(hintText: "Enter brilliant idea"),
autofocus: true,
),
),
),
)
],
),
);
}
}
class\u MyHomePageState扩展状态{
int _计数器=0;
列表注释=[“注释1”、“注释2”、“注释4”、“注释5”、“注释6”];
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:正文(
“Notizen应用程序”,
),
背景颜色:颜色。灰色[700],
标题:对,
),
正文:专栏(
儿童:[
灵活的(
子项:ListView.builder(
itemCount:notes.length,
itemBuilder:(上下文,索引){
回程卡(
孩子:ListTile(
onTap:(){},
标题:文本(注释[索引]),
拖尾:扁平按钮(
已按下:(){
设置状态(){
注:删除(索引);
});
},
子项:文本(“删除”),
颜色:颜色,红色,
),
));
}),
),
扩大(
弹性:0,
子对象:对齐(
对齐:分馏Loffset.bottomCenter,
子:容器(
孩子:TextField(
键盘类型:TextInputType.multiline,
maxLines:2,
装饰:输入装饰(hintText:“输入精彩创意”),
自动对焦:对,
),
),
),
)
],
),
);
}
}
请将您的帖子限制为每篇帖子一个问题。把你所有的问题都放在一个帖子里会让人们很难给出高质量的答案,并且会让人们在将来遇到类似问题时更难找到你的问题。啊,好的。很抱歉谢谢你!非常感谢。我明天会试试。在Flutter中猜测小部件的大小通常是一个信号,表明您使用的Flutter小部件是错误的。在这种情况下,使用列
和扩展
比硬编码要好。不良做法通常意味着您使用的工具是错误的。硬编码小部件高度(特别是以一种与小部件本身没有直接关系的方式)会导致脆弱的UI,它只能在开发它的设备上工作,并且当最终用户在具有不同分辨率或屏幕比率的设备上查看它时,它会看起来损坏或实际上损坏。只需将外部列表视图
替换为列
,将容器
替换为扩展的
即可获得相同的结果,而不必依赖脆弱的幻数,让Flatter为您进行布局。您尝试过吗?我已经将您的代码复制到一个项目中,只做了我提到的更改,它可以很好地满足所有3个问题,而不需要硬编码文本字段高度。不,固定高度在不同分辨率和/或屏幕比例的屏幕上看起来会有所不同。非常感谢你们。帮助像我这样的人:D
class _MyHomePageState extends State<Home> {
int _counter = 0;
List<String> notes = ["Note1", "Note2", "Note4", "Note5","Note6"];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
"Notizen App",
),
backgroundColor: Colors.grey[700],
centerTitle: true,
),
body: Column(
children: [
Flexible(
child: ListView.builder(
itemCount: notes.length,
itemBuilder: (context, index) {
return Card(
child: ListTile(
onTap: () {},
title: Text(notes[index]),
trailing: FlatButton(
onPressed: () {
setState(() {
notes.removeAt(index);
});
},
child: Text("Delete"),
color: Colors.red,
),
));
}),
),
Expanded(
flex: 0,
child: Align(
alignment: FractionalOffset.bottomCenter,
child: Container(
child: TextField(
keyboardType: TextInputType.multiline,
maxLines: 2,
decoration: InputDecoration(hintText: "Enter brilliant idea"),
autofocus: true,
),
),
),
)
],
),
);
}
}