how to make two text widgets the same size even they have different text and different fontsize

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

how to make two text widgets the same size even they have different text and different fontsize

问题

flutter如何使两个文本小部件具有相同的大小,即使它们具有不同的文本和不同的字体大小

我尝试过使用FittedBox,但不起作用

英文:

flutter how to make two text widgets the same size even they have different text and different fontsize

I have tried FittedBox but not working

答案1

得分: 0

我认为你希望它们具有相同的高度,对吗?
在这种情况下,你可以使用 IntrinsicHeight。这将强制其子元素具有相同的高度(https://api.flutter.dev/flutter/widgets/IntrinsicHeight-class.html)

FittedBox 只在你想将一个小部件包含在另一个小部件内时使用。我建议观看YouTube视频:'FittedBox(Flutter Widget of the Week)'

以下是一个使用 IntrinsicHeight 的简单代码示例:

IntrinsicHeight(
  child: Row(
    crossAxisAlignment: CrossAxisAlignment.stretch,
    children: [
      Container(
        color: Colors.blue,
        child: Text(
          '短但是很大的文字',
          style: TextStyle(fontSize: 24),
        ),
      ),
      SizedBox(width: 16),
      Container(
        color: Colors.red,
        child: Text(
          '较长的小文字与许多单词',
          style: TextStyle(fontSize: 16),
        ),
      ),
    ],
  ),
),
英文:

I think, you want them to have the same height, right?
In that case you can use IntrinsicHeight. This will will force its children to have the same height (https://api.flutter.dev/flutter/widgets/IntrinsicHeight-class.html)

FittedBox is only if you want to contain a widget inside another widget. I suggest watching on YT: 'FittedBox (Flutter Widget of the Week)'

Here is a simple Code example with IntrinsicHeight:

IntrinsicHeight(
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          Container(
            color: Colors.blue,
            child: Text(
              'Short but big text',
              style: TextStyle(fontSize: 24),
            ),
          ),
          SizedBox(width: 16),
          Container(
            color: Colors.red,
            child: Text(
              'Longer smaller text with many words',
              style: TextStyle(fontSize: 16),
            ),
          ),
        ],
      ),
    ),

huangapple
  • 本文由 发表于 2023年3月31日 21:20:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899020.html
匿名

发表评论

匿名网友

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

确定