Hide Appbar on Scroll Flutter?

后端 未结 4 803
不知归路
不知归路 2020-12-07 17:21

Consider this image. As you can see it has an appbar and the appbar has Tabbed buttons. Am trying to animate the appbar so that it hides on scrollup and leaves only

4条回答
  •  情深已故
    2020-12-07 17:33

    import 'package:flutter/material.dart';
    import 'package:flutter/rendering.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: MyHomePage(),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State
        with SingleTickerProviderStateMixin {
      bool _showAppbar = true;
      ScrollController _scrollBottomBarController = new ScrollController();
      bool isScrollingDown = false;
      bool _show = true;
      double bottomBarHeight = 75;
      double _bottomBarOffset = 0;
    
      @override
      void initState() {
        super.initState();
        myScroll();    
      }
    
      @override
      void dispose() {
        super.dispose();
        _scrollBottomBarController.removeListener(() {});
        super.dispose();
      }
    
      void showBottomBar() {
        setState(() {
          _show = true;
        });
      }
    
      void hideBottomBar() {
        setState(() {
          _show = false;
        });
      }
    
      void myScroll() async {
        _scrollBottomBarController.addListener(() {
          if (_scrollBottomBarController.position.userScrollDirection ==
              ScrollDirection.reverse) {
            if (!isScrollingDown) {
              isScrollingDown = true;
              _showAppbar = false;
              hideBottomBar();
            }
          }
          if (_scrollBottomBarController.position.userScrollDirection ==
              ScrollDirection.forward) {
            if (isScrollingDown) {
              isScrollingDown = false;
              _showAppbar = true;
              showBottomBar();
            }
          }
        });
      }
    
      Widget containterContent(){
        return Container(
          height: 50.0,
          color: Colors.cyanAccent,
          margin: EdgeInsets.all(8.0),
          width: MediaQuery.of(context).size.width - 100,
          child: Center(child: Text('Item 1',
            style: TextStyle(
              fontSize: 14.0,
            ),)),
        );
      }
    
      Widget body() {
        return ListView(
          controller: _scrollBottomBarController,
          children: [
            containterContent(),
            containterContent(),
            containterContent(),
            containterContent(),
            containterContent(),
            containterContent(),
            containterContent(),
            containterContent(),
            containterContent(),
            containterContent(),
    
          ],
        );
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: _showAppbar
              ? AppBar(
            title: Text('My Tasks'),
          )
              : PreferredSize(
            child: Container(),
            preferredSize: Size(0.0, 0.0),
          ),
          bottomNavigationBar: Container(
            height: bottomBarHeight,
            width: MediaQuery.of(context).size.width,
            child: _show
                ?BottomNavigationBar(
              currentIndex: 0, // this will be set when a new tab is tapped
              items: [
                BottomNavigationBarItem(
                  icon: new Icon(Icons.home),
                  title: new Text('Home'),
                ),
                BottomNavigationBarItem(
                  icon: new Icon(Icons.mail),
                  title: new Text('Messages'),
                ),
                BottomNavigationBarItem(
                    icon: Icon(Icons.person), title: Text('Profile'))
              ],
            )
                : Container(
              color: Colors.white,
              width: MediaQuery.of(context).size.width,
            ),
          ),
    
          body: body(
          ),
    
        );
      }
    }
    

提交回复
热议问题