Flutter 元素溢出导致 MouseRegion 失效的解决方法

问题描述:

Flutter 中,当一个元素的宽度超出其父元素的宽度时,MouseRegionhovercursor 属性会失效。

解决方法:

  1. 使用其他的 Widget 代替 MouseRegion,如 GestureDetectorInkWell 等。

  2. MouseRegion 包装在一个可滚动的 Widget 中,如 ListViewSingleChildScrollView 等,这样即使元素溢出也能够滚动显示,而 MouseRegion 仍然有效。

  3. 尝试使用 Positioned WidgetStack Widget 实现布局,将 MouseRegion 放在 Stack 中的最上层,这样即使元素溢出也能够响应鼠标事件。

示例代码:

SingleChildScrollView(
  scrollDirection: Axis.horizontal,
  child: Row(
    children: <Widget>[
      Container(
        width: 100,
        height: 100,
        color: Colors.blue,
        child: Stack(
          children: <Widget>[
            Positioned.fill(
              child: MouseRegion(
                cursor: SystemMouseCursors.click,
                onHover: (event) {},
                child: Container(
                  color: Colors.transparent,
                ),
              ),
            ),
            Container(
              width: 200,
              height: 200,
              color: Colors.red,
            ),
          ],
        ),
      ),
    ],
  ),
)
Flutter 元素溢出导致 MouseRegion 失效的解决方法

原文地址: https://www.cveoy.top/t/topic/mGfN 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录