首页 > 解决方案 > 如果在 Flutter 中禁用滚动,如何连续检测?

问题描述

我想不断检查是否无法滚动。

  @override
  void initState() {
    super.initState();

    WidgetsBinding.instance!.addPostFrameCallback((duration) {

      print("${_scrollViewController.position.maxScrollExtent}");
      // prints true if scrollable else false
      print("isScrollable = ${_scrollViewController.position.maxScrollExtent != 0}");

    });
  }

我试过这段代码,但它只检测到一次,而不是连续检测到。我该怎么办?

标签: flutterdartlistviewscrollscrollcontroller

解决方案


使用NotificationListener小部件。

例子:

NotificationListener<ScrollNotification>(
  child: ListView(
     children: MyListChilren()),
  onNotification: (ScrollNotification scrollNotif) {
    print(scrollNotif.metrics.maxScrollExtent);
  },
);

推荐阅读