gpt4 book ai didi

flutter - 如何在RichText中使用

转载 作者:行者123 更新时间:2023-12-03 04:33:29 25 4
gpt4 key购买 nike

我正在尝试在RichText中显示新旧价格。

if(regularPrice >= discountPrice){
TextSpan(
text: ' $regularPrice',
style: TextStyle(
decoration: TextDecoration.lineThrough,
decorationThickness: 2.85,
fontSize: 15,
ontWeight: FontWeight.bold),
),
TextSpan(
text: ' $discountPrice',
style: TextStyle(
fontSize: 15,
ontWeight: FontWeight.bold),
),
}else{
TextSpan(
text: ' $regularPrice',
style: TextStyle(
fontSize: 15,
ontWeight: FontWeight.bold),
),
}
我可以在RichText中使用此逻辑吗?

最佳答案

您可以使用spread operator:

class RichTextDiscountedPrice extends StatelessWidget {
static const regularPrice = 100;
static const discountedPriced = 95;

@override
Widget build(BuildContext context) {
final regularPriceTextSpan = TextSpan(text: ' $discountedPriced', style: TextStyle(fontWeight: FontWeight.bold));

final discountedTextSpanList = [
TextSpan(text: '$regularPrice', style: TextStyle(decoration: TextDecoration.lineThrough)),
regularPriceTextSpan
];

return Center(
child: RichText(
text: TextSpan(
text: "Price ",
style: DefaultTextStyle.of(context).style,
children: <TextSpan>[
if (regularPrice > discountedPriced) ...discountedTextSpanList else ...<TextSpan>[regularPriceTextSpan],
],
),
),
);
}
}
discounted price
regular price
注意:我对样式进行了一些编辑,以减少示例的行数。

关于flutter - 如何在RichText中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64528909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com