Skip to main content

Command Palette

Search for a command to run...

SliverAppBar widget and Attributes

Updated
2 min read
SliverAppBar 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 SliverAppBar widget in Flutter provides a flexible app bar that integrates with CustomScrollView. It's commonly used in scrollable views where the app bar should expand or contract as the user scrolls. The SliverAppBar allows for smooth and customizable transitions between different states.

Here are the primary attributes of the SliverAppBar widget along with examples:

  1. expandedHeight (double):

    • The maximum height of the app bar when fully expanded.

      Example:

        SliverAppBar(
          expandedHeight: 200.0,
          // other attributes...
        )
      
  2. flexibleSpace (Widget):

    • The widget to display within the flexible space of the app bar.

      Example:

        SliverAppBar(
          flexibleSpace: Image.network('https://example.com/image.jpg', fit: BoxFit.cover),
          // other attributes...
        )
      
  3. floating (bool):

    • Whether the app bar should become a floating app bar when the user scrolls down.

      Example:

        SliverAppBar(
          floating: true,
          // other attributes...
        )
      
  4. pinned (bool):

    • Whether the app bar should remain pinned at the top when the user scrolls.

      Example:

        SliverAppBar(
          pinned: true,
          // other attributes...
        )
      
  5. snap (bool):

    • Whether the app bar should snap into view when the user scrolls up, only if floating is true.

      Example:

        SliverAppBar(
          floating: true,
          snap: true,
          // other attributes...
        )
      
  6. stretch (bool):

    • Whether the app bar should stretch and contract as the user scrolls.

      Example:

        SliverAppBar(
          stretch: true,
          // other attributes...
        )
      
  7. stretchConfigurations (List<StretchConfiguration>):

    • A list of configurations to customize the stretch behavior.

      Example:

        dartCopy codeSliverAppBar(
          stretch: true,
          stretchConfigurations: [
            StretchConfiguration(parallaxFactor: 0.5),
            StretchConfiguration(parallaxFactor: 0.3),
          ],
          // other attributes...
        )
      

Full Example:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: CustomScrollView(
          slivers: [
            SliverAppBar(
              expandedHeight: 200.0,
              floating: false,
              pinned: true,
              flexibleSpace: FlexibleSpaceBar(
                title: Text('SliverAppBar Example'),
                background: Image.network(
                  'https://buffer.com/cdn-cgi/image/w=1000,fit=contain,q=90,f=auto/library/content/images/size/w600/2023/10/free-images.jpg',
                  fit: BoxFit.cover,
                ),
              ),
            ),
            SliverList(
              delegate: SliverChildBuilderDelegate(
                (BuildContext context, int index) {
                  return ListTile(
                    title: Text('List item $index'),
                  );
                },
                childCount: 50,
              ),
            ),
          ],
        ),
      ),
    );
  }
}

This example demonstrates a simple Flutter application with a CustomScrollView containing a SliverAppBar and a SliverList. The SliverAppBar includes an expanded height, a flexible space with an image, and is set to remain pinned at the top while scrolling through a list of items. Adjust the attributes according to your specific design requirements.

Learn Flutter

Part 1 of 50

Explore Flutter's magic in crafting cross-platform apps effortlessly. Join the adventure!

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! 🚀"