【问题标题】:Design BottomNavigationBar in Flutter在 Flutter 中设计 BottomNavigationBar
【发布时间】:2022-11-18 03:08:32
【问题描述】:

这个BottomNavigationBar我在设计

我是这样设计的,但是没有设置中间的图标。我已经为这个图标设计了样式,只需按图片中的样式放置即可。

这是我的输出

我的代码

 return Scaffold(
      bottomNavigationBar: Neumorphic(
        margin: const EdgeInsets.only(left: 20, right: 20, top: 2, bottom: 14),
        style: NeumorphicStyle(
          depth: -3,
          boxShape: const NeumorphicBoxShape.stadium(),
          shadowLightColorEmboss: Colors.white,
          shadowDarkColorEmboss: Colors.grey,
          color: Colors.grey[200],
          intensity: 0.8,
        ),
        child: BottomNavigationBar(
          currentIndex: currentIndex,
          onTap: (index) => setState(() => currentIndex = index),
          iconSize: 30,
          type: BottomNavigationBarType.fixed,
          showSelectedLabels: false,
          showUnselectedLabels: false,
          items: [
            const BottomNavigationBarItem(
                icon: Icon(Icons.home, color: Color(0xFF59BCCB)),
                label: "Home"),
            const BottomNavigationBarItem(
                icon: Icon(Icons.star_rounded, color: Color(0xFF59BCCB)),
                label: "Home"),
            BottomNavigationBarItem(
                icon: Stack(
                  children: [
                    Container(
                      width: MediaQuery.of(context).size.width,
                      height: 50,
                      padding: const EdgeInsets.all(10),
                      decoration: ShapeDecoration(
                        shape: const StadiumBorder(),
                        color: const Color.fromRGBO(133, 208, 212, 1),
                        shadows: <BoxShadow>[
                          const BoxShadow(
                            color: Colors.white,
                            spreadRadius: 8,
                            blurRadius: 2,
                            offset: Offset(0, 0),
                          ),
                          BoxShadow(
                            color: Colors.grey.shade300,
                            spreadRadius: 3,
                            blurRadius: 3,
                            offset: const Offset(5, 2),
                          ),
                        ],
                      ),
                      child: const Text(
                        'S',
                        textAlign: TextAlign.center,
                        style: TextStyle(
                            color: Colors.white,
                            fontFamily: "ROCK",
                            fontSize: 18),
                      ),
                    ),
                  ],
                ),
                label: "Home"),
            const BottomNavigationBarItem(
                icon: Icon(Icons.notifications_outlined,
                    color: Color(0xFF59BCCB)),
                label: "Home"),
            const BottomNavigationBarItem(
                icon: Icon(Icons.person, color: Color(0xFF59BCCB)),
                label: "Home"),
          ],
        ),
      ),
      body: IndexedStack(index: currentIndex, children: screens),
    );

