如何提取BottomNavigationBarItem以获得更可读的代码并重用项目小部件?
如果我在无状态小部件中提取它们,我会得到一个错误:
无法分配给列表类型"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。提前谢谢!
要为特定的小部件创建任何方法,您需要定义该项的返回类型
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。