Performance
An example based on
AppAvatar.Prefer const where you can: literals like
EdgeInsets,SizedBox, staticIconData,TextStylefragments, and any widget subtrees that don’t depend on runtime values.Keep widgets small and immutable: the cost to allocate widgets is tiny compared to layout/paint. Not having const on
AppAvatar.useris fine in practice.Reuse/canonicalize heavy inputs:
- If
MeshGradientColorscan be represented by a small set of presets (e.g., perUserLevel), expose a const lookup:const Map<UserLevel, MeshGradientColors> kGradientByLevel = { ... };gradient: kGradientByLevel[user.level](this gives you a const instance for equal levels, but theAppAvatarcall still won’t be const since it depends onuser, yet you reduce allocations of the gradient object).
- Ensure your image loading uses Flutter’s
ImageCache(mostImageProviders do) so repeated avatars don’t refetch/decodes.
- If
Stabilize lists to reduce rebuild churn:
- Use keys like
ValueKey(user.id)when rendering many avatars in a list to improve element reuse. - Use
ListView.builder(orSliverList) for large lists and setcacheExtentappropriately.
- Use keys like
Use const inside AppAvatar where possible:
- Any static paddings/spaces/icons in
AppAvatarcan beconst. - If your
SvgAssetand related classes support const constructors and the size is known at compile time, make those const; otherwise, it’s fine to be non-const.
- Any static paddings/spaces/icons in
If you want, I can:
- Add a const preset table for
MeshGradientColorsbyUserLeveland switchAppAvatar.userto pull from that (reduces allocations). - Scan
AppAvatarand its subtree to const-qualify literals and style objects safely without changing behavior.