cover-img

Exploring the Versatility of SliverAppBar in Flutter

A Flexible Navigation Solution for Scrollable Views

17 June, 2023

0

0

0

Introduction:

In the world of Flutter, building beautiful and responsive user interfaces is made easier with a wide range of widgets. One such powerful widget is the SliverAppBar, which offers a flexible and dynamic navigation solution for scrollable views. In this blog, we will delve into the capabilities of SliverAppBar and explore how it can enhance the user experience in Flutter applications.

Understanding SliverAppBar

A SliverAppBar is a specialized version of the AppBar widget that can automatically expand or collapse based on the user's scrolling actions within a scrollable view. It is primarily designed to work seamlessly with slivers, which are widgets that support scrollable areas. By incorporating a SliverAppBar into your Flutter app, you can create an immersive and interactive header section that adapts to the user's scrolling behavior.

How to Use SliverAppBar

To utilize the features of SliverAppBar, you need to wrap it within a CustomScrollView widget. Here's an example to illustrate its usage 👇

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:liquid_swipe/liquid_swipe.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'SliverAppBarDemo',
      debugShowCheckedModeBanner: false,
      home: SliverAppBarDemo(),
    );
  }
}

class SliverAppBarDemo extends StatefulWidget {
  const SliverAppBarDemo({super.key});

  @override
  State<SliverAppBarDemo> createState() => _SliverAppBarDemoState();
}

class _SliverAppBarDemoState extends State<SliverAppBarDemo> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('SilverAppBar Demo'),
      ),
      body: CustomScrollView(
        slivers: <Widget>[
          SliverAppBar(
            expandedHeight: 250.0,
            flexibleSpace: FlexibleSpaceBar(
              title: Text('Goa', textScaleFactor: 1),
              background: Image.asset(
                'assets/dog.png',
                fit: BoxFit.fill,
              ),
            ),
          ),
          SliverList(
            delegate: SliverChildBuilderDelegate(
              (_, int index) {
                return ListTile(
                  leading: Container(
                    padding: EdgeInsets.all(8),
                    width: 100,
                    child: const Placeholder(),
                  ),
                  title: Text('Place ${index + 1}', textScaleFactor: 2),
                );
              },
              childCount: 20,
            ),
          ),
        ],
      ),
    );
  }
}

The above code demonstrates the implementation of a SliverAppBar in Flutter. The main function sets up the app and runs the MyApp widget. The MyApp widget is a stateless widget that returns a MaterialApp with the title and home set to SliverAppBarDemo.

The SliverAppBarDemo widget is a stateful widget that extends StatefulWidget. It returns a Scaffold with an AppBar and a CustomScrollView as the body. Inside the CustomScrollView, there are two slivers.

The first sliver is the SliverAppBar itself, which has an expanded height of 250.0 and a FlexibleSpaceBar as its flexible space. The FlexibleSpaceBar contains a title widget displaying 'Goa' and an image of a dog that fills the space.

The second sliver is a SliverList, which is populated with ListTile widgets using SliverChildBuilderDelegate. The delegate constructs a ListTile for each index, displaying the title as 'Place' followed by the index number.

Overall, this code creates a scrollable view with a SliverAppBar at the top, featuring an image and a title. Below the SliverAppBar, there is a list of place names displayed using ListTile widgets.

Output

Here's the output of the above code 👇

Conclusion:

The SliverAppBar widget in Flutter provides a versatile and interactive navigation solution for scrollable views. Its ability to adapt to the user's scrolling behavior allows for a seamless user experience, making it an essential component for building modern and engaging applications. With its customization options and integration with other sliver widgets, the SliverAppBar opens up a realm of possibilities for creating immersive and visually appealing UIs in Flutter.

So, embrace the power of SliverAppBar and unleash your creativity to develop captivating user interfaces in your next Flutter project!

If you have any further doubts related to Flutter and DevOps, Feel free to reach out on my Twitter and Showwcase Handle.

Till then, Happy Fluttering 😃


flutter

sliverappbar

dynamicui

smoothtransitions

0

0

0

flutter

sliverappbar

dynamicui

smoothtransitions

Hasnain Makada
Elite @Showwcase | Prev: CCO @ShowwcaseHQ | Building out OSWH 👨‍💻 | MLSA - β | DevOps and Flutter 💙 | Blogger at Hashnode and Showwcase 📑

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2024. Showcase Creators Inc. All rights reserved.