Skip to content
The Chat Archive
gpt-3.5-turbo-0613· OpenAI Arena Human Preference 55K

flutter app how to hide the app bar and bottom navigation bar while scrolling

  1. User

    flutter app how to hide the app bar and bottom navigation bar while scrolling

  2. Assistant

    To hide the app bar and bottom navigation bar while scrolling in a Flutter app, you can make use of the ScrollController and AnimatedContainer widgets.

    Here's an example of how you can achieve this:

    1. First, create a ScrollController instance in your StatefulWidget:
    ScrollController _scrollController = ScrollController();
    
    1. Wrap your Scaffold body with an AnimatedContainer and listen to the scroll events:
    AnimatedContainer(
      duration: Duration(milliseconds: 300),
      height: _isVisible ? kToolbarHeight + 56.0 : 0.0, // Change the height based on your app bar and bottom navigation bar height
      child: Scaffold(
        appBar: AppBar(
          title: Text('Flutter App'),
        ),
        body: ListView.builder(
          controller: _scrollController,
          itemCount: // Your list item count,
          itemBuilder: // Your list item builder,
        ),
        bottomNavigationBar: BottomNavigationBar(
          // Bottom navigation bar details
        ),
      ),
    )
    
    1. Add a listener to the ScrollController to update the visibility of the app bar and bottom navigation bar:
    bool _isVisible = true;
    
    @override
    void initState() {
      super.initState();
      _scrollController.addListener(() {
        if (_scrollController.position.userScrollDirection ==
            ScrollDirection.reverse) {
          setState(() {
            _isVisible = false;
          });
        }
        if (_scrollController.position.userScrollDirection ==
            ScrollDirection.forward) {
          setState(() {
            _isVisible = true;
          });
        }
      });
    }
    
    @override
    void dispose() {
      _scrollController.dispose();
      super.dispose();
    }
    

    With this setup, the app bar and bottom navigation bar will be hidden when the user scrolls down and shown again when the user scrolls up.

    Please note that you may need to adjust the height values based on your specific app bar and bottom navigation bar heights.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.