Flutter ListView添加文本报错:已指定subtitle参数
问题解决:ListTile重复传递subtitle导致报错
报错原因
你在ListTile中重复传递了两次subtitle参数,Flutter的命名参数不允许重复赋值,所以触发报错:
参数'subtitle'已被指定。
尝试移除其中一个命名参数,或者修改其中一个参数名称以引用不同的命名参数。
解决方案
方法1:合并文本到同一个subtitle
如果两个文本样式相近,可用换行符\n合并成一个Text组件:
return ListView.builder( itemCount: problemlist.length, itemBuilder: ((context, index) { problemModel problem = problemlist[index]; return Card( margin: EdgeInsets.all(10), child: ListTile( title: Text( problem.name_surname, style: TextStyle(fontWeight: FontWeight.bold, fontSize: 19), ), // 合并两个文本,用换行分隔 subtitle: Text( '${problem.address}\n${problem.your_second_field}', // 替换成你要显示的第二个字段 style: TextStyle(fontWeight: FontWeight.bold, fontSize: 17), ), trailing: Text( problem.status, style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16), ), ), ); }));
如果需要不同样式,用RichText实现差异化显示:
subtitle: RichText( text: TextSpan( // 默认样式 style: TextStyle(color: Colors.black87), children: [ TextSpan( text: problem.address, style: TextStyle(fontWeight: FontWeight.bold, fontSize: 17), ), TextSpan(text: '\n'), TextSpan( text: problem.your_second_field, style: TextStyle(fontWeight: FontWeight.normal, fontSize: 16), // 自定义第二个文本样式 ), ], ), ),
方法2:自定义ListTile内容
如果需要更灵活的布局,直接在ListTile的title中用Column组织多个文本:
return ListView.builder( itemCount: problemlist.length, itemBuilder: ((context, index) { problemModel problem = problemlist[index]; return Card( margin: EdgeInsets.all(10), child: ListTile( // 用Column作为title,包含主标题和两个子文本 title: Column( crossAxisAlignment: CrossAxisAlignment.start, mainAxisSize: MainAxisSize.min, children: [ Text( problem.name_surname, style: TextStyle(fontWeight: FontWeight.bold, fontSize: 19), ), SizedBox(height: 6), Text( problem.address, style: TextStyle(fontWeight: FontWeight.bold, fontSize: 17), ), SizedBox(height: 6), Text( problem.your_second_field, style: TextStyle(fontWeight: FontWeight.bold, fontSize: 17), ), ], ), trailing: Text( problem.status, style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16), ), ), ); }));
内容的提问来源于stack exchange,提问作者KHL kh
相关产品推荐
相关产品推荐

