Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter BottomNavigationBar: why selected item shifts other icons?

Tags:

flutter

I'm new to flutter and I created a BottomNavigationBar and add into it some BottomNavigationBarItem that contains flutter icons.

The problem is that, when I select one item of my BottomNavigationBar, this item shifts other icons.

this is a screenshot of my app:

enter image description here

is there a way to block this shift ?

my code:

import 'package:flutter/material.dart';

class Navbar extends StatefulWidget {
  const Navbar({ Key key }) : super(key: key);

  @override
  _NavbarState createState() => _NavbarState();
}

class _NavbarState extends State<Navbar> {

int index = 0;

@override
Widget build(BuildContext context) {
  return BottomNavigationBar(
    iconSize: 30,
    showSelectedLabels: false,
    showUnselectedLabels: false,
    selectedItemColor: Colors.blueGrey,
    unselectedItemColor: Colors.black,
    currentIndex: index,
    onTap: (int selectedIndex) {
      setState(() {
        index = selectedIndex;
      });
    },
    items: [
      BottomNavigationBarItem(
        icon: new Icon(
          Icons.home,
        ),
        title: new Text('Home'),
      ),
      BottomNavigationBarItem(
        icon: new Icon(
          Icons.search,
        ),
        title: new Text('Search'),
      ),
      BottomNavigationBarItem(
        icon: Icon(
          Icons.add,
        ),
        title: Text('Add')
      ),
      BottomNavigationBarItem(
        icon: Icon(
          Icons.favorite,
        ),
        title: Text('Add')
      ),
      BottomNavigationBarItem(
        icon: Icon(
          Icons.account_circle,
        ),
        title: Text('Account')
      )
    ],
  );
}
}
like image 361
Loufi Avatar asked Oct 07 '19 14:10

Loufi


People also ask

How to create bottom navigation bar in scaffold widget in flutter?

In flutter, we can easily implement the bottom navigation bar by using the default bottomnavigationbar property. In scaffold widget has property called BottomNavigationBar, that allows to create bottom navigation in easy way. Before creating bottom navigation bar, few things to remember, We can display only 2 to 5 navigation bar items only.

Is there a way to change the color of the bottomnavigationbaritem?

If anyone is looking for a solution to change the Bottom Navigation Bar Item's color,when "type" is set to "shifting", then give this a try: Show activity on this post. If all you want to change is the color of the BottomNavigationBarItem icon, you don't need to have two images with different colors for one icon. Just one is enough.

What is the bottonnavigationbar widget?

The BottonNavigationBar widget is used to show the bottom of an app. It can consist of multiple items such as icons, text, or both that leads to a different route depending upon the design of the application. It is meant to help the user navigate to different sections of the application in general.

How many navigation bar items can be displayed at the bottom?

Before creating bottom navigation bar, few things to remember, We can display only 2 to 5 navigation bar items only. Each navigation bar item must-have icon and label property. At least two navigation bar items required, otherwise, you will get an error. Here the simple example of bottom navigation bar without routes,


Video Answer


1 Answers

You can change this behavior of the BottomNavigationBar by setting its type parameter to BottomNavigationBarType.fixed when constructing it.

BottomNavigationBar(
  ...
  type: BottomNavigationBarType.fixed,
  ...
}

According to the documentation the default type is fixed if there are four or less items and shifting if there are more.

like image 123
Hannes Küttner Avatar answered Nov 02 '22 19:11

Hannes Küttner