skip to Main Content

I have question – I want to create bottomNavigationBar but in some custom way.
Please take a look on this image:
navImage from Desing

Im begginer so sorry for a question if its a stupid question/
I try to align every item to the bottom but now I don’t have any idea how to do it…

import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:streeti/static/static_colors.dart';

class CustomBottomNavigationBar extends StatefulWidget {
  const CustomBottomNavigationBar({super.key});

  @override
  State<CustomBottomNavigationBar> createState() =>
      _CustomBottomNavigationBarState();
}

class _CustomBottomNavigationBarState extends State<CustomBottomNavigationBar> {
  @override
  Widget build(BuildContext context) {
    return Container(
      height: 108,
      width: double.infinity,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(15),
          topRight: Radius.circular(15),
        ),
        border: Border.all(
          color: AppColors.blueColor,
        ),
      ),
      child: BottomNavigationBar(
        showUnselectedLabels: true,
        selectedFontSize: 10,
        unselectedLabelStyle:
            const TextStyle(color: Colors.black, fontSize: 10),
        backgroundColor: Colors.transparent,
        fixedColor: Colors.black,
        unselectedItemColor: Colors.black,
        items: [
          BottomNavigationBarItem(
            icon: Padding(
              padding: EdgeInsets.only(bottom: 5),
              child: SvgPicture.asset('lib/assets/images/email2.svg'),
            ),
            label: 'Nachricht',
          ),
          BottomNavigationBarItem(
            icon: Padding(
              padding: EdgeInsets.only(bottom: 5),
              child: SvgPicture.asset('lib/assets/images/map.svg'),
            ),
            label: 'Karte',
          ),
          BottomNavigationBarItem(
            icon: Padding(
              padding: EdgeInsets.only(bottom: 5),
              child: SvgPicture.asset('lib/assets/images/hot.svg'),
            ),
            label: 'Angebote',
          ),
          BottomNavigationBarItem(
            icon: Padding(
              padding: EdgeInsets.only(bottom: 5),
              child: SvgPicture.asset('lib/assets/images/community.svg'),
            ),
            label: 'Community',
          ),
          BottomNavigationBarItem(
            icon: Padding(
              padding: EdgeInsets.only(bottom: 10),
              child: SvgPicture.asset('lib/assets/images/profil.svg'),
            ),
            label: 'Profil',
          ),
        ],
      ),
    );
  }
}

To adjust the Container so that the middle icon shows slightly above the Container as I understand it, should I use Stack()?

My navigations looks like this right now:
My nav - right now

2

Answers


  1. Sometimes the predefined widgets are limited, like the BottomNavigationBar, but you are using Flutter, so you can customize and create your own Widgets 😀 .

    I created an example for you, I’m using IndexedStack to control the view pages, and a simple Container and Stack to create the navigation bottom bar (you can extract this in your own widget).

    Results:

    enter image description here

    Code:

    class MyNewPage extends StatefulWidget {
     const MyNewPage({super.key});
    
     @override
     State<MyNewPage> createState() => _MyNewPageState();
    }
    
    class _MyNewPageState extends State<MyNewPage> {
     int _currentPage = 0;
     final int _maxPages = 5;
    
     Widget _buildPage(String text) {
       return Center(
         child: Text(
           text,
           style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 60),
         ),
       );
     }
    
     Widget _buildTab(
       String text,
       int index, {
       bool isCenter = false,
     }) {
       return InkWell(
         onTap: () {
           setState(() {
             _currentPage = index;
           });
         },
         child: Column(
           mainAxisSize: MainAxisSize.min,
           mainAxisAlignment: MainAxisAlignment.center,
           children: [
             isCenter
                 ? const Icon(
                     Icons.apartment,
                     size: 50,
                   )
                 : const Icon(Icons.home),
             Text(text),
           ],
         ),
       );
     }
    
     @override
     Widget build(BuildContext context) {
       return Scaffold(
         body: IndexedStack(
           index: _currentPage,
           children: List.generate(
             _maxPages,
             (index) => _buildPage('Page: $index'),
           ),
         ),
         bottomNavigationBar: SafeArea(
           child: Padding(
             padding: const EdgeInsets.symmetric(horizontal: 10),
             child: SizedBox(
               height: 100,
               child: Stack(
                 children: [
                   Align(
                     alignment: Alignment.bottomCenter,
                     child: Container(
                       height: 80,
                       decoration: const BoxDecoration(
                         borderRadius: BorderRadius.all(Radius.circular(20)),
                         border:
                             Border.fromBorderSide(BorderSide(color: Colors.grey)),
                       ),
                     ),
                   ),
                   Positioned.fill(
                     child: Padding(
                       padding: const EdgeInsets.only(bottom: 20),
                       child: Row(
                         crossAxisAlignment: CrossAxisAlignment.end,
                         mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                         children: List.generate(
                           _maxPages,
                           (index) => _buildTab(
                             'Home',
                             index,
                             isCenter: index == 2,
                           ),
                         ),
                       ),
                     ),
                   ),
                 ],
               ),
             ),
           ),
         ),
       );
     }
    }
    
    Login or Signup to reply.
  2. Also, try with NavigationBar wich has replaced BottomNavigationBar in Flutter 3.16 to see if there are more properties that you might find usable.

    https://api.flutter.dev/flutter/material/NavigationBar-class.html

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search