在Flutter中,如何使用FittedBox调整行内文本的大小?

时间:2019-02-26 16:08:53

标签: layout flutter

我有一排,每侧都有一个图标,然后是中间带有图标的文本:

@override
Widget build(BuildContext context) {
  return Row(
    children: [
      Icon(Icons.arrow_back),
      Expanded(child: SizedBox()),
      Icon(Icons.account_box),
      Text("Some Text Here ", maxLines: 1), // ➜ This is the text.
      Expanded(child: SizedBox()),
      Icon(Icons.arrow_forward),
    ],
  );
}

当文本足够大以填充整个空间时,我希望将FittedBox缩小。所以我尝试了这个:

      ...
      FittedBox(child: Text("Some Text Here. More. More. More. More. More. More. More. More. ", maxLines: 1)), 
      ...

它不起作用(溢出)。我认为问题在于Row并没有告诉FittedBox它可以拥有的最大大小。我曾考虑过将FittedBoxExpandedIntrinsicWidthUnconstrainedBoxFlexibleAlign一起使用,但无济于事。

我该如何解决?

2 个答案:

答案 0 :(得分:0)

您是否只想使用public class Recursive { static int[] combo = new int[100]; public static void main(String argv[]) { int n = 8; int[] amounts = {1, 5, 10}; ways(n, amounts, combo, 0, 0, 0); } public static void ways(int n, int[] amounts, int[] combo, int count, int sum, int index) { if(sum == n) { printArray(combo, index); } if(sum > n) { return; } for(int i=0;i<amounts.length;i++) { sum = sum + amounts[i]; combo[index] = amounts[i]; ways(n, amounts, combo, 0, sum, index + 1); sum = sum - amounts[i]; } } public static void printArray(int[] combo, int index) { for(int i=0;i < index; i++) { System.out.print(combo[i] + " "); } System.out.println(); } } 来使同一行中的文本适合?另外,如果您需要将文本放在一行中或将其换行,则需要将FittedBox封装在Text小部件内,这基本上适合孩子。

我重新创建了您的案件,并能够实现以下目标:

enter image description here

以上代码为:

Flexible

自从您将Row( children: [ Icon(Icons.arrow_back), Expanded(child: SizedBox()), Icon(Icons.account_box), Flexible( child: Text("Some Text Here and here and here and here and more and more and more and more and more and more ", maxLines: 1)), // ➜ This is the text. Expanded(child: SizedBox()), Icon(Icons.arrow_forward), ], ) 设置为1以来,它将采用原始文本maxLines并将正确显示它而不会溢出。如果您向其中添加更多文本,它将不会扩展该行,并且可以防止溢出。

答案 1 :(得分:0)

据我所知,您需要根据文本长度在行内填充文本。

工作代码:

Row(
       mainAxisAlignment: MainAxisAlignment.spaceBetween,
       children: [
         Icon(Icons.arrow_back),
         Expanded(
             child: Row(
               mainAxisAlignment: MainAxisAlignment.center,
               children: <Widget>[
                 Icon(Icons.account_box),
                 Flexible(
                   child: FittedBox(
                     fit: BoxFit.scaleDown,
                     child: Text(
                         "  Text HereSome Text Here Text HereSomext HereSome TText HereSome Text HereSome Text HereSome Text HereSome Text HereSome TText HereSome Text HereSome Text HereSome",
                         maxLines: 1),
                   ))])),
              Icon(Icons.arrow_forward),
            ]);

输出长文本:

enter image description here

输出,短文本:

enter image description here