简体   繁体   English

Flutter 中带有图标的按钮内的居中文本

[英]Centered Text inside a button with an icon in Flutter

I'm having issues where I want to make a button (any kind to achieve this) that has an icon that is always in the same position at the start of the button, and that the text of the button is always centered compared to the text below it and always centered in the button itself, even if the text is dynamic.我遇到了问题,我想制作一个按钮(任何类型来实现此目的),该按钮的图标始终位于按钮开头的相同位置,并且与按钮相比,按钮的文本始终居中它下方的文本并始终以按钮本身为中心,即使文本是动态的。 This is what I want to achieve:这就是我想要实现的目标: 在此处输入图片说明

The button text is centered according to the text below(that stays the same) and it is centered with the above button, but if the button text changes dynamically, then I have these issues:按钮文本根据下面的文本居中(保持不变),并与上面的按钮居中,但如果按钮文本动态变化,那么我有这些问题:

在此处输入图片说明

Or like this one:或者像这个:

在此处输入图片说明

Any ideas how to solve this problem?任何想法如何解决这个问题?

I tried with the Expanded widget and adding the flex properties but failed.我尝试使用Expanded小部件并添加 flex 属性但失败了。 I tried some other stuff too but failed also... Here is the button code:我也尝试了其他一些东西,但也失败了......这是按钮代码:

TextButton(
                              style: ButtonStyle(
                                  backgroundColor:
                                      MaterialStateProperty.all(Colors.blue)),
                              onPressed: () {},
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.start,
                                children: [
                                  Expanded(
                                    flex: 1,
                                    child: Icon(
                                      Icons.keyboard_arrow_right_sharp,
                                      color: Colors.white,
                                    ),
                                  ),
                                  Expanded(
                                    flex: 4,
                                    child: Center(
                                      child: Text(
                                        "SOME TEXT",
                                        style: TextStyle(color: Colors.white),
                                      ),
                                    ),
                                  )
                                ],
                              ),
                            ),

Thanks in advance for your help!在此先感谢您的帮助!

There are a couple of options to do that.有几个选项可以做到这一点。 I'm going to assume the size of the text could change so you want a flexible solution.我将假设文本的大小可能会发生变化,因此您需要一个灵活的解决方案。

The first option is the most obvious of the two, but will break if the text is too long.第一个选项是两个选项中最明显的,但如果文本太长会中断。 The trick is to add an empty sizedBox at the end of the button, so the text can be centered in an expanded widget.诀窍是在按钮的末尾添加一个空的 sizedBox,这样文本就可以在展开的小部件中居中。

TextButton(
  style: ButtonStyle(
    backgroundColor: MaterialStateProperty.all(Colors.blue),
  ),
  onPressed: () {},
  child: Row(
    children: [
      Icon(
        Icons.keyboard_arrow_right_sharp,
          color: Colors.white,
          size: 24,
      ),
      Expanded(
        child: Center(
          child: Text(
            "SOME TEXT",
            style: TextStyle(
              color:Colors.white,
            ),
          ),
        ),
      ),
      SizedBox(
        width: 24, // width of the icon
      ),
    ],
  ),
);

The other option is using a stack widget.另一种选择是使用堆栈小部件。 This way, if the text is too long it will overlap with the icon but it's less probable that it will overflow the button.这样,如果文本太长,它会与图标重叠,但不太可能会溢出按钮。

TextButton(
  style: ButtonStyle(
    backgroundColor: MaterialStateProperty.all(Colors.blue),
  ),
  onPressed: () {},
  child: Stack(
    children: [
      Positioned(
        left: 0,
        child: Icon(
          Icons.keyboard_arrow_right_sharp,
          color: Colors.white,
          size: 24,
        ),
      ),
      Center(
        child: Text(
          "SOME TEXT",
          style: TextStyle(
            color:Colors.white,
          ),
        ),
      ),
    ],
  ),
);

Let's discuss about you code : using row with a mainaxisalignment from start make the element start from the available area.让我们讨论一下您的代码:从开始使用带有主轴对齐的行使元素从可用区域开始。 then i suggest to use the SizedBox to make some of spacing between the elements as you want and play with the width of it.然后我建议使用SizedBox根据需要在元素之间SizedBox出一些间距并调整它的宽度。

TextButton(
                          style: ButtonStyle(
                              backgroundColor:
                                  MaterialStateProperty.all(Colors.blue)),
                          onPressed: () {},
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.start, // here the issue
                            children: [
                              Expanded(
                                flex: 1,
                                child: Icon(
                                  Icons.keyboard_arrow_right_sharp,
                                  color: Colors.white,
                                ),
                              ),
                             SizedBox(width: MediaQuery.of(context).size.width * 0.08, // change this value as you want 0.08 or 0.1 ...etc
      ),
                              Expanded(
                                flex: 4,
                                child: Center(
                                  child: Text(
                                    "SOME TEXT",
                                    style: TextStyle(color: Colors.white),
                                  ),
                                ),
                              )
                            ],
                          ),
                        ),

for more read this documentation有关更多信息,请阅读此文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM