降低Flutter中TextButton的高度

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

how to decrease textbutton height Flutter

问题

Container(
            color: Colors.amberAccent[100],
            child: TextButton(
              onPressed: () {},
              child: const Text('View',
                  style: TextStyle(color: Colors.black)),
              style: TextButton.styleFrom(
                padding: EdgeInsets.zero,
              ),
            ),
          )
英文:

How can I decrease the height of the text button? If I force a small height for him it works. But I want the height to be just the height of the text letter. It is possible? I tried wrapping it in an Expanded but it doesn't work.

Container(
            color: Colors.amberAccent[100],
            child: TextButton(
              onPressed: () {},
              child: const Text('Visualizar',
                  style: TextStyle(color: Colors.black)),
              style: TextButton.styleFrom(
                padding: EdgeInsets.zero,
              ),
            ),
          )

as it is now in the code:

降低Flutter中TextButton的高度

How I want it to look:

降低Flutter中TextButton的高度

答案1

得分: 2

你可以使用InkWell创建简单的自定义按钮。这样它的高度与你的Text小部件的高度相同。

英文:

You can create simple custom buttons with InkWell. This way its height is same as your Text widget's height.

class CustomButton extends StatelessWidget {
  const CustomButton({super.key, required this.child, required this.onPressed});

  final Widget child;
  final VoidCallback onPressed;

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: onPressed,
      child: child,
    );
  }
}

答案2

得分: 1

你可以设置TextButton的宽度和高度如下:

TextButton(
    style: TextButton.styleFrom(
        fixedSize: const Size(300, 100),
        foregroundColor: Colors.red,
        backgroundColor: Colors.yellow,
        textStyle: const TextStyle(fontSize: 20)),
    onPressed: () {},
    child: const Text('TextButton宽度和高度'),
),

或者只设置高度,像这样:

TextButton(
    child: Text('文本按钮'),
    style: TextButton.styleFrom(fixedSize: Size.fromHeight(350)),
),
英文:

you can set the width & height of textbutton as follows

TextButton(
            style: TextButton.styleFrom(
                fixedSize: const Size(300, 100),
                foregroundColor: Colors.red,
                backgroundColor: Colors.yellow,
                textStyle: const TextStyle(fontSize: 20)),
            onPressed: () {},
            child: const Text('TextButton width & height'),
          ),

or just set height only like this

TextButton(
  child: Text('Text Button'),
  style: TextButton.styleFrom(fixedSize: Size.fromHeight(350)),
),

答案3

得分: 0

以下是您要翻译的内容:

你也可以用一个固定高度/宽度的 SizedBox/Container 来包裹你的 TextButton。

Container 示例:

Container(
  height: 20.0, // 你想要给的任何高度
  width: 80.0,  // 你想要给的任何宽度
  color: Colors.amberAccent[100],
  child: TextButton(
    onPressed: () {},
    child: const Text('Visualizar',
        style: TextStyle(color: Colors.black)),
    style: TextButton.styleFrom(
      padding: const EdgeInsets.all(4.0),
    ),
  ),
)

SizedBox(它没有颜色参数,这就是我从下面的代码中去掉它的原因)示例:

SizedBox(
  height: 20.0, // 你想要给的任何高度
  width: 80.0,  // 你想要给的任何宽度
  child: TextButton(
    onPressed: () {},
    child: const Text('Visualizar',
        style: TextStyle(color: Colors.black)),
    style: TextButton.styleFrom(
      padding: const EdgeInsets.all(4.0),
    ),
  ),
)
英文:

You can also wrap your TextButton with a fixed height/width SizedBox/Container

Container example:

Container(
        height: 20.0, // whatever height you wanna give
        width: 80.0,  // whatever width you wanna give
        color: Colors.amberAccent[100],
        child: TextButton(
          onPressed: () {},
          child: const Text('Visualizar',
              style: TextStyle(color: Colors.black)),
          style: TextButton.styleFrom(
            padding: const EdgeInsets.all(4.0),
          ),
        ),
      )

SizedBox(It doesn't have the color argument, that's why I removed it from the below code) example:

SizedBox(
        height: 20.0, // whatever height you wanna give
        width: 80.0,  // whatever width you wanna give
        child: TextButton(
          onPressed: () {},
          child: const Text('Visualizar',
              style: TextStyle(color: Colors.black)),
          style: TextButton.styleFrom(
            padding: const EdgeInsets.all(4.0),
          ),
        ),
      )

答案4

得分: 0

TL;DR

使用 visualDensity 属性和/或 tapTargetSize 属性来实现此功能。

参考你的示例:

TextButton(
  onPressed: () {},
  style: TextButton.styleFrom(
    visualDensity: VisualDensity.compact,
    tapTargetSize: MaterialTapTargetSize.shrinkWrap,
  ),
  child: const Text(
     'Visualizar',
     style: TextStyle(color: Colors.black),
   ),
),

Explanation

Flutter 材质文档中提到:

VisualDensity 类

>
> 定义用户界面组件的视觉密度。
>
> 密度在 UI 上下文中是指界面组件的垂直和水平“紧密度”。它是无单位的,因为对不同的 UI 组件来说,它有不同的含义。
>
> [...]
>
> 例如,对于按钮,它影响按钮子元素周围的间距。 [...]
>

tapTargetSize 属性

>
> 配置按钮可以被点击的区域的最小大小。
>
> 如果 tapTargetSize 大于 minimumSize,则按钮将包括一个响应轻触的透明边距。

英文:

TL;DR

Use the visualDensity property and/or tapTargetSize property for this.

Referring to your example:

TextButton(
  onPressed: () {},
  style: TextButton.styleFrom(
    visualDensity: VisualDensity.compact,
    tapTargetSize: MaterialTapTargetSize.shrinkWrap,
  ),
  child: const Text(
     'Visualizar',
     style: TextStyle(color: Colors.black),
   ),
),

Explanation

The Flutter Material Documentation writes about this:

VisualDensity class

>
> Defines the visual density of user interface components.
>
> Density, in the context of a UI, is the vertical and horizontal
> "compactness" of the components in the UI. It is unitless, since it
> means different things to different UI components.
>
> [...]
>
> For example, for buttons, it affects the spacing around the child of the button. [...]
>

tapTargetSize property

>
> Configures the minimum size of the area within which the button may be
> pressed.
>
> If the tapTargetSize is larger than minimumSize, the button will
> include a transparent margin that responds to taps.

huangapple
  • 本文由 发表于 2023年3月23日 10:16:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75818752.html
匿名

发表评论

匿名网友

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

确定