如何在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
相关产品推荐
相关产品推荐

