【问题标题】:Flutter how to add margin or padding in BottomNavigationBarFlutter如何在BottomNavigationBar中添加边距或填充
【发布时间】:2020-03-11 18:03:42
【问题描述】:

我正在尝试制作底部导航栏,但在屏幕上左右填充。现在我用容器包装 BottomNavigationBar 并在那里添加填充。问题是BottomNavigationBar默认背景仍然包裹着所有图层,那我们可以去掉那里的背景颜色吗?

Goal

Current result

bottomNavigationBar: Container(
    margin: EdgeInsets.only(left: 16, right: 16),
    decoration: BoxDecoration(
        color: Colors.black,
        borderRadius: BorderRadius.only(
            topLeft: Radius.circular(20),
            topRight: Radius.circular(20)),
        ),
    child: BottomNavigationBar(
      backgroundColor: Colors.transparent,
      showUnselectedLabels: true,
      type: BottomNavigationBarType.fixed,
      elevation: 0,
      currentIndex: _currentIndex,
      items: [
        BottomNavigationBarItem(
            icon: Icon(Icons.home), title: Text('Home')),
        BottomNavigationBarItem(
            icon: Icon(Icons.local_activity), title: Text('Activity')),
        BottomNavigationBarItem(
            icon: Icon(Icons.inbox), title: Text('Inbox')),
        BottomNavigationBarItem(
            icon: Icon(Icons.person), title: Text('Profile')),
      ],
    ),
  ),

编辑:我已经删除了脚手架和所有主题中的背景颜色,但是当您滚动项目时,您可以看到那里仍然有背景 Remove Scafold bg

编辑 2:这里是活动的代码

class App extends StatelessWidget {
  final List<Widget> _children = [
    Center(
      child: Container(
        height: 850,
        color: Colors.red,
      ),
    )
  ];
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: _children[0],
        bottomNavigationBar: Container(
          margin: EdgeInsets.only(left: 16, right: 16),
          decoration: BoxDecoration(
            color: Colors.amber,
            borderRadius: BorderRadius.only(
                topLeft: Radius.circular(200), topRight: Radius.circular(200)),
          ),
          child: BottomNavigationBar(
            backgroundColor: Colors.transparent,
            showUnselectedLabels: true,
            type: BottomNavigationBarType.fixed,
            elevation: 0,
            items: [
              BottomNavigationBarItem(
                  icon: Icon(Icons.home), title: Text('Home')),
              BottomNavigationBarItem(
                  icon: Icon(Icons.local_activity), title: Text('Activity')),
              BottomNavigationBarItem(
                  icon: Icon(Icons.inbox), title: Text('Inbox')),
              BottomNavigationBarItem(
                  icon: Icon(Icons.person), title: Text('Profile')),
            ],
          ),
        ),
      ),
    );
  }
}

result

【问题讨论】:

  • 它应该可以按您的预期工作。我认为你已经在你的脚手架或你使用过的任何其他包装方法上设置了背景颜色。请检查或共享整页源代码
  • 我也测试过,没有问题。您可能已通过主题数据为导航栏设置颜色或在父元素上设置背景颜色
  • 如果我们在列表视图中有滚动项,我们可以看到背景仍然存在
  • 你肯定做错了什么。如果您需要帮助,请发布发生此问题的活动的完整代码。
  • helo @OMiShah 我已经发布了活动代码和结果截图,你能看看

标签: flutter flutter-layout


【解决方案1】:

您需要将BodyBottomNavigationBar 放在Stack 下,以便BottomNavigationBar 可以放在主体内容的顶部。

您的完整代码将是:

import 'package:flutter/material.dart';

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final List<Widget> _children = [
      Center(
        child: Container(
          height: 850, // use 'MediaQuery.of(context).size.height' to fit the screen height,
          color: Colors.red,
        ),
      )
    ];

    return MaterialApp(
        debugShowCheckedModeBanner: false,
        home: Scaffold(
          body: Stack(
            children: <Widget>[
              _children[0],
              Positioned(
                left: 0,
                right: 0,
                bottom: 0,
                child: bottomNavigationBar(),
              ),
            ],
          ),
        ));
  }
}

Widget bottomNavigationBar() {
  return Container(
    margin: EdgeInsets.only(left: 16, right: 16),
    decoration: BoxDecoration(
      color: Colors.amber,
      borderRadius: BorderRadius.only(
          topLeft: Radius.circular(200), topRight: Radius.circular(200)),
    ),
    child: BottomNavigationBar(
      backgroundColor: Colors.transparent,
      showUnselectedLabels: true,
      type: BottomNavigationBarType.fixed,
      elevation: 0,
      items: [
        BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('Home')),
        BottomNavigationBarItem(
            icon: Icon(Icons.local_activity), title: Text('Activity')),
        BottomNavigationBarItem(icon: Icon(Icons.inbox), title: Text('Inbox')),
        BottomNavigationBarItem(
            icon: Icon(Icons.person), title: Text('Profile')),
      ],
    ),
  );
}

部分代码来自:

How to set border radius to bottom app bar in a flutter app?

【讨论】:

    【解决方案2】:

    我知道有点晚了。但这应该对像我这样的未来观众有所帮助。

    来自BottomNavigationBarItemicon 参数接受Widget。我所做的只是将我的 Icon 包装成一个 Container 并定义一个 padding

    BottomNavigationBarItem(
     icon: Container(
       padding: EdgeInsets.symmetric(vertical: 10),
       child: Icon(
         Icons.home
       )
     )    
    )
    

    【讨论】:

    • 这与我采用的解决方案相同,但这只会移动图标,而不是标签!
    • 我们如何也可以移动标签?
    猜你喜欢
    • 2019-08-13
    • 2017-05-19
    • 1970-01-01
    • 1970-01-01
    • 2021-02-18
    • 2015-08-14
    • 2012-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多