【问题标题】:How to set BackGroundColor in BottomNavigationBarItem in flutter?如何在颤动的BottomNavigationBarItem中设置BackGroundColor?
【发布时间】:2021-11-27 12:48:33
【问题描述】:

[在此处输入图片说明][1] 那是我的代码 如何在flutter 中仅添加选定的BottomNavigationBarItem 背景颜色和大小。我在下面添加了我的模型屏幕图像..任何人请帮助我

bottomNavigationBar: BottomNavigationBar(
          type: BottomNavigationBarType.fixed,

          backgroundColor: Color(0xffffcc2a),
          showUnselectedLabels: true,
          currentIndex: _currentIndex,
          elevation: 20.0,
          onTap: callPage,

          selectedItemColor: Color(0xff3666ad),
          unselectedItemColor: Color(0xff3666ad),

          // this will be set when a new tab is tapped
          items: [
            BottomNavigationBarItem(
              icon: new Icon(
                Icons.ballot,
                color: Color(0xff3666ad),
              ),
              label: 'Insurance',
            ),
            BottomNavigationBarItem(
              icon: new FaIcon(
                FontAwesomeIcons.car,
                color: Color(0xff3666ad),
              ),
              label: 'Motor',
            ),
            BottomNavigationBarItem(
              icon: new Icon(
                Icons.medical_services,
                color: Color(0xff3666ad),
              ),
              label: ('Health'),
            ),
            BottomNavigationBarItem(
              // backgroundColor: Color(0xffffcc2a),
              icon: new Icon(
                Icons.flight,
                color: Color(0xff3666ad),
              ),
              label: ('Travel'),
            ),
            BottomNavigationBarItem(
              // backgroundColor: Color(0xffffcc2a),
              icon: new Icon(
                Icons.local_fire_department,
                color: Color(0xff3666ad),
              ),
              label: ('Fire'),
            ),
            BottomNavigationBarItem(
              // backgroundColor: Color(0xffffcc2a),
              icon: FaIcon(
                FontAwesomeIcons.layerGroup,
                color: Color(0xff3666ad),
              ),
              label: ('Misc'),
            )
          ],
        ),

【问题讨论】:

  • 你的模型屏幕在哪里?
  • sry...我不知道如何添加图片!!

标签: flutter dart flutter-bottomnavigation


【解决方案1】:

我在这种情况下使用自定义bottomNavigationBar,以你想要的方式玩弄风格。


class _TestWidgetState extends State<TestWidget> {
  int _currentIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: Container(
        color: Colors.green, // main background
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            NavItem(
              icon: const Icon(Icons.ac_unit),
              isSelected: 0 == _currentIndex,
              label: "Item 1",
              onTap: () {
                setState(() {
                  _currentIndex = 0;
                });
              },
            ),
            NavItem(
              icon: Icon(Icons.umbrella),
              isSelected: 1 == _currentIndex,
              label: "Item 2",
              onTap: () {
                setState(() {
                  _currentIndex = 1;
                });
              },
            ),
          ],
        ),
      ),
    );
  }
}

class NavItem extends StatelessWidget {
  const NavItem({
    Key? key,
    required this.icon,
    required this.isSelected,
    required this.onTap,
    required this.label,
  }) : super(key: key);

  final bool isSelected;
  final VoidCallback onTap;
  final String label;
  final Widget icon; // you may use different widget

  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: (InkWell(
        onTap: onTap,
        child: Container(
          color: isSelected
              ? Colors.pink
              : Colors.amber, //selected background pink
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              icon,
              Text("$label"),
            ],
          ),
        ),
      )),
    );
  }
}

【讨论】:

  • 谢谢.....对我帮助很大...
猜你喜欢
  • 2020-02-25
  • 2018-10-25
  • 2021-02-04
  • 1970-01-01
  • 1970-01-01
  • 2010-10-05
  • 1970-01-01
  • 1970-01-01
  • 2023-03-05
相关资源
最近更新 更多