Can you have swipeable tabs with a transparent app bar and bottom app bar?

Issue

I’m trying to build a flutter layout which has a transparent app bar and bottom app bar. However, I want to be able to swipe between tabs. The issue is I can have swipeable tabs but the body doesn’t sit under the app bars. Or I can have the body underneath the app bars but the tabs are not swipeable.

I have already tried an implementation of using a Stack() and not a Scaffold(). However I think this places the TabBarView in front of the app bars due to the inability to differentiate the body and app bars.

This is the code in which the app bars are not visible:

class _AppStateTab extends State<App>{
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: DefaultTabController(
        length: 3,
        child: Stack(
          children: <Widget>[
            Positioned(
              top: 0.0,
              left: 0.0,
              right: 0.0,
              child: AppBar(
                backgroundColor: Colors.transparent,
                elevation: 0.0,
                actions: <Widget>[
                  IconButton(
                    icon: Icon(Icons.search), 
                    color: Colors.white,
                    onPressed: () {},
                  ),
                  IconButton(
                    icon: Icon(Icons.cached), 
                    color: Colors.white,
                    onPressed: () {},
                  ),
                ],
              ),
            ),
            Positioned(
              bottom: 0.0,
              left: 0.0,
              right: 0.0,
              child: BottomAppBar(
                color: Colors.transparent,
                elevation: 0.0,
                child: Row(
                  mainAxisSize: MainAxisSize.max,
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: <Widget>[
                    IconButton(
                      icon: Icon(Icons.dialpad), 
                      color: Colors.white,
                      onPressed: (){},
                    ),
                    IconButton(
                      icon: Icon(Icons.camera_alt),
                      color: Colors.white,
                      onPressed: (){},
                    ),
                    IconButton(
                      icon: Icon(Icons.edit),
                      color: Colors.white,
                      onPressed: (){},
                    ),
                  ],
                ),
              ),
            ),
            TabBarView(
              children: <Widget>[
                Calculator(),
                Camera(),
                Solution(),
              ],
            )
          ],
        ),
      ),
    );
  }
}

And this is the code in which the body doesn’t travel underneath the app bars:

  @override
  Widget build(BuildContext context){
    return Scaffold(
      appBar: AppBar(
        title: Text('data'),
        backgroundColor: Colors.transparent,
        elevation: 0.0,
      ),
      body: TabBarView(
        controller: _tabController,
        children: _tabList,
      ),
      bottomNavigationBar: BottomAppBar(
        color: Colors.transparent,
        elevation: 0.0,
        child: Row(
          mainAxisSize: MainAxisSize.max,
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: <Widget>[
            IconButton(
              icon: Icon(Icons.dialpad), 
              color: Colors.white,
              onPressed: (){
                _tabController.animateTo(0);
              },
            ),
            IconButton(
              icon: Icon(Icons.camera_alt),
              color: Colors.white,
              onPressed: (){
                _tabController.animateTo(1);
              },
            ),
            IconButton(
              icon: Icon(Icons.edit),
              color: Colors.white,
              onPressed: (){
                _tabController.animateTo(2);
              },
            ),
          ],
        ),
      ),
    );

Solution

Okay just an update, I managed to fix my issue. I didn’t realise the order within a stack determined it’s layer so to speak. The first item is at the very back and each other item goes one layer infront. So to fix this all I did was rearranged the body to be the first item and have the positioneds follow it. This allowed for a swipable app with transparent app bars.

Answered By – kiwikodes

Answer Checked By – Katrina (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *