【问题标题】:How to make Ink effect fill all the space in a BottomNavigationBarItem如何使 Ink 效果填充 BottomNavigationBarItem 中的所有空间
【发布时间】:2020-04-01 10:23:22
【问题描述】:

我正在制作一个带有 BottomNavigationBar 的简单 Flutter 应用程序,并且我的 BottomNavigationBarItem 中的标签超过了我点击它时创建的墨水效果。出于某种原因,它似乎只覆盖了图标而不是标签。

如何更改此行为以适应标签?我也希望有一个更矩形的墨水反应。

编辑:

我的导航栏代码

bottomNavigationBar: BottomNavigationBar(
    type: BottomNavigationBarType.fixed,
    selectedItemColor: Colors.blue[900],
    unselectedItemColor: Colors.black87,
    onTap: onTabTapped,
    showUnselectedLabels: true,
    currentIndex: _currentIndex,
    items: const <BottomNavigationBarItem>[
      BottomNavigationBarItem(
        icon: Icon(Icons.home),
        title: Text('Home'),
      ),
      BottomNavigationBarItem(
        icon: Icon(Icons.collections),
        title: Text('Collections'),
      ),
      BottomNavigationBarItem(
        icon: Icon(Icons.phone),
        title: Text('Recent'),
      ),
      BottomNavigationBarItem(
        icon: Icon(Icons.more_horiz),
        title: Text('More'),
      ),
    ],
  ),

Current Behavior

Expected Behavior

【问题讨论】:

  • 嗨。看来您是 stackoverflow 的新手。如果您没有正确阐述它,人们可能会否决您的问题。如果您提供您正在谈论的代码,您的问题会更好。如果您不知道我在说什么,请阅读有关如何提问的指南。 :)
  • 尝试使用BottomAppBar而不是BottoNavigationBar

标签: flutter flutter-layout


【解决方案1】:

在摆弄了 Flutter 标准库之后,我现在意识到 BottomNavigationBarItem 不是一个小部件,而是一个容器类,用于封装 BottomNavigationBar 对其项目所需的信息。

很遗憾,您需要覆盖 BottomNavigationBar 并创建自己的 CustomBottomNavigationBar 来做您想做的事情。

好消息是这非常容易,因为您只需更改一行代码。 :)

我从$FLUTTER_HOME/packages/flutter/lib/src/material/bottom_navigation_bar.dart 复制代码并只进行了一次调整:在第 480 行,我将InkResponse 更改为InkWell,现在它可以按您的意愿工作了。

InkResponse:

与InkWell:

我建议您在项目中创建一个包含覆盖类的覆盖文件夹,我就是这样做的(我已经覆盖了默认的底部工作表类):

然后,您可以像这样导入覆盖的类:

import 'package:your_project/widgets/override/bottom_navigation_bar.dart' as BottomNavigationBarOverride;

然后像这样使用它:

  bottomNavigationBar: BottomNavigationBarOverride.BottomNavigationBar(
    type: BottomNavigationBarType.fixed,
    selectedItemColor: Colors.blue[900],
    unselectedItemColor: Colors.black87,
    onTap: onTabTapped,
    showUnselectedLabels: true,
    currentIndex: _currentIndex,
    items: const <BottomNavigationBarItem>[
      BottomNavigationBarItem(
        icon: Icon(Icons.home),
        title: Text('Home'),
      ),
      BottomNavigationBarItem(
        icon: Icon(Icons.collections),
        title: Text('Collections'),
      ),
      BottomNavigationBarItem(
        icon: Icon(Icons.phone),
        title: Text('Recent'),
      ),
      BottomNavigationBarItem(
        icon: Icon(Icons.more_horiz),
        title: Text('More'),
      ),
    ],
  ),

旧响应

如果您提供更多代码,我可以为您提供更多帮助。

从您提供的图片来看,这是预期的行为,因为底部导航项并不打算很大。正如material spec 所建议的那样,他们打算快速而干净:

如果您打算只做一个“选项”,我建议您将您的小部件更改为FlatButton。但请注意,这违反规范,因为出于可用性目的,底部导航项应包含至少 3 个选项,最多 5 个选项。

【讨论】:

  • 我实际上有 4 个选项。我已经编辑了帖子以包含我的示例代码。我只是希望在单击时墨水飞溅效果跨越整个 BottomNavigationBarItem,而不仅仅是图标周围的圆圈。
  • 谢谢!这正是我想要的。
【解决方案2】:

经过几次尝试,我认为更改 splashFactory 可以解决问题。

MaterialApp(
  theme: ThemeData(
    splashFactory: InkRipple.splashFactory,
  ),
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-07
    • 2013-07-19
    • 2016-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-26
    相关资源
    最近更新 更多