【问题标题】:Flutter add a black outline to top of BottomNavigationBarFlutter 在 BottomNavigationBar 顶部添加黑色轮廓
【发布时间】:2019-11-08 02:44:29
【问题描述】:

我试图弄清楚如何在 BottomNavigationBar 的顶部添加一条非常微妙的黑线,以使其开始与其余内容更加不同。 AirBnb 就是一个很好的例子。

有没有办法用 Flutter 实现这一点?

下面是我目前用来渲染 BottomNavigationBar 的小部件,我在下方附加了一个示例,其中 AirBnb 的导航栏顶部有一条明显的灰线。

@override
Widget build(BuildContext context) {
  return Scaffold(
    body: currentPage,
    bottomNavigationBar: PreferredSize(
      preferredSize: Size.fromHeight(50),
      child: Container (
        decoration: BoxDecoration(
        border: Border.all(color: Colors.black)
        ),
      )
    ),
  );
}

此 Widget 的父级:

void main() => runApp(MaterialApp(
  home: new MyApp(),
  debugShowCheckedModeBanner: true,
));

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Navbar();
  }
}

【问题讨论】:

  • 张贴一个你正在尝试做的样本。
  • @Doc 我添加了一张图片供参考
  • 您需要添加一些到目前为止您尝试过的代码。

标签: flutter


【解决方案1】:

如何在颤动的底部导航视图顶部添加线条?

代码

class BottomNavigationBarHandler {
  Widget bar(int currentIndex) {
    return Container(
        decoration: BoxDecoration(
            color: Colors.white,
            border: Border(top: BorderSide(color: Colors.white, width: 3.0))),
        child: BottomNavigationBar(
            backgroundColor: Colors.black,
            selectedItemColor: Colors.pinkAccent,
            unselectedItemColor: Colors.white,
            showSelectedLabels: false,
            showUnselectedLabels: false,
            onTap: onTabTapped,
            currentIndex: currentIndex,
            items: [
              BottomNavigationBarItem(
                icon: Icon(Icons.home_outlined),
                title: Text('Home')),
              BottomNavigationBarItem(
                icon: Icon(Icons.search),
                title: Text('Messages')),
              BottomNavigationBarItem(
                icon: Icon(Icons.add), 
                title: Text('Profile'))]));
  }

  void onTabTapped(int index) {
     print('BottomNavigationBarIndex ::' + index.toString());
  }
}

输出

【讨论】:

  • 最简单的答案,完美!
【解决方案2】:
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: PreferredSize(
        preferredSize: Size.fromHeight(200),
        child: Container(
          alignment: Alignment.center,
          color: globals.white,
          height: 100,
          child: Text('imyadunandan'),
        ),
      ),
      body: Container(
        color: globals.white,
      ),
      bottomNavigationBar: Container(
        decoration: BoxDecoration(
          color: globals.white,
          boxShadow: [
            BoxShadow(
              color: globals.black,
            ),
          ],
        ),
        child: BottomNavigationBar(
          items: [
            BottomNavigationBarItem(
              icon: Icon(Icons.android),
              title: Text('Android'),
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.desktop_windows),
              title: Text('Windows'),
            ),
          ],
          backgroundColor: globals.white,
          elevation: 0,
        ),
      ),
    );
  }

这段代码实现了休闲

【讨论】:

  • 不幸的是,我似乎无法让它工作:(当我尝试做这样的事情时,我的屏幕只会呈现全白
  • 显示你正在尝试做的事情
  • 我已经附上了我用来渲染 BottomNavigationBar 的当前代码以及一张图片作为示例
  • 查看答案
  • BoxShadow(color: Colors.grey[400], offset: Offset(0, -0.5)) 这样一来,它就消除了同样创建的底线。
猜你喜欢
  • 2020-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多