dart 检查PageView中的过度滚动-抖动

5hcedyr0  于 2023-01-28  发布在  其他
关注(0)|答案(3)|浏览(166)

你好,我在flutter应用程序中使用PageView。我想检测用户何时过度滚动并超出页面。我如何实现它
我的网页浏览代码:

PageView(
      controller: _controller,
      children: widget.imagePaths,
    ),
7eumitmz

7eumitmz1#

把你的PageView像这样 Package 在一个NotificationListener<OverscrollIndicatorNotification>中,然后每当用户向任一方向滚动时,函数onNotification就会被调用。

return NotificationListener<
                          OverscrollIndicatorNotification>(
                        onNotification: (overscroll) {
                          print("overscrolled"); //do whatever you need to do when overscroll
                        },
                        child: PageView(
                          controller: _controller,
                          children: widget.imagePaths,
                        ),
                      );

完整脚本文件

import 'package:flutter/material.dart';

class Test extends StatefulWidget {
  @override
  _TestState createState() => _TestState();
}

class _TestState extends State<Test> {
  Widget build(BuildContext context) {
    return NotificationListener<OverscrollIndicatorNotification>(
      onNotification: (overscroll) {
        print("overscrolled");
      },
      child: PageView(
        children: [
          Container(color: Colors.red),
          Container(color: Colors.green)
        ],
      ),
    );
  }
}
vxf3dgd4

vxf3dgd42#

另一个解决方案是使用NotificationListener<OverscrollNotification>,因为这样你就可以选择你想要对过卷的量做出React。如果你想忽略偶然的小拖动移动,这是很有用的。
例如:

NotificationListener<OverscrollNotification>(
  onNotification: (OverscrollNotification notification) {
    if (notification.overscroll < 8) {
      // ignore, don't do anything
      return false;
    }

    // do something, for example: load next screen, etc ...

    return true;
  },
  child: PageView(
    controller: _controller,
    children: widget.imagePaths,
  ),
),

上例中的8是实验结果。您应该选择最适合您的量。
完整示例请 checkout this blog post

r1zhe5dt

r1zhe5dt3#

class Test extends StatefulWidget {
  @override
  _TestState createState() => _TestState();
}

class _TestState extends State<Test> {
  Widget build(BuildContext context) {
    return NotificationListener<OverscrollIndicatorNotification>(
      onNotification: (overscroll) {
        onNotification: (ScrollNotification notification) {
         if (notification is OverscrollNotification || 
             notification.metrics.outOfRange) {
              print("Bounced!");
           }
        return false;
      },
      child: PageView(
        children: [
          Container(color: Colors.red),
          Container(color: Colors.green),
        ],
      ),
    );
  }
}

相关问题