Open In App

Flutter – SliverAppBar Widget

Last Updated : 07 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

SliverAppBar is a Material Design widget in Flutter that gives a scrollable or collapsible app bar. The word Sliver is given to scrollable areas here. It allows us to create an app bar that can change appearance, blend in the background, or even disappear as we scroll. We already had AppBar widget in flutter, which places the app bar at a fixed height. But, looking around us, we can see that the scrollable app bar user interface is widely used. We can see that even the GeeksforGeeks app uses the app bar, which is collapsible. To achieve the same functionality, Flutter gives us the SliverAppBar widget, which is usually taken as a child widget to CustomScrollView (flutter widget), which provides it the power to interact with the scroll. 

Constructor of SliverAppBar Class:

const SliverAppBar(
{
Key key,
Widget leading,
bool automaticallyImplyLeading: true,
Widget title,
List<Widget> actions,
Widget flexibleSpace,
PreferredSizeWidget bottom,
double elevation,
Color shadowColor,
bool forceElevated: false,
Color backgroundColor,
Brightness brightness,
IconThemeData iconTheme,
IconThemeData actionsIconTheme,
TextTheme textTheme,
bool primary: true,
bool centerTitle,
bool excludeHeaderSemantics: false,
double titleSpacing: NavigationToolbar.kMiddleSpacing,
double collapsedHeight,
double expandedHeight,
bool floating: false,
bool pinned: false,
bool snap: false,
bool stretch: false,
double stretchTriggerOffset: 100.0,
Future<void> onStretchTrigger(),
ShapeBorder shape,
double toolbarHeight: kToolbarHeight,
double leadingWidth}
)

Properties of SliverAppBar Widget:

Property

Description

title

This property usually takes in the main widget as a parameter to be displayed in the SliverAppBar.

expandedHeight

Similar to the collapsedHeight property, it also takes a double as a parameter and determines the height at which the SliverAppBar should be fully expanded.

floating

This property takes in boolean as a Boolean parameter and controls the animation related to the visibility of the SliverAppBar. It determines whether the SliverAppBar should be made visible as soon as the user scrolls towards it (top or bottom) or not.

pinned

This property sets whether the SliverAppBar should remain visible at the start of the scroll view. It takes a boolean as a parameter.

flexibleSpace

This property takes in a widget as a parameter and stacks it behind the toolbar when it collapses.

actions

This property takes in a list of widgets as a parameter to be displayed after the title if the SliverAppBar is a row.

leading

This property sets the widget that should be displayed before the title.

bottom

This property takes in PreferredSizeWidget as a parameter. And it determines the widget to be shown across the bottom of the SliverAppBar. It is usually a TabBar similar to the GeeksforGeeks app.

backgroundColor

This property is used to add colors to the background of the SliverAppbar.

foregroundColor

The default color for text and icons within the SliverAppBar.

shadowColor

This property determines the color of the shadow that gets displayed below the SliverAppBar.

elevation

This property is used to set the z-coordinate at which to place this app bar relative to its parent.


Example:

main.dart:

Dart
import 'package:flutter/material.dart';

// Entry point of the application
void main() => runApp(MyApp());

// Main application widget
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    
    // Title for the app bar
    final title = 'GeeksforGeeks'; 

    return MaterialApp(
      home: Scaffold(
        body: CustomScrollView(
          slivers: <Widget>[
            // SliverAppBar provides a flexible app bar
            SliverAppBar(
                
              // App bar does not snap into view
              snap: false, 
              
              // App bar is not pinned at the top
              pinned: false, 
              
              // App bar does not float
              floating: false, 
              flexibleSpace: FlexibleSpaceBar(
                  
                // Center the title in the app bar
                centerTitle: true, 
                title: Text(
                    
                  // Title text
                  "$title", 
                  style: TextStyle(
                      
                    // White text color
                    color: Colors.white, 
                    
                    // Font size
                    fontSize: 16.0, 
                  ),
                ),
                background: Image.network(
                  
                  // Background image
                  "https://i.ibb.co/QpWGK5j/Geeksfor-Geeks.png", 
                  
                  // Cover the entire space
                  fit: BoxFit.cover, 
                ),
              ),
              
              // Height of the expanded app bar
              expandedHeight: 230, 
              
              // Background color
              backgroundColor: Colors.greenAccent[400], 
              leading: IconButton(
                  
                // Menu icon
                icon: Icon(Icons.menu), 
                
                // Tooltip for the menu icon
                tooltip: 'Menu', 
                
                // Action for the menu icon
                onPressed: () {}, 
              ),
              actions: <Widget>[
                IconButton(
                    
                  // Comment icon
                  icon: Icon(Icons.comment), 
                  
                  // Tooltip for the comment icon
                  tooltip: 'Comment Icon', 
                  
                  // Action for the comment icon
                  onPressed: () {}, 
                ),
                IconButton(
                  
                  // Settings icon
                  icon: Icon(Icons.settings), 
                  
                  // Tooltip for the settings icon
                  tooltip: 'Setting Icon', 
                  
                  // Action for the settings icon
                  onPressed: () {}, 
                ),
              ],
            ),
            
            // SliverList displays a scrollable list of items
            SliverList(
              delegate: SliverChildBuilderDelegate(
                (context, index) => ListTile(
                  tileColor: (index % 2 == 0)
                      ? Colors.white
                      : Colors.green[50], // Alternating tile colors
                  title: Center(
                    child: Text(
                      
                      // Display the index as text
                      '$index', 
                      style: TextStyle(
                          
                        // Normal font weight
                        fontWeight: FontWeight.normal, 
                        
                        // Font size
                        fontSize: 50, 
                        
                        // Text color
                        color: Colors.greenAccent[400], 
                      ),
                    ),
                  ),
                ),
                
                // Number of items in the list
                childCount: 51, 
              ),
            ),
          ],
        ),
      ),
      
      // Remove debug banner
      debugShowCheckedModeBanner: false, 
    );
  }
}


Output:

If the properties are defined as follows:

snap: false;
pinned: false;
floating: false;

Output:

If the properties are defined as follows:

snap: false;
pinned: false;
floating: true;

Output:

If the properties are defined as follows:

snap: false;
pinned: true;
floating: false;

Output:
If the properties are defined as follows:

snap: true;
pinned: false;
floating: true;

Output:

If the properties are defined as follows:

snap: true;
pinned: true;
floating: true;


Next Article

Similar Reads