flame
flame copied to clipboard
DocumentStyle.copyWith resets the properties that the other style doesn't set
What happened?
DocumentStyle.copyWith doesn't keep the properties of the original style that the other style leaves unset. Instead they are reset:
- Text and block styles go back to their defaults.
copyWithmergesother.text,other.boldText,other.paragraph,other.header1and so on, but those getters already have the defaults merged in. So the other style's defaults override whatever the original set. ADocumentStyle(text: InlineTextStyle(fontSize: 8))copied withDocumentStyle(width: 100)ends up with a font size of 16, and a paragraph margin of 3 becomes 6. paddinggoes back to zero, since it is always taken fromother.customStylesare dropped, since they aren't passed to the new style at all.- The background border loses its width and radius.
BackgroundStyle.copyWithalways takesborderWidthandborderRadiusfromother, so merging in a background that only changes the fill color leaves a border with width 0 and radius 0.
What do you expect?
As the copyWith docs say: the properties of other are used where they are set, and the properties of the original style are kept everywhere else.
How can we reproduce this?
final style = DocumentStyle(
padding: const EdgeInsets.all(16),
text: InlineTextStyle(fontSize: 8),
paragraph: const BlockStyle(margin: EdgeInsets.all(3)),
customStyles: {'author': InlineTextStyle(fontSize: 10)},
background: BackgroundStyle(
color: const Color(0xFF00FF00),
borderColor: const Color(0xFFFF0000),
borderWidth: 2,
borderRadius: 5,
),
);
final copy = style.copyWith(
DocumentStyle(
width: 100,
background: BackgroundStyle(color: const Color(0xFF0000FF)),
),
);
print(copy.text.fontSize); // 16.0, expected 8
print(copy.paragraph.margin); // EdgeInsets.all(6.0), expected EdgeInsets.all(3.0)
print(copy.padding); // EdgeInsets.zero, expected EdgeInsets.all(16.0)
print(copy.getCustomStyle('author')); // null
print(copy.background!.borderWidths); // EdgeInsets.zero, expected EdgeInsets.all(2.0)
print(copy.background!.borderRadius); // 0.0, expected 5.0
What steps should take to fix this?
Keep the styles as they were passed to the constructor (before the defaults are merged in), and merge those from other in copyWith. Same for padding, borderWidth and borderRadius, which need to be nullable internally so that "not set" can be told apart from zero. Merge customStyles by class name.
Affected platforms
All
Other information
Flame main at ce28876 (flame 2.0.0-dev.1), Flutter 3.47.9 stable.
Are you interested in working on a PR for this?
- [x] I want to work on this
Fix in #4230