检查 PageView 中的过度滚动 - Flutter

Jos*_*hua 4 dart flutter

您好,我正在我的 flutter 应用程序中使用 PageView。我想检测用户何时过度滚动并超出页面。我如何实现它

我的PageView的代码:

PageView(
      controller: _controller,
      children: widget.imagePaths,
    ),
Run Code Online (Sandbox Code Playgroud)

Ruk*_*nJS 7

像这样把你的PageView里面包裹起来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,
                        ),
                      );
Run Code Online (Sandbox Code Playgroud)

完整脚本文件

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)
        ],
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)