Skip to content
Blog

Mastering CustomScrollView and SliverGrid for Dynamic Grid Layouts in Flutter

Learn how to compose complex scrollable views in Flutter combining headers, lists, and dynamic grids using CustomScrollView and SliverGrid.

Published on • November 9, 2026

AI Assistant

Standard GridView or ListView widgets work well for uniform lists, but real-world dashboards, media feeds, and e-commerce applications require combining headers, search bars, horizontal carousels, and grid layouts within a single unified scroll view.

CustomScrollView powered by slivers—such as SliverGrid and SliverList—provides lazy rendering and smooth 60fps/120fps scrolling across heterogeneous UI sections.

Understanding Slivers

A sliver is a slice of a scrollable area that renders its contents lazily as they enter the viewport. Unlike standard box widgets, slivers communicate directly with the RenderViewport protocol to optimize memory usage and GPU draw calls.

Anatomy of a CustomScrollView with SliverGrid

import 'package:flutter/material.dart';

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: CustomScrollView(
        slivers: [
          // 1. Collapsible AppBar
          const SliverAppBar(
            expandedHeight: 180.0,
            floating: false,
            pinned: true,
            flexibleSpace: FlexibleSpaceBar(
              title: Text('Media Catalog'),
              background: Image(
                image: NetworkImage('https://picsum.photos/800/400'),
                fit: BoxFit.cover,
              ),
            ),
          ),

          // 2. Section Header
          const SliverToBoxAdapter(
            child: Padding(
              padding: EdgeInsets.all(16.0),
              child: Text(
                'Featured Products',
                style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
              ),
            ),
          ),

          // 3. Dynamic Lazy Grid
          SliverPadding(
            padding: const EdgeInsets.symmetric(horizontal: 16.0),
            sliver: SliverGrid(
              gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 2,
                mainAxisSpacing: 12.0,
                crossAxisSpacing: 12.0,
                childAspectRatio: 0.85,
              ),
              delegate: SliverChildBuilderDelegate(
                (BuildContext context, int index) {
                  return Card(
                    elevation: 2,
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Icon(Icons.image, size: 48, color: Colors.indigo.shade300),
                        const SizedBox(height: 8),
                        Text('Catalog Item #$index'),
                      ],
                    ),
                  );
                },
                childCount: 20, // Rendered lazily on demand
              ),
            ),
          ),

          // 4. Footer Section
          const SliverToBoxAdapter(
            child: SizedBox(height: 40),
          ),
        ],
      ),
    );
  }
}

SliverGrid Delegates: Fixed vs MaxExtent

SliverGrid requires two delegates:

  1. SliverGridDelegate: Determines spatial layout.
    • SliverGridDelegateWithFixedCrossAxisCount: Defines a fixed number of columns (e.g. 2 columns on mobile, 4 on desktop).
    • SliverGridDelegateWithMaxCrossAxisTile: Sets a maximum item width (e.g., maxCrossAxisExtent: 200), enabling responsive column count automatically based on available width.
SliverGridDelegateWithMaxCrossAxisExtent(
  maxCrossAxisExtent: 220.0,
  mainAxisSpacing: 16.0,
  crossAxisSpacing: 16.0,
  childAspectRatio: 0.75,
)
  1. SliverChildDelegate: Generates child widgets.
    • SliverChildBuilderDelegate: Lazily instantiates widgets as they scroll into view (essential for long lists).
    • SliverChildListDelegate: Accepts a pre-built static list of widgets.

Inserting Standard Widgets into Slivers

Standard layout widgets (like Container, Card, or Padding) cannot be passed directly into the slivers list of a CustomScrollView. Wrap any standard box widget inside SliverToBoxAdapter to integrate it into the sliver scroll sequence.

Takeaway: CustomScrollView and SliverGrid eliminate jank by lazily rendering mixed content types within a unified, high-performance scroll view.