DEV Community

Van Anh Pham
Van Anh Pham

Posted on

Unveiling the Power of Flutter's New Sliver API

Understanding Slivers: A Brief Overview

Before delving into the intricacies of Flutter's latest update, let's grasp the concept of slivers. In Flutter, a sliver represents a portion of a scrollable area with specialized behavior. Think of slivers as a detailed tool to precisely control scrolling effects, offering a lower-level interface for fine-grained control over scrollable areas.

All your favorite scrollable views, such as ListView and GridView, are essentially implemented using slivers. Their ability to lazily build each item as it scrolls into view makes slivers particularly efficient for navigating through extensive lists of children.

Introducing SliverMainAxisGroup and SliverCrossAxisGroup
The cornerstone of the new Sliver API lies in two innovative widgets: SliverMainAxisGroup and SliverCrossAxisGroup. These widgets address the long-standing challenge of grouping slivers in a CustomScrollView.

SliverMainAxisGroup

The SliverMainAxisGroup widget allows you to group sliver children sequentially along the main axis. This is achieved by encapsulating various slivers within a SliverMainAxisGroup. Take a look at the code snippet below to understand how this works:

CustomScrollView(
slivers: [
SliverMainAxisGroup(slivers: [
// Slivers grouped along the main axis
SliverPersistentHeader(
delegate: HeaderDelegate(title: 'SliverList'),
pinned: true,
),
SliverPadding(
padding: EdgeInsets.all(16),
sliver: decoratedSliverList(),
),
]),
// Additional SliverMainAxisGroup for different sliver content
],
),

In this example, SliverMainAxisGroup organizes slivers sequentially, including a SliverPersistentHeader for creating sticky headers. The pinned: true property ensures that headers remain fixed at the top during scrolling.

SliverCrossAxisGroup

The SliverCrossAxisGroup widget, on the other hand, arranges a list of slivers along the cross-axis.

SliverCrossAxisGroup(slivers: [
SliverPadding(
padding: const EdgeInsets.all(16),
sliver: decoratedSliverList(),
),
// Additional SliverPadding entries or other slivers
]),

This widget efficiently organizes slivers in a linear array along the cross-axis, similar to how rows handle their children.

Top comments (0)