flutter 中的 TextStyle.merge 和 TextStyle.copyWith 有什么区别?

问题描述 投票:0回答:1

TextStyle.merge 和 TextStyle.copyWith 有不同的描述,但我似乎得到了相同的结果。 如果 merge 的行为与 copyWith 相同,则无需在 TextStyle 中添加 merge 方法。它们之间有什么区别吗?


class TestView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    const baseStyle = TextStyle(color: Colors.blue, fontSize: 16);
    final titleStyle = baseStyle.copyWith(
      color: Colors.red,
      decoration: TextDecoration.lineThrough,
    ); // copyWith
    final noticeStyle = baseStyle.merge(const TextStyle(
      color: Colors.grey,
      decoration: TextDecoration.lineThrough,
    )); // merge
    return Column(
      children: [
        Text('this is title style', style: titleStyle),
        Text('this is notice style', style: noticeStyle),
      ],
    );
  }
}
flutter
1个回答
0
投票

merge
最终会调用
copyWith
,所以如果
other != null && other.inherit
,它们没有区别

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.