消除图像抖动周围的空间

发布于 2025-01-10 23:43:47 字数 1558 浏览 4 评论 0原文

你好,我正在制作一个应用程序,其中我有来自相机的图像和发送按钮。图像周围有很多空间,如果图像的高度小于发送按钮,则不再位于底部。我需要图像垂直居中和最大宽度。消息容器应位于底部。 感谢您的帮助。

这是我的应用程序的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾铮苏瑾 2025-01-17 23:43:47

首先,可能有无数种方法可以实现这一目标。我选择使用Column及其属性来对齐两个堆叠的项目。

聊天栏和发送按钮

让我们从聊天栏和发送按钮开始。它本质上可以归结为:

Column(
  mainAxisSize: MainAxisSize.max,
  mainAxisAlignment: MainAxisAlignment.end,
  children: [RowWithTextBarAndSendButton(...)],
),

确保您的聊天栏整齐地与屏幕底部对齐。此外,我删除了堆栈中的 alignment 属性,因为当键盘打开打字时,这不会向上移动聊天栏,我认为这没有意义。

然后对于图像

,我使用了另一列(尽管特别是这里存在很多选项,我只是喜欢我的列),它看起来像:

Column(
  mainAxisAlignment: MainAxisAlignment.center,
  crossAxisAlignment: CrossAxisAlignment.center,
  children: [
    Expanded(
      child: Image(..., width: double.maxFinite,),
    ),
  ],
),

请注意,为图像提供宽度 double.maxFinite 确保它是当宽度小于屏幕宽度时水平居中。

对于普通图像(高分辨率

),它使图像垂直居中,并水平填充可用空间。如果键盘打开,聊天栏会向上移动,并且图像现在位于剩余空间的中心。如果图像足够高,stack 将导致图像与聊天栏稍微重叠。如果它不够宽,它只会水平居中。请参阅下面的屏幕截图:

默认情况下的样子

最后,对于小图像,它将水平和垂直居中。
小图片example

代码

为了完整起见,这是我使用的代码。对于图像,我使用了网络图像,但同样的事情也应该适用。

return Scaffold(
      body: Stack(
        children: [
          Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
            Expanded(child: Image.network('https://upload.wikimedia.org/wikipedia/commons/thumb/4/4d/Cat_November_2010-1a.jpg/1200px-Cat_November_2010-1a.jpg', width: double.maxFinite,),),
          ],),
          Column(
            mainAxisSize: MainAxisSize.max,
            mainAxisAlignment: MainAxisAlignment.end,
            children: [Row(
            children: [
              Expanded(
                child: Container(
                  margin: EdgeInsets.all(3),
                  height: 50,
                  child: TextField(
                    controller: _controller,
                    style: const TextStyle(
                        color: Colors.white),
                    decoration: InputDecoration(
                      contentPadding: const EdgeInsets.only(left: 15, top: 15,),
                      border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(300),
                        borderSide: const BorderSide(
                          width: 0,
                          style:
                          BorderStyle.none,
                        ),
                      ),
                      focusedBorder: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(300),
                        borderSide: const BorderSide(
                          width: 0,
                          style: BorderStyle.none,
                        ),
                      ),
                      filled: true,
                      hintStyle: const TextStyle( color: Colors.grey, ),
                      hintText: "Message",
                      fillColor: Colors.grey[800],
                    ),
                  ),
                ),
              ),
              Container(
                margin: const EdgeInsets.all(3),
                height: 50,
                width: 50,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(100),
                  color: const Color(0xff0077b6),
                ),
                padding: const EdgeInsets.all(8),
                child: GestureDetector(
                  child: const Icon(
                    Icons.send,
                    color: Colors.white,
                  ),
                  onTap: (){

                  },
                ),
              ),
            ],
          )],
          ),
        ],
      ),
    );

So to start off, there are probably countless ways to achieve this. I opted to use Columns 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:

Column(
  mainAxisSize: MainAxisSize.max,
  mainAxisAlignment: MainAxisAlignment.end,
  children: [RowWithTextBarAndSendButton(...)],
),

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:

Column(
  mainAxisAlignment: MainAxisAlignment.center,
  crossAxisAlignment: CrossAxisAlignment.center,
  children: [
    Expanded(
      child: Image(..., width: double.maxFinite,),
    ),
  ],
),

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:

What it looks like by default
With the keyboard open

Lastly for a small image, it centers it both horizontally and vertically.
Small image example

The code

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

return Scaffold(
      body: Stack(
        children: [
          Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
            Expanded(child: Image.network('https://upload.wikimedia.org/wikipedia/commons/thumb/4/4d/Cat_November_2010-1a.jpg/1200px-Cat_November_2010-1a.jpg', width: double.maxFinite,),),
          ],),
          Column(
            mainAxisSize: MainAxisSize.max,
            mainAxisAlignment: MainAxisAlignment.end,
            children: [Row(
            children: [
              Expanded(
                child: Container(
                  margin: EdgeInsets.all(3),
                  height: 50,
                  child: TextField(
                    controller: _controller,
                    style: const TextStyle(
                        color: Colors.white),
                    decoration: InputDecoration(
                      contentPadding: const EdgeInsets.only(left: 15, top: 15,),
                      border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(300),
                        borderSide: const BorderSide(
                          width: 0,
                          style:
                          BorderStyle.none,
                        ),
                      ),
                      focusedBorder: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(300),
                        borderSide: const BorderSide(
                          width: 0,
                          style: BorderStyle.none,
                        ),
                      ),
                      filled: true,
                      hintStyle: const TextStyle( color: Colors.grey, ),
                      hintText: "Message",
                      fillColor: Colors.grey[800],
                    ),
                  ),
                ),
              ),
              Container(
                margin: const EdgeInsets.all(3),
                height: 50,
                width: 50,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(100),
                  color: const Color(0xff0077b6),
                ),
                padding: const EdgeInsets.all(8),
                child: GestureDetector(
                  child: const Icon(
                    Icons.send,
                    color: Colors.white,
                  ),
                  onTap: (){

                  },
                ),
              ),
            ],
          )],
          ),
        ],
      ),
    );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文