如何仅添加一个BottomNavigationBarItem [英] How to add one BottomNavigationBarItem only

查看:17
本文介绍了如何仅添加一个BottomNavigationBarItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个BottomNavigationBar,我只需要在其中添加一个集中按钮,但我收到此错误:

‘package:flutter/src/material/bottom_navigation_bar.dart’:断言失败:第191行位置15:‘items.length>=2’:不是真的。

哪个是合乎逻辑的,因为颤动的源代码有这样的条件:

//..
assert(items.length >= 2),

那么,这是我的代码,是否可以使用BottomNavigationBar解决此问题以保持代码整洁?

BottomNavigationBar(

   items: <BottomNavigationBarItem>[
      buildBottomNavigationBarItem(
         iconData: Icons.close,
      ),

// AN ERROR AFTER COMMENTING THIS:

//    buildBottomNavigationBarItem(
//       iconData: Icons.open,
//    ),
   ],
),


BottomNavigationBarItem buildBottomNavigationBarItem(
      {IconData iconData, String title = ''}
    ) {
    return BottomNavigationBarItem(
      icon: Icon(
        iconData,
        color: Theme.of(context).accentColor,
        size: 0.04 * _deviceHeight,
      ),
      title: Text(
        title,
      ),
    );
}

谢谢

推荐答案

您不能使用BottomNavigationBar,但是您可以创建自己的小部件并将其传递给bottomNavigationBar参数。

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(primarySwatch: Colors.blue),
      home: Scaffold(
        body: SafeArea(
          child: Text('Hi'),
        ),
        bottomNavigationBar: Container(
          height: 60,
          color: Colors.black12,
          child: InkWell(
            onTap: () => print('tap on close'),
            child: Padding(
              padding: EdgeInsets.only(top: 8.0),
              child: Column(
                children: <Widget>[
                  Icon(
                    Icons.close,
                    color: Theme.of(context).accentColor,
                  ),
                  Text('close'),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}

如果新的自定义底部导航栏与电话的操作系统GUI重叠,您可以用SafeArea小工具包装InkWell

这篇关于如何仅添加一个BottomNavigationBarItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