I have question - I want to create bottomNavigationBar but in some custom way. Please take a look on this image: navImage from Desing

我为一个愚蠢的问题向你道歉/ 我试着把每一件东西都放在底部,但现在我完全不知道该怎么做……

import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:streeti/static/static_colors.dart';

class CustomBottomNavigationBar extends StatefulWidget {
  const CustomBottomNavigationBar({super.key});

  @override
  State<CustomBottomNavigationBar> createState() =>
      _CustomBottomNavigationBarState();
}

class _CustomBottomNavigationBarState extends State<CustomBottomNavigationBar> {
  @override
  Widget build(BuildContext context) {
    return Container(
      height: 108,
      width: double.infinity,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(15),
          topRight: Radius.circular(15),
        ),
        border: Border.all(
          color: AppColors.blueColor,
        ),
      ),
      child: BottomNavigationBar(
        showUnselectedLabels: true,
        selectedFontSize: 10,
        unselectedLabelStyle:
            const TextStyle(color: Colors.black, fontSize: 10),
        backgroundColor: Colors.transparent,
        fixedColor: Colors.black,
        unselectedItemColor: Colors.black,
        items: [
          BottomNavigationBarItem(
            icon: Padding(
              padding: EdgeInsets.only(bottom: 5),
              child: SvgPicture.asset('lib/assets/images/email2.svg'),
            ),
            label: 'Nachricht',
          ),
          BottomNavigationBarItem(
            icon: Padding(
              padding: EdgeInsets.only(bottom: 5),
              child: SvgPicture.asset('lib/assets/images/map.svg'),
            ),
            label: 'Karte',
          ),
          BottomNavigationBarItem(
            icon: Padding(
              padding: EdgeInsets.only(bottom: 5),
              child: SvgPicture.asset('lib/assets/images/hot.svg'),
            ),
            label: 'Angebote',
          ),
          BottomNavigationBarItem(
            icon: Padding(
              padding: EdgeInsets.only(bottom: 5),
              child: SvgPicture.asset('lib/assets/images/community.svg'),
            ),
            label: 'Community',
          ),
          BottomNavigationBarItem(
            icon: Padding(
              padding: EdgeInsets.only(bottom: 10),
              child: SvgPicture.asset('lib/assets/images/profil.svg'),
            ),
            label: 'Profil',
          ),
        ],
      ),
    );
  }
}

要调整Container以使中间的图标显示在我理解的Container的略上方,我应该使用Stack()吗?

My navigations looks like this right now: My nav - right now

推荐答案

有时预定义的小部件是有限的,比如BottomNavigationBar,但您使用的是Ffltter,因此您可以自定义和创建自己的小部件:d.

我为您创建了一个示例,我使用IndexedStack来控制视图页面,使用简单的ContainerStack来创建导航底栏(您可以在自己的小部件中提取它).

结果:

enter image description here

代码:

class MyNewPage extends StatefulWidget {
 const MyNewPage({super.key});

 @override
 State<MyNewPage> createState() => _MyNewPageState();
}

class _MyNewPageState extends State<MyNewPage> {
 int _currentPage = 0;
 final int _maxPages = 5;

 Widget _buildPage(String text) {
   return Center(
     child: Text(
       text,
       style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 60),
     ),
   );
 }

 Widget _buildTab(
   String text,
   int index, {
   bool isCenter = false,
 }) {
   return InkWell(
     onTap: () {
       setState(() {
         _currentPage = index;
       });
     },
     child: Column(
       mainAxisSize: MainAxisSize.min,
       mainAxisAlignment: MainAxisAlignment.center,
       children: [
         isCenter
             ? const Icon(
                 Icons.apartment,
                 size: 50,
               )
             : const Icon(Icons.home),
         Text(text),
       ],
     ),
   );
 }

 @override
 Widget build(BuildContext context) {
   return Scaffold(
     body: IndexedStack(
       index: _currentPage,
       children: List.generate(
         _maxPages,
         (index) => _buildPage('Page: $index'),
       ),
     ),
     bottomNavigationBar: SafeArea(
       child: Padding(
         padding: const EdgeInsets.symmetric(horizontal: 10),
         child: SizedBox(
           height: 100,
           child: Stack(
             children: [
               Align(
                 alignment: Alignment.bottomCenter,
                 child: Container(
                   height: 80,
                   decoration: const BoxDecoration(
                     borderRadius: BorderRadius.all(Radius.circular(20)),
                     border:
                         Border.fromBorderSide(BorderSide(color: Colors.grey)),
                   ),
                 ),
               ),
               Positioned.fill(
                 child: Padding(
                   padding: const EdgeInsets.only(bottom: 20),
                   child: Row(
                     crossAxisAlignment: CrossAxisAlignment.end,
                     mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                     children: List.generate(
                       _maxPages,
                       (index) => _buildTab(
                         'Home',
                         index,
                         isCenter: index == 2,
                       ),
                     ),
                   ),
                 ),
               ),
             ],
           ),
         ),
       ),
     ),
   );
 }
}

Flutter相关问答推荐

如何在flutter文本字段中添加要点条目?

如何在Flutter的easy_translation包中链接嵌套的链接翻译?

带有可滚动页面(不包括分页区)的Flutter 页面视图

当键盘在抖动中打开时,如何才能看到包装在SingleChildScrollView中的列中的最后一个小工具?

如何使用flutter_local_notification包处理通知操作?

火焰Flutter 在多个向量之间插补2

StateNotifer正在riverpod中提供初始状态值(bool)

Flutter记录返回类型问题

当键盘隐藏表单时该怎么办?

Paypal 支付网关添加带有 magento 2 网络详细信息用户名、密码、签名 Magento 2 的 flutter 应用程序?

转换函数上传文件上传Uint8List

如何使用 riverpod_annotation 创建非自动处置提供程序?

如何显示从 MariaDB 获取的数据到列表视图

flutter 创建一个带有 2 个图标的按钮

如何在 tabBar 中的选项卡之间制作垂直线?

Flutter 用户过滤器

在 Flutter Bloc 中具有称为 status 的属性的一种状态或不同的状态,哪个更可取?

如何从dart 中的当前日期时间中减go 1 天?

用于空值 FirebaseFirestore 的空值判断运算符

如何从 MemoryFileSystem 字节创建假 dart:io 文件?