Back to Articles

Intermediate

Loading States in Flutter with a Shimmer Effect

· 6 min read

Loading States in Flutter with a Shimmer Effect

What Is Shimmer / Skeleton, and Why Use It?

Putting a single CircularProgressIndicator in the middle of the screen while a list loads is the easiest solution, but not the best experience: the user can't see the page's final shape until the data arrives, and when it does, the content suddenly 'pops in'. Skeleton screens show a rough outline of the real content (circles, rectangular blocks) up front; the user sees the page's structure immediately and the transition feels much smoother. Shimmer is a light/gradient animation sliding across those skeleton blocks, an extra layer that makes it feel like 'something is loading'.

A bright band sweeping left to right across gray skeleton boxes
A shimmer effect built from scratch with ShaderMask + AnimationController, next to the final loaded content.

The Basic Idea: AnimationController + ShaderMask

You can build the shimmer effect without adding a package. The logic is simple: a continuously repeating AnimationController shifts a LinearGradient's start and end points on every frame, and ShaderMask applies that gradient as a mask on top of the child widget. The result is a bright band sliding left to right over static gray blocks.

class ShimmerLoading extends StatefulWidget {
  const ShimmerLoading({super.key, required this.child});

  final Widget child;

  @override
  State<ShimmerLoading> createState() => _ShimmerLoadingState();
}

class _ShimmerLoadingState extends State<ShimmerLoading>
    with SingleTickerProviderStateMixin {
  late final AnimationController _controller = AnimationController(
    vsync: this,
    duration: const Duration(milliseconds: 1200),
  )..repeat();

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: _controller,
      builder: (context, _) {
        return ShaderMask(
          blendMode: BlendMode.srcATop,
          shaderCallback: (bounds) {
            final dx = _controller.value * 2 - 1;
            return LinearGradient(
              colors: const [
                Color(0xFF2A2A2A),
                Color(0xFF4A4A4A),
                Color(0xFF2A2A2A),
              ],
              stops: const [0.35, 0.5, 0.65],
              begin: Alignment(dx - 1, 0),
              end: Alignment(dx + 1, 0),
            ).createShader(bounds);
          },
          child: widget.child,
        );
      },
    );
  }
}

Building the Skeleton Boxes

ShimmerLoading does nothing on its own; you need to wrap it around plain colored boxes that roughly match the real content. For each row in a user list, drawing flat gray blocks of the same size instead of a round avatar and two lines of text is enough.

Widget skeletonTile() {
  return ShimmerLoading(
    child: Row(
      children: [
        Container(
          width: 48,
          height: 48,
          decoration: const BoxDecoration(
            color: Color(0xFF3A3A3A),
            shape: BoxShape.circle,
          ),
        ),
        const SizedBox(width: 12),
        Expanded(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(height: 14, color: const Color(0xFF3A3A3A)),
              const SizedBox(height: 8),
              Container(
                height: 14,
                width: 120,
                color: const Color(0xFF3A3A3A),
              ),
            ],
          ),
        ),
      ],
    ),
  );
}

Swapping In the Real Content: AnimatedSwitcher

Once the data arrives, AnimatedSwitcher is used to swap the skeleton list for the real list. Since the two states have different widget trees, you need to give them different ValueKeys so Flutter knows they're not the same child; otherwise the transition animation never fires.

AnimatedSwitcher(
  duration: const Duration(milliseconds: 300),
  child: isLoading
      ? ListView.builder(
          key: const ValueKey('skeleton'),
          itemCount: 6,
          itemBuilder: (context, index) => skeletonTile(),
        )
      : ListView.builder(
          key: const ValueKey('content'),
          itemCount: items.length,
          itemBuilder: (context, index) => UserTile(item: items[index]),
        ),
)

Things to Watch Out For

• Always dispose the AnimationController; since shimmer is usually set up per list item, a forgotten controller quickly turns into a leak. • If dozens of shimmer boxes are animating at once, wrapping each one in a RepaintBoundary keeps the repaint cost contained. • Don't make the animation too short (around 900-1500ms feels like motion without being distracting); a too-fast shimmer can be distracting. • For screen readers, it's good practice to mark skeleton blocks with Semantics(excludeSemantics: true) and keep them out of the accessibility tree until the real, meaningful content arrives.

Conclusion

A shimmer effect gives far better perceived performance than a single spinning icon: the user sees the page's structure immediately, and when the data arrives the content takes its place with a smooth transition instead of suddenly popping in. Without adding a package, an AnimationController and a ShaderMask let you control this effect exactly to fit your own design language.