消除图像抖动周围的空间
你好,我正在制作一个应用程序,其中我有来自相机的图像和发送按钮。图像周围有很多空间,如果图像的高度小于发送按钮,则不再位于底部。我需要图像垂直居中和最大宽度。消息容器应位于底部。 感谢您的帮助。
这是我的应用程序的代码:
return Scaffold(
resizeToAvoidBottomInset: false,
body: Stack(
alignment: Alignment.bottomCenter,
children: [
Image.file(widget.image,),
Container(
child: Row(
children: [
Expanded(
child: Container(
margin: EdgeInsets.all(3),
height: 50,
child: TextField(
controller: _writeMessageTextEditingController,
style: TextStyle(
color: Colors.white),
decoration: InputDecoration(
contentPadding: EdgeInsets.only(left: 15, top: 15,),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(300),
borderSide: BorderSide(
width: 0,
style:
BorderStyle.none,
),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(300),
borderSide: BorderSide(
width: 0,
style: BorderStyle.none,
),
),
filled: true,
hintStyle: TextStyle( color: Colors.grey, ),
hintText: "Message",
fillColor: Colors.grey[800],
),
),
),
),
Container(
margin: EdgeInsets.all(3),
height: 50,
width: 50,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(100),
color: Color(0xff0077b6),
),
padding: EdgeInsets.all(8),
child: GestureDetector(
child: Icon(
Icons.send,
color: Colors.white,
),
onTap: (){
String message = _writeMessageTextEditingController.text.toString();
File file = widget.image;
print(file);
print(message);
_writeMessageTextEditingController.clear();
Navigator.pop(context);
//send message to server
},
),
),
],
),
),
],
),
);
Hello im making an app where I have an image from the camera with a send button. Around the image is much space and if the image's height is less than the send button isnt at the bottom anymore. I need the image vertically centered and a maximun width. The Message container should be on the bottom.
Thanks for your help.
This is the code of my app:
return Scaffold(
resizeToAvoidBottomInset: false,
body: Stack(
alignment: Alignment.bottomCenter,
children: [
Image.file(widget.image,),
Container(
child: Row(
children: [
Expanded(
child: Container(
margin: EdgeInsets.all(3),
height: 50,
child: TextField(
controller: _writeMessageTextEditingController,
style: TextStyle(
color: Colors.white),
decoration: InputDecoration(
contentPadding: EdgeInsets.only(left: 15, top: 15,),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(300),
borderSide: BorderSide(
width: 0,
style:
BorderStyle.none,
),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(300),
borderSide: BorderSide(
width: 0,
style: BorderStyle.none,
),
),
filled: true,
hintStyle: TextStyle( color: Colors.grey, ),
hintText: "Message",
fillColor: Colors.grey[800],
),
),
),
),
Container(
margin: EdgeInsets.all(3),
height: 50,
width: 50,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(100),
color: Color(0xff0077b6),
),
padding: EdgeInsets.all(8),
child: GestureDetector(
child: Icon(
Icons.send,
color: Colors.white,
),
onTap: (){
String message = _writeMessageTextEditingController.text.toString();
File file = widget.image;
print(file);
print(message);
_writeMessageTextEditingController.clear();
Navigator.pop(context);
//send message to server
},
),
),
],
),
),
],
),
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,可能有无数种方法可以实现这一目标。我选择使用
Column
及其属性来对齐两个堆叠的项目。聊天栏和发送按钮
让我们从聊天栏和发送按钮开始。它本质上可以归结为:
确保您的聊天栏整齐地与屏幕底部对齐。此外,我删除了堆栈中的
alignment
属性,因为当键盘打开打字时,这不会向上移动聊天栏,我认为这没有意义。然后对于图像
,我使用了另一列(尽管特别是这里存在很多选项,我只是喜欢我的列),它看起来像:
请注意,为图像提供宽度
double.maxFinite
确保它是当宽度小于屏幕宽度时水平居中。对于普通图像(高分辨率
),它使图像垂直居中,并水平填充可用空间。如果键盘打开,聊天栏会向上移动,并且图像现在位于剩余空间的中心。如果图像足够高,
stack
将导致图像与聊天栏稍微重叠。如果它不够宽,它只会水平居中。请参阅下面的屏幕截图:最后,对于小图像,它将水平和垂直居中。

代码
为了完整起见,这是我使用的代码。对于图像,我使用了网络图像,但同样的事情也应该适用。
So to start off, there are probably countless ways to achieve this. I opted to use
Column
s and their properties to align both of your stacked items.The chat bar and send button
Let's start with the chat bar and send button. It essentially boils down to:
Which ensures that your chat bar neatly aligns to the bottom of the screen. Moreover, I removed the
alignment
property in the stack, as that would not move the chat bar up when the keyboard opens to type, which doesn't make sense I think.The image
Then for the image, I used another column (though especially here many options exist, I just like my columns), which looks like:
Note that giving the image a width
double.maxFinite
ensures it is centered horizontally when the width is less than your screen width.What does that look like
For a normal image (high resolution) it centers the image vertically, and fills up the available space horizontally. If the keyboard is opened, the chat bar moves up, and the image is now centered in the remaining space. If the image is high enough the
stack
will cause the image to overlap a bit with the chat bar. If it's then not wide enough it will simply center horizontally. See the screenshots below:Lastly for a small image, it centers it both horizontally and vertically.

The code
For completeness, here's the code I used. For the image I used a network image but the same things should apply.