← Back to posts Cover image for Optimizing Flutter UI Performance: Solving `ImageFiltered` Lag and Smooth Blurring

Optimizing Flutter UI Performance: Solving `ImageFiltered` Lag and Smooth Blurring

· 5 min read
Weekly Digest

The Flutter news you actually need

No spam, ever. Unsubscribe in one click.

Chris
By Chris

Building a visually polished Flutter app often means reaching for effects like blurs and shadows. The ImageFiltered widget is a go-to for this, allowing you to apply a ui.ImageFilter.blur() to any child widget. It works beautifully for small, static areas. However, many developers hit a jarring performance wall when using it over dynamic or content-rich regions, like a scrolling list or a large block of text. The UI becomes laggy, frames are dropped, and the smooth experience is ruined. Let’s understand why this happens and, more importantly, how to fix it.

Why ImageFiltered Causes Lag

The core issue is one of rendering frequency. When you wrap a widget tree with ImageFiltered, Flutter doesn’t just take a picture of that tree and blur it once. Instead, it must re-process the entire visual output of that subtree on every single frame to apply the Gaussian blur effect.

A blur is a computationally expensive operation. For each pixel in the output, the GPU must sample many surrounding pixels (the sample area is determined by the sigma value). When your blurred region covers a large area—say, 400x800 pixels—you’re asking the GPU to perform millions of these sampling calculations 60 or 120 times per second. If the content underneath is also changing (like text being typed or a list scrolling), there is no respite; it’s a continuous, heavy workload.

In short, ImageFiltered is performing real-time image processing, which is fantastic for small, dynamic effects but scales poorly for large areas.

Solution 1: Isolate and Cache with RepaintBoundary

The first strategy is to tell Flutter: “This content is expensive to blur, so please render it once into an image, then just reuse that image.” We do this by combining RepaintBoundary with ImageFiltered.

A RepaintBoundary creates a distinct layer in the rendering engine. When the content within that boundary is static, Flutter can cache the rasterized result and skip repainting it.

import 'dart:ui' as ui;

class OptimizedBlurDemo extends StatelessWidget {
  const OptimizedBlurDemo({super.key});

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        // Your background content
        const Placeholder(), // Imagine this is a complex widget tree

        // The blurred overlay
        Positioned.fill(
          child: ImageFiltered(
            imageFilter: ui.ImageFilter.blur(sigmaX: 8.0, sigmaY: 8.0),
            child: RepaintBoundary( // <-- The key addition
              child: Container(
                color: Colors.black.withOpacity(0.5),
                child: const Center(
                  child: Text(
                    'Static Overlay Text',
                    style: TextStyle(color: Colors.white, fontSize: 24),
                  ),
                ),
              ),
            ),
          ),
        ),
      ],
    );
  }
}

When to use this: This works well when the content being blurred is static. If the text or widgets inside the RepaintBoundary change, the boundary will be repainted and the blur recalculated, which brings us back to the performance problem.

Solution 2: Manual Snapshot Caching for Dynamic Content

What if the content underneath changes, but not every frame? For example, a modal sheet with blurry background that only updates when the user makes a selection. We need a way to take a snapshot of the background, blur it once, and display that snapshot until the background intentionally changes.

While Flutter doesn’t have a built-in SnapshotWidget in the stable SDK, we can implement a similar concept using a RepaintBoundary and an Image widget.

Here’s a practical example using a GlobalKey to capture the widget tree as an image:

import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

class DynamicBlurBackground extends StatefulWidget {
  const DynamicBlurBackground({super.key});

  @override
  State<DynamicBlurBackground> createState() => _DynamicBlurBackgroundState();
}

class _DynamicBlurBackgroundState extends State<DynamicBlurBackground> {
  final GlobalKey _boundaryKey = GlobalKey();
  ui.Image? _cachedImage;

  Future<void> _capture() async {
    final boundary = _boundaryKey.currentContext?.findRenderObject() as RenderRepaintBoundary?;
    if (boundary == null) return;

    final image = await boundary.toImage();
    setState(() {
      _cachedImage = image;
    });
  }

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) => _capture());
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        // The dynamic content to be blurred (e.g., a changing list)
        RepaintBoundary(
          key: _boundaryKey,
          child: YourDynamicContentWidget(), // Your actual content here
        ),

        // The overlay showing the *cached* image
        if (_cachedImage != null)
          Positioned.fill(
            child: RawImage(
              image: _cachedImage,
              fit: BoxFit.cover,
            ),
          ),
      ],
    );
  }
}

The Crucial Point: The expensive operation (toImage) is triggered manually, not on every frame. You call _capture() only when the background content has meaningfully changed.

Practical Tips for Smooth Blurring

  1. Reduce the Blur Area: Don’t blur an entire screen if you only need a small section. Use a ClipRect or a fixed-size Container to limit the blurred region.
  2. Lower the Sigma: A sigma of 10 creates a much larger sampling radius than a sigma of 4. Use the smallest value that achieves your design goal.
  3. Pre-blur Static Assets: If you’re blurring a background image that never changes in-app, do the blur in an image editor (e.g., with Gaussian Blur in Photoshop) and use the pre-processed asset. This is the ultimate performance win.
  4. Consider Alternatives: For complex, dynamic scenes, sometimes a semi-transparent overlay or a gradient is a more performant way to achieve visual separation than a real-time blur.

Final Thoughts

ImageFiltered is a powerful widget, but it’s not a free lunch. By understanding that it forces per-frame processing, you can make smarter architectural decisions. For static content, RepaintBoundary provides automatic caching. For dynamic content, implement a manual snapshot strategy where you control when the expensive operation occurs. By moving from continuous real-time processing to controlled, one-time operations, you can keep your app’s UI buttery smooth without sacrificing those beautiful visual effects.

This blog is produced with the assistance of AI by a human editor. Learn more

Related Posts

Cover image for Choosing the Right Storage for Flutter: Beyond SharedPreferences for Persistent Data

Choosing the Right Storage for Flutter: Beyond SharedPreferences for Persistent Data

Developers frequently struggle with persistent storage in Flutter, especially when dealing with OS-level backups and app reinstalls. This post will clarify the nuances of different storage solutions like `SharedPreferences`, `path_provider`, and `flutter_secure_storage`, focusing on how to correctly manage data lifetimes (per-install vs. per-user) and avoid issues like device IDs migrating to new phones or data being unexpectedly backed up.

Cover image for Optimizing Flutter UI Performance: Solving `ImageFiltered` Lag and Smooth Blurring

Optimizing Flutter UI Performance: Solving `ImageFiltered` Lag and Smooth Blurring

Flutter's `ImageFiltered` can cause significant lag when blurring large or frequently changing content like text. This post will explain the performance bottlenecks, provide solutions like caching blurred outputs with `RepaintBoundary` or `SnapshotWidget`, and offer practical tips to ensure smooth UI effects without sacrificing frame rates.