Coding

Practical performance guidance

  • An example based on AppAvatar.

  • Prefer const where you can: literals like EdgeInsets, SizedBox, static IconData, TextStyle fragments, 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.user is fine in practice.

  • Reuse/canonicalize heavy inputs:

    • If MeshGradientColors can be represented by a small set of presets (e.g., per UserLevel), expose a const lookup:
      • const Map<UserLevel, MeshGradientColors> kGradientByLevel = { ... };
      • gradient: kGradientByLevel[user.level] (this gives you a const instance for equal levels, but the AppAvatar call still won’t be const since it depends on user, yet you reduce allocations of the gradient object).
    • Ensure your image loading uses Flutter’s ImageCache (most ImageProviders do) so repeated avatars don’t refetch/decodes.
  • 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 (or SliverList) for large lists and set cacheExtent appropriately.
  • Use const inside AppAvatar where possible:

    • Any static paddings/spaces/icons in AppAvatar can be const.
    • If your SvgAsset and related classes support const constructors and the size is known at compile time, make those const; otherwise, it’s fine to be non-const.

If you want, I can:

  • Add a const preset table for MeshGradientColors by UserLevel and switch AppAvatar.user to pull from that (reduces allocations).
  • Scan AppAvatar and its subtree to const-qualify literals and style objects safely without changing behavior.