Aaron
Aaron

Reputation: 1055

How can I enforce certain parameter values for Flutter widgets of a given type?

For instance, I am working on an app with many screens, so I am creating lots of Scaffolds and AppBars.

Say my design spec says that "the appbar title should be centered always, on all platforms", i.e. I want the centerTitle argument to AppBar to always be true.

I could wrap AppBar with a custom widget MyAppBar and always set centerTitle to true, but

  1. This implies a lot of boilerplate to expose all of the other AppBar properties
  2. This requires annoying maintenance to keep up with framework changes to AppBar

Is there an option better than "just try to remember to set it everywhere you create an AppBar"?

Upvotes: 0

Views: 62

Answers (1)

Rémi Rousselet
Rémi Rousselet

Reputation: 277487

Making a custom MyAppBar widget that always center the title is the correct solution.

But the premise that is has a lot of boilerplate/hard to maintain is false.

You don't need all AppBar properties. Only expose those that are relevant to your app.

Upvotes: 2

Related Questions