Flutter TabBar and SliverAppBar that hides when you scroll down

后端 未结 3 976
-上瘾入骨i
-上瘾入骨i 2020-12-25 14:53

I am trying to create an app with a top application bar and a tab bar below. When you scroll down, the bar should hide by moving off the screen (but tabs should stay), and w

3条回答
  •  忘掉有多难
    2020-12-25 15:16

    I was able to make the floating Appbar with Tabbar similar to that of WhatsApp by using SliverAppbar with NestedScrollView.

    Do add floatHeaderSlivers: true, in NestedScrollView and

    pinned: true, floating: true, in SliverAppBar

    Link to working code sample

    import 'dart:math';
    import 'package:flutter/material.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
            visualDensity: VisualDensity.adaptivePlatformDensity,
          ),
          home: CustomSliverAppbar(),
        );
      }
    }
    
    class CustomSliverAppbar extends StatefulWidget {
      @override
      _CustomSliverAppbarState createState() => _CustomSliverAppbarState();
    }
    
    class _CustomSliverAppbarState extends State
        with SingleTickerProviderStateMixin {
      TabController _tabController;
    
      @override
      void initState() {
        _tabController = TabController(
          initialIndex: 0,
          length: 2,
          vsync: this,
        );
        super.initState();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: NestedScrollView(
            floatHeaderSlivers: true,
            headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
              return [
                SliverAppBar(
                  title: Text(
                    "WhatsApp type sliver appbar",
                  ),
                  centerTitle: true,
                  pinned: true,
                  floating: true,
                  bottom: TabBar(
                      indicatorColor: Colors.black,
                      labelPadding: const EdgeInsets.only(
                        bottom: 16,
                      ),
                      controller: _tabController,
                      tabs: [
                        Text("TAB A"),
                        Text("TAB B"),
                      ]),
                ),
              ];
            },
            body: TabBarView(
              controller: _tabController,
              children: [
                TabA(),
                const Center(
                  child: Text('Display Tab 2',
                      style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold)),
                ),
              ],
            ),
          ),
        );
      }
    
      @override
      void dispose() {
        _tabController.dispose();
        super.dispose();
      }
    }
    
    class TabA extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scrollbar(
          child: ListView.separated(
            separatorBuilder: (context, child) => Divider(
              height: 1,
            ),
            padding: EdgeInsets.all(0.0),
            itemCount: 30,
            itemBuilder: (context, i) {
              return Container(
                height: 100,
                width: double.infinity,
                color: Colors.primaries[Random().nextInt(Colors.primaries.length)],
              );
            },
          ),
        );
      }
    }
    

提交回复
热议问题