Text versus TextPainter
This is the story of an issue I discovered while using Flutter, a cross-platform framework for building apps that can run on iOS, Android, macOS, Windows, and the web. (As an aside: it’s pretty good! I have been using it to implement cross-platform iOS and Android apps for a few years now. I don’t have any major complaints.)
The most prominent way to add strings of text to a Flutter app is by using the Text widget. This is the way you want to go, about 95 percent of the time. But there are occasionally situations in which a Text widget will not get the job done. For example, say you are building a graphical image by drawing pixels on top of a Canvas object, and you also want to add some text in there. In that case, TextPainter is the right tool for the job. It operates in much the same way as Text does, but it is intended for drawing on a Canvas, rather than being added to a widget tree, which is what Text objects are for.
Flutter has adopted the React widget model, as opposed to the “old-fashioned” declarative paradigm, used by many other GUI frameworks. I put “old-fashioned” in scare quotes there, because this is a decision that I very much disagree with. One of my few complaints about Flutter is the React widget model. This causes me no end of grief. But there is no major piece of tech infrastructure of this scale that is going to incorporate nothing but decisions I agree with. So I have made my peace with the React widget model. (Grudgingly. A lot of the Flutter code I write is to put a nice, declarative layer on top, implemented with Flutter's React-style widgets under the hood. But I digress.)