【问题讨论】:

    标签: flutter dart flutter-bottomnavigation


    【解决方案1】:

    当您尝试模仿自定义 BottomNav 设计时,为什么不创建自定义导航而不是使用有限的默认导航?

    检查一下,这需要在 Box Shadow 中进行一些修改,但它可以使用更简单的代码实现输出。

    import 'package:flutter_neumorphic/flutter_neumorphic.dart';
    
    class Sample extends StatelessWidget {
      const Sample({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(),
          body: Stack(children: [
            Container(
              padding: EdgeInsets.only(bottom: 70),
              color: Colors.white,
              child: IndexedStack(index: currentIndex, children: screens),
            ),
            Padding(
              padding: const EdgeInsets.only(bottom: 8.0),
              child: Align(
                alignment: Alignment.bottomCenter,
                child: Neumorphic(
                  margin: const EdgeInsets.only(
                      left: 20, right: 20, top: 2, bottom: 14),
                  style: NeumorphicStyle(
                    depth: -3,
                    boxShape: const NeumorphicBoxShape.stadium(),
                    shadowLightColorEmboss: Colors.white,
                    shadowDarkColorEmboss: Colors.grey,
                    color: Colors.grey[200],
                    intensity: 0.8,
                  ),
                  child: Container(
                    width: MediaQuery.of(context).size.width,
                    height: 50,
                    decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(25)),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: [
                        ClipRRect(
                          borderRadius: BorderRadius.circular(25),
                          child: Material(
                            shape: const StadiumBorder(),
                            color: Colors.transparent,
                            child: InkWell(
                              child: const Padding(
                                padding: EdgeInsets.all(10.0),
                                child: Icon(Icons.home,
                                    size: 30, color: Color(0xFF59BCCB)),
                              ),
                              onTap: () {},
                            ),
                          ),
                        ),
                        ClipRRect(
                          borderRadius: BorderRadius.circular(25),
                          child: Material(
                            shape: const StadiumBorder(),
                            color: Colors.transparent,
                            child: InkWell(
                              child: const Padding(
                                padding: EdgeInsets.all(10.0),
                                child: Icon(Icons.star_rounded,
                                    size: 30, color: Color(0xFF59BCCB)),
                              ),
                              onTap: () {},
                            ),
                          ),
                        ),
                        Container(
                          width: 50,
                        ),
                        ClipRRect(
                          borderRadius: BorderRadius.circular(25),
                          child: Material(
                            shape: const StadiumBorder(),
                            color: Colors.transparent,
                            child: InkWell(
                              child: const Padding(
                                padding: EdgeInsets.all(10.0),
                                child: Icon(Icons.notifications_outlined,
                                    size: 30, color: Color(0xFF59BCCB)),
                              ),
                              onTap: () {},
                            ),
                          ),
                        ),
                        ClipRRect(
                          borderRadius: BorderRadius.circular(25),
                          child: Material(
                            shape: const StadiumBorder(),
                            color: Colors.transparent,
                            child: InkWell(
                              child: const Padding(
                                padding: EdgeInsets.all(10.0),
                                child: Icon(Icons.person,
                                    size: 30, color: Color(0xFF59BCCB)),
                              ),
                              onTap: () {},
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            ),
            Padding(
              padding: const EdgeInsets.only(bottom: 15.0),
              child: Align(
                alignment: Alignment.bottomCenter,
                child: Container(
                  width: 70,
                  height: 70,
                  padding: const EdgeInsets.all(10),
                  decoration: ShapeDecoration(
                    shape: const StadiumBorder(),
                    color: const Color.fromRGBO(133, 208, 212, 1),
                    shadows: <BoxShadow>[
                      const BoxShadow(
                        color: Colors.white,
                        spreadRadius: 8,
                        blurRadius: 2,
                        offset: Offset(0, 0),
                      ),
                      BoxShadow(
                        color: Colors.grey.shade300,
                        spreadRadius: 3,
                        blurRadius: 3,
                        offset: const Offset(5, 2),
                      ),
                    ],
                  ),
                  child: ClipRRect(
                    borderRadius: BorderRadius.circular(25),
                    child: Material(
                      color: Colors.transparent,
                      child: InkWell(
                        onTap: () {},
                        child: const Center(
                          child: Text(
                            'S',
                            textAlign: TextAlign.center,
                            style: TextStyle(
                                color: Colors.white,
                                fontFamily: "ROCK",
                                fontSize: 22),
                          ),
                        ),
                      ),
                    ),
                  ),
                ),
              ),
            )
          ]),
        );
      }
    }
    
    

    这使用堆栈的两个子组件。第一个是包含项目的 Row,其中一个是空白的。第二个是像 Container 这样的 Circular Fab,它位于 Row 上方并居中以正确定位。

    输出:

    编辑:我检查了实现,我认为考虑到最好的 UI/UX,这很好。滚动时您将能够看到 BottomNav 后面的内容,并提供底部填充以将内容保持在底部导航上方。

    【讨论】:

    • 你怎么设计的,它破坏了我的整个布局。我已经更新了底部 nav 的整个代码。请检查一下。
    • @MansoorMalik 现在检查一下。
    • 但现在我无法在点击图标时转到特定屏幕。
    • @MansoorMalik 因为您没有在这些图标的onTap 上设置特定功能。
    • 仍然不会页面
    猜你喜欢
    • 1970-01-01
    • 2021-05-20
    • 2019-06-09
    • 2020-07-29
    • 1970-01-01
    • 2020-11-30
    • 2020-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多