Skip to content

Swift UI Modifier Methods

Matt Carroll edited this page Oct 2, 2023 · 3 revisions

Swift UI chose to embrace an approach to declarative UI that relies upon calling methods on views. These methods are called "modifier methods".

For example, let's say that you'd like some bold text with padding in Swift UI, you'd achieve it with the following:

Text("Bold text")
  .bold()
  .padding([.vertical])

Despite how different this code looks from typical Flutter code, it's purely stylistic. One could create Flutter widgets to do the same thing, as follows:

Padding(
  StandardPadding.vertical,
  child: Bold(
    child: Text("Bold text"),
  ),
);

Does swift_ui use modifier methods?

The swift_ui package does NOT use modifier methods, for multiple reasons.

First, Swift UI built various engineering decisions into the foundation of their toolkit to make it convenient to use this modifier method approach. Flutter has no such foundational artifacts, thus making it much more cumbersome to attempt such a thing. This isn't a problem with Flutter because, as mentioned, these modifier methods don't offer any new capabilities. Flutter chose one style of declarative UI and Swift UI chose another style. Thus, attempting to force the Swift UI paradigm into Flutter would yield significantly more work and various API hacks.

Second, all other Flutter code embraces Flutter's declarative UI paradigm. Imposing the Swift UI declarative UI paradigm upon the swift_ui package would make it more difficult for others to adopt the swift_ui package. Non-iOS developers would be forced to learn the Swift UI paradigm just to use the swift_ui Flutter package.

Therefore, in the interest of minimizing unnecessary work, and embracing the broader Flutter community, the swift_ui package embraces widgets, and rejects modifier methods.

Clone this wiki locally