自定义底部导航栏项目(提取为小部件)

Custom Bottom Navigation Bar Item (extract as widget)

如何提取 BottomNavigationBarItem 以获得更具可读性的代码并重用项目小部件?

如果我在无状态小部件中提取它们,我会收到错误消息:

can't be assigned to the list type 'BottomNavigationBarItem'

BottomNavigationBarItem(
            icon: Container(
              decoration: const BoxDecoration(
                color: Colors.black,
                shape: BoxShape.circle,
              ),
              height: 56,
              width: 56,
              child: const Icon(Icons.favorite),
            ),
            label: '')

我知道我可以提取图标内的 Container 属性,但我想提取整个 Widget。 提前致谢!

要为特定小部件创建任何方法,您需要定义 item.eg 类型的 item.eg。

class CommonWidget{

BottomNavigationBarItem getItem(){
    return BottomNavigationBarItem(
            icon: Container(
              decoration: const BoxDecoration(
                color: Colors.black,
                shape: BoxShape.circle,
              ),
              height: 56,
              width: 56,
              child: const Icon(Icons.favorite),
            ),
            label: '');
   }

}

您可以在底部导航项列表中调用此 getItem。