flutter - 当一个小部件在它前面颤动时在 PageView 中滑动
问题描述
我里面有一个 Stack 和一个 PageView。Stack 中我的 PageView 前面还有一些其他小部件。像这样的东西:
Stack(
PageView(...),
Container(...),
Text(...),
....
),
现在,如果我的手指触摸到其他小部件,则我尝试滑动 PageView 滑动采场和操作。
我怎样才能让它工作?
解决方案
小部件树上的 UI 优先级是自下而上,这意味着在渲染Stack
小部件时,PageView
小部件放置在最后一层,这就是您面临滑动问题的原因。您可能有充分的理由将其他小部件放在PageView
. 为了解决这个问题,您可以使用另一个GestureDetector
作为子组件的最后一个 WidgetStack
并用于PageController
在页面之间切换。
Stack(
PageView(...),
Container(...),
Text(...),
/// ....
GestureDetector(),///* you can test and set animation, direction, duration etc
),
完整的小部件
class SwapPV extends StatefulWidget {
const SwapPV({Key? key}) : super(key: key);
@override
_SwapPVState createState() => _SwapPVState();
}
class _SwapPVState extends State<SwapPV> {
PageController controller = PageController();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
alignment: Alignment.center,
children: [
PageView(
controller: controller,
children: [
Container(
color: Colors.red,
),
Container(
color: Colors.amber,
),
Container(
color: Colors.green,
),
],
),
Container(
color: Colors.pink.withOpacity(.2),
child: Text("OverLap Container"),
),
Align(alignment: Alignment(0, .1), child: Text("Another OverLapText")),
///THis will controll the PageView
GestureDetector(
onTap: () {},
onPanUpdate: (details) {
// Swiping in right direction.
if (details.delta.dx > 0) {
controller.nextPage(
duration: Duration(milliseconds: 200), curve: Curves.easeIn);
}
// Swiping in left direction.
if (details.delta.dx < 0) {
controller.previousPage(
duration: Duration(milliseconds: 200),
curve: Curves.easeInOut);
}
},
)
],
));
}
}
推荐阅读
- java - java.lang.IllegalArgumentException:URI 不是绝对的 - FileInputStream
- c# - 知道何时以及如何旋转对象
- javascript - 多维数组正确但返回值未定义?
- vbscript - VBScript 在计划时无法正常工作
- html - 将 Ionic 页面放到网站上
- matlab - 如何在 Matlab 中使用默认或动态生成的文件名进行保存?
- aws-mobilehub - 使用 AWS Mobile Hub + AWS Amplify 会带来或增加 HIPAA 审计流程的问题吗?
- r - R-从数据框中该组的每个元素中减去该组的平均值
- python - 在python中更改x标签
- excel - 关于通过VBA导入google sheet到excel的问题