flutter 垫片导致溢出问题

pbpqsu0x  于 2022-12-24  发布在  Flutter
关注(0)|答案(2)|浏览(176)

我在一行中有两个文本,其中一个比另一个长。我使用spacer给予其中一个文本更多的空间。如果文本太长,它会导致溢出。我该如何解决这个问题?

title: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Spacer(),
          Text(
            "Longer text",
            style: Theme.of(context).textTheme.bodyText1?.copyWith(
                  color:
                      Theme.of(context).colorScheme.white.withOpacity(0.85),
                ),
            overflow: TextOverflow.fade,
            softWrap: false,
            textAlign: TextAlign.right,
          ),
          const SizedBox(width: 10),
          Text(
            "smaller text",
            style: Theme.of(context).textTheme.bodyText1?.copyWith(
                  color:
                      Theme.of(context).colorScheme.white.withOpacity(0.85),
                ),
            overflow: TextOverflow.fade,
            softWrap: false,
            textAlign: TextAlign.left,
          ),
          Spacer(),
        ],
      ),

bqucvtff

bqucvtff1#

使用“扩展”小部件环绕文本小部件,并根据需要为它们指定间距

Expanded(
  flex: 1,// if you give them 1 flex value to Expanded widget both they take equal space and if you give one widget 2 flex value and one 1 flex value the one with 2 flex value will get double the space of the other 
  child:Text("Longer text")
)
xwbd5t1u

xwbd5t1u2#

您需要像这样使用RichText

Center(
    child: RichText(
      textAlign: TextAlign.center,
      maxLines: 1,
      overflow: TextOverflow.ellipsis,
      text: TextSpan(
        text: 'smaller text',
        style: TextStyle(
          color: Colors.black.withOpacity(0.85),
        ),
        children: [TextSpan(text: 'Longer text ')],
      ),
    ),
  ),

相关问题