Flutter,如何在TextButton中去除文本周围的空格?

huangapple go评论59阅读模式
英文:

Flutter, how to remove spaces around text in a TextButton?

问题

padding: EdgeInsets.zero 似乎不起作用。

英文:

As titled, padding: EdgeInsets.zero doesn't seem to work.
Flutter,如何在TextButton中去除文本周围的空格?

Container(
  height: 30,
  width: 60,
  padding: EdgeInsets.zero,
  decoration: BoxDecoration(
      color: Colors.green, borderRadius: BorderRadius.circular(6)),
  child: Padding(
    padding: EdgeInsets.zero,
    child: TextButton(
      child: Text('Login', style: TextStyle(fontSize: 14, color: Colors.white)),
      onPressed: () {},
    ),
  ),
),

答案1

得分: 1

你可以这样做。无需使用容器

ElevatedButton(
  onPressed: () {},
  child: Text('Login', style: TextStyle(fontSize: 18)),
  style: ButtonStyle(
    backgroundColor: MaterialStateProperty.all(Colors.green),
    padding: MaterialStateProperty.all(EdgeInsets.zero),
  ),
),

你还可以通过将minimumSize添加到ButtonStyle来更改小部件的最小尺寸。例如:

minimumSize: MaterialStateProperty.all(Size(10, 10)),

这将使最小尺寸为10x10,如果你的文本尺寸足够小的话。但要记住,你也需要能够轻松按下按钮。

还可以查看这个链接:
https://api.flutter.dev/flutter/material/ButtonStyle-class.html

英文:

From your comment:
> The text Login doesn’t fully shown, I would like to maintain the size of the green box, while making the size of the text inside as big as possible.

You can do that like this. No need for Containers

ElevatedButton(
  onPressed: () {},
  child: Text('Login', style: TextStyle(fontSize: 18)),
  style: ButtonStyle(
    backgroundColor: MaterialStateProperty.all(Colors.green),
    padding: MaterialStateProperty.all(EdgeInsets.zero),
  ),
),

You can also change the minimum size of the widget by adding minimumSize to the ButtonStyle. For example:

minimumSize: MaterialStateProperty.all(Size(10, 10)),

This will make the minimum size 10x10, if your text size is small enough.
But keep in mind that you need to be able to easily press the button too.

Also check this:
https://api.flutter.dev/flutter/material/ButtonStyle-class.html

答案2

得分: 0

尝试这样做:将您的容器包装在此中:

MediaQuery.removePadding(
  context: context,
  removeTop: true,
  removeBottom: true,
  child: Container(....)
)
英文:

Try this: Wrap your container in this

MediaQuery.removePadding(
  context:context,
  removeTop:true,
  removeBottom:true,
  child: Container(....)
)

</details>



huangapple
  • 本文由 发表于 2023年2月8日 16:50:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75383271.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定