You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Flutter PDF中单行放置多个文本变量

实现Flutter PDF包中多文本元素同行显示

要让多个文本元素在生成的PDF文档里显示在同一行,你可以用pw.Row组件替代原来单独的pw.Center,把所有需要同行的pw.Text组件放到Row的children列表中即可。

修改后的代码示例

pw.Center(
  child: pw.Row(
    // 可根据需求调整文本的对齐方式,比如均匀分布、居中、两端对齐等
    mainAxisAlignment: pw.MainAxisAlignment.spaceEvenly,
    children: [
      pw.Text('Var 1: ' + var1.toString(), style: pw.TextStyle(fontSize: 10, fontWeight: pw.FontWeight.bold)),
      pw.Text('Var 2: ' + var2.toString(), style: pw.TextStyle(fontSize: 10, fontWeight: pw.FontWeight.bold)),
      pw.Text('Var 3: ' + var3.toString(), style: pw.TextStyle(fontSize: 10, fontWeight: pw.FontWeight.bold)),
    ],
  ),
),

额外布局调整建议

  • 如果需要让每个文本元素平分行宽,可以用pw.Expanded包裹每个pw.Text:
pw.Center(
  child: pw.Row(
    children: [
      pw.Expanded(child: pw.Text('Var 1: ' + var1.toString(), style: pw.TextStyle(fontSize: 10, fontWeight: pw.FontWeight.bold))),
      pw.Expanded(child: pw.Text('Var 2: ' + var2.toString(), style: pw.TextStyle(fontSize: 10, fontWeight: pw.FontWeight.bold))),
      pw.Expanded(child: pw.Text('Var 3: ' + var3.toString(), style: pw.TextStyle(fontSize: 10, fontWeight: pw.FontWeight.bold))),
    ],
  ),
),
  • 若想让文本整体靠左/靠右对齐,只需修改mainAxisAlignment为pw.MainAxisAlignment.start或pw.MainAxisAlignment.end即可。

内容的提问来源于stack exchange,提问作者DktPhl2018

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 18:22:08