skip to Main Content

I need to implement filters at the top of the screen. The idea is that when you scroll down, the filters should disappear, but the title and leading widget should remain visible. Additionally, when scrolling up, the filters should reappear. Does anyone have any suggestions on how to achieve this, what is the best use for this for this feature?

2

Answers


  1. you achive this using ScrollController to detect scroll direction and a combination of SliverAppBar and SliverPersistentHeader for the layout.

    import 'package:flutter/material.dart';
    
    class ScrollableFiltersPage extends StatefulWidget {
      @override
      _ScrollableFiltersPageState createState() => _ScrollableFiltersPageState();
    }
    
    class _ScrollableFiltersPageState extends State<ScrollableFiltersPage> {
      final ScrollController _scrollController = ScrollController();
      bool _showFilters = true;
      double _lastOffset = 0;
    
      @override
      void initState() {
        super.initState();
        _scrollController.addListener(() {
          if (_scrollController.offset > _lastOffset && _showFilters) {
            setState(() => _showFilters = false); // Scrolling down
          } else if (_scrollController.offset < _lastOffset && !_showFilters) {
            setState(() => _showFilters = true); // Scrolling up
          }
          _lastOffset = _scrollController.offset;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: CustomScrollView(
            controller: _scrollController,
            slivers: [
              SliverAppBar(
                pinned: true,
                title: Text('Title'),
                leading: Icon(Icons.menu),
              ),
              SliverPersistentHeader(
                pinned: true,
                delegate: _FiltersDelegate(
                  showFilters: _showFilters,
                  child: Container(
                    color: Colors.blueGrey,
                    height: 50,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: [
                        TextButton(onPressed: () {}, child: Text("Filter 1")),
                        TextButton(onPressed: () {}, child: Text("Filter 2")),
                        TextButton(onPressed: () {}, child: Text("Filter 3")),
                      ],
                    ),
                  ),
                ),
              ),
              SliverList(
                delegate: SliverChildBuilderDelegate(
                  (context, index) => ListTile(title: Text('Item $index')),
                  childCount: 50,
                ),
              ),
            ],
          ),
        );
      }
    }
    
    
    class _FiltersDelegate extends SliverPersistentHeaderDelegate {
      final bool showFilters;
      final Widget child;
    
      _FiltersDelegate({required this.showFilters, required this.child});
    
      @override
      Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) {
        return AnimatedOpacity(
          duration: Duration(milliseconds: 200),
          opacity: showFilters ? 1.0 : 0.0,
          child: child,
        );
      }
    
      @override
      double get maxExtent => 50.0;
      @override
      double get minExtent => 50.0;
      @override
      bool shouldRebuild(covariant _FiltersDelegate oldDelegate) =>
          showFilters != oldDelegate.showFilters || child != oldDelegate.child;
    }
    

    I HOPE THIS IS HELP YOU.
    THANK YOU:)

    Login or Signup to reply.
  2. You can achieve this functionality using the new PinnedHeaderSliver and SliverResizingHeader widgets introduced in Flutter 3.24. These widgets allow you to design dynamic App Bars where headers can float, stay pinned, or resize as the user scrolls.

    Implementation Steps:

    • Use PinnedHeaderSliver: This keeps your title and leading widget always visible at the top.
    • Use SliverResizingHeader: Place your filters inside this widget so they disappear when scrolling down and reappear when scrolling up.

    These new widgets provide a simpler and more flexible interface compared to the existing SliverPersistentHeader and SliverAppBar.

    💡 For a practical example, check out the PinnedHeaderSliver code that recreates the effect of the settings bar in iOS apps.

    References:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search