如何在Flutter中的`BottomNavigationBar()`中更改项目之间的间距?

时间:2019-10-16 17:21:40

标签: flutter dart bottomnavigationview

1。我希望我能做的

到目前为止,我的BottomNavigationBar看起来还不错(而且很简单),但是鉴于我只有2件物品,我希望将它们放得更近一些(也许在右边),因此更容易达到。我怎么做?有做这项工作的财产吗?也许在BottomNavigationBar主题内?

2。摘录

这是现在的样子:

bottomNavigationBar: BottomNavigationBar(
  items: <BottomNavigationBarItem>[
    BottomNavigationBarItem(
      icon: Icon(Icons.account_circle),
      title: Text('Personal Data'),
    ),
    BottomNavigationBarItem(
      icon: Icon(Icons.payment),
      title: Text('Contracts'),
    ),
  ],
  currentIndex: _selectedIndex,
  selectedItemColor: Colors.orange,
  onTap: _onItemTapped,
),

2 个答案:

答案 0 :(得分:1)

在检查BottomNavigationBar的源代码时,由于此行,因此似乎不太容易实现:

Widget _createContainer(List<Widget> tiles) {
    return DefaultTextStyle.merge(
      overflow: TextOverflow.ellipsis,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: tiles,
      ),
    );
  }

它在类中的私有函数中。意味着要更改包含图块的行的mainAxisAlignment属性,必须重新创建整个类。

答案 1 :(得分:0)

    @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: Theme(
        data: Theme.of(context).copyWith(
            canvasColor: Color.fromRGBO(255, 255, 255, 0),
              primaryColor: Colors.red,
                textTheme: Theme.of(context).textTheme.copyWith(
                    caption: TextStyle(
                        color: Colors
                            .white))), 
            child: Container(
              color: Colors.black,
              child: Padding(
                padding: const EdgeInsets.symmetric(horizontal: 40),
                   child: BottomNavigationBar(
                      showSelectedLabels: false,
                      showUnselectedLabels: false,
                      type: BottomNavigationBarType.fixed,
                      currentIndex: 0,
                      items: [
                        BottomNavigationBarItem(
                          icon: Icon(Icons.home),
                          title: Text(''),
                        ),
                        BottomNavigationBarItem(
                          icon: Icon(Icons.search),
                          title: Text(''),
                        ),
                        BottomNavigationBarItem(
                          icon: Icon(Icons.favorite_border),
                          title: Text(''),
                        ),
                        BottomNavigationBarItem(
                          icon: Icon(
                            Icons.folder_open,
                          ),
                          title: Text(''),
                        )
                      ],
                    ),
                  ),
                ),
              ),
            );

enter image description here