Skip to main content

Command Palette

Search for a command to run...

SliverList widget and Attributes

Published
2 min read
SliverList widget and Attributes
V

"Hello World, I'm Vinit Mepani, a coding virtuoso driven by passion, fueled by curiosity, and always poised to conquer challenges. Picture me as a digital explorer, navigating through the vast realms of code, forever in pursuit of innovation.

In the enchanting kingdom of algorithms and syntax, I wield my keyboard as a magical wand, casting spells of logic and crafting solutions to digital enigmas. With each line of code, I embark on an odyssey of learning, embracing the ever-evolving landscape of technology.

Eager to decode the secrets of the programming universe, I see challenges not as obstacles but as thrilling quests, opportunities to push boundaries and uncover new dimensions in the realm of possibilities.

In this symphony of zeros and ones, I am Vinit Mepani, a coder by passion, an adventurer in the digital wilderness, and a seeker of knowledge in the enchanting world of code. Join me on this quest, and let's create digital wonders together!"

The SliverList widget in Flutter is used to create a scrollable list of children within a CustomScrollView. It's typically used in scenarios where you need to create a list of items that can be scrolled infinitely or when you want to customize the scroll behavior.

Attributes:

  1. delegate (SliverChildDelegate):

    • The delegate that controls the list of children. It can be either a SliverChildBuilderDelegate or a SliverChildListDelegate.
  2. semanticIndexCallback (IndexedSemanticsCallback):

    • An optional callback that provides the semantic index for each child in the list. This is useful for accessibility purposes.
  3. semanticIndexOffset (int):

    • An optional offset to be added to the semantic indexes provided by the semanticIndexCallback. This can be used to ensure unique semantic indexes across multiple SliverList widgets.
  4. itemExtent (double):

    • An optional fixed extent for each item in the list. If specified, all items will have the same height, and the scrolling performance may be improved.

Example:

import 'package:flutter/material.dart';

class SliverListExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('SliverList Widget Example'),
      ),
      body: CustomScrollView(
        slivers: [
          SliverAppBar(
            title: Text('SliverList Example'),
            floating: true,
            expandedHeight: 200,
            flexibleSpace: FlexibleSpaceBar(
              background: Image.network(
                'https://via.placeholder.com/600x200',
                fit: BoxFit.cover,
              ),
            ),
          ),
          SliverList(
            delegate: SliverChildBuilderDelegate(
              (BuildContext context, int index) {
                return ListTile(
                  leading: Icon(Icons.circle),
                  title: Text('Item $index'),
                );
              },
              childCount: 50,
            ),
          ),
        ],
      ),
    );
  }
}

void main() {
  runApp(MaterialApp(
    home: SliverListExample(),
  ));
}

Explanation:

  • In this example, a SliverList widget is used to create a scrollable list of items within a CustomScrollView.

  • The SliverList widget is added as a sliver within the CustomScrollView.

  • The delegate attribute of the SliverList is set to a SliverChildBuilderDelegate, which generates list items dynamically based on a builder function.

  • The builder function within the SliverChildBuilderDelegate creates a ListTile for each item in the list with a leading icon and a title.

  • The childCount attribute of the SliverChildBuilderDelegate specifies the total number of items in the list.

  • As a result, you'll see a scrollable list of 50 items with leading icons and titles when you run this code.

More from this blog

Vinit Mepani (Flutter Developer)

270 posts

"Vinit Mepani, passionate coder! Dive into my Dart and Flutter journey on the blog. Let's master these tech wonders together. Happy coding! 🚀"