Flutter 元素溢出导致 MouseRegion 失效的解决方法
Flutter 元素溢出导致 MouseRegion 失效的解决方法
问题描述:
Flutter 中,当一个元素的宽度超出其父元素的宽度时,MouseRegion 的 hover 和 cursor 属性会失效。
解决方法:
-
使用其他的 Widget 代替
MouseRegion,如GestureDetector或InkWell等。 -
将
MouseRegion包装在一个可滚动的 Widget 中,如ListView、SingleChildScrollView等,这样即使元素溢出也能够滚动显示,而MouseRegion仍然有效。 -
尝试使用
Positioned Widget和Stack 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,
),
],
),
),
],
),
)
原文地址: https://www.cveoy.top/t/topic/mGfN 著作权归作者所有。请勿转载和采集!