Search code examples
flutterfloating-action-buttonflutter-bottomnavigation

A hole in bottom nav bar with a fab inside it - flutter


With flutter, how can I create a hole inside the bottom nav bar and put the fab inside it, something like the image below

I have tried wrapping the fab with padding, increase the top padding to move it down and increase the notch margin but the bottom nav bar did not look like I want.

FAb code:

Padding(
    padding: const EdgeInsets.only(top: 90),
    child: FloatingActionButton(...)
)

bottom nav bar code:

BottomAppBar(
    elevation: 0,
    color: Colors.transparent,
    shape: const CircularNotchedRectangle(),
    clipBehavior: Clip.antiAlias,
    notchMargin: 50.0,
    child: SizedBox(...)
)

Here is how I want it to look like

enter image description here


Solution

  • I am using Stack to draw on UI. play with widget's size, position, and color.

    enter image description here

    Run on dartPad

    bottomNavigationBar: LayoutBuilder(
      builder: (context, constraints) => SizedBox(
        width: constraints.maxWidth,
        height: kToolbarHeight,
        child: Stack(
          children: [
            Positioned.fill(
              child: Container(
                color: Colors.white,
              ),
            ),
    
            /// middle fab
            Positioned(
              bottom: -20,
              top: -20,
              left: 0,
              right: 0,
              child: Container(
                height: kToolbarHeight * 1.5,
                width: kToolbarHeight * 1.5,
                decoration: BoxDecoration(
                  color: Theme.of(context).scaffoldBackgroundColor,
                  // Colors.deepPurple,
    
                  shape: BoxShape.circle,
                ),
              ),
            ),
    
            Align(
              alignment: Alignment.center,
              child: Container(
                width: kToolbarHeight * .8,
                height: kToolbarHeight * .8,
                alignment: Alignment.center,
                decoration: const BoxDecoration(
                  color: Colors.amber,
                  shape: BoxShape.circle,
                ),
                child: const Text("A"),
              ),
            )
          ],
        ),
      ),
    ),