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

发布于 2025-01-19 13:39:47 字数 535 浏览 0 评论 0原文

我如何提取底部努力范围,以便拥有更可读的代码并重复使用该项目窗口小部件?

如果我在无状态小部件中提取它们,我会发现一个错误:

无法分配到列表类型'bottomNavigationBariTem'

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

内提取容器,但是我想提取整个小部件。 先感谢您!

How can I extract BottomNavigationBarItem in order to have a more readable code and reuse the item widget?

If I extract them in a Stateless Widget I get an error:

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: '')

I know I could extract the Container inside the icon property, but I'd like to extract the whole Widget.
Thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风吹短裙飘 2025-01-26 13:39:48

要为特定小部件创建任何方法,您需要定义该项目的返回类型。

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。

For creating any method for the particular widget , you need to define return type of that 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: '');
   }

}

And you can call this getItem inside your Bottom Navigation item List.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文