Flutter NotificationListener 监听列表的滚动

import 'package:flutter/material.dart';
import 'package:flutter_imagenetwork/flutter_imagenetwork.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List<String> images =
      List.generate(3, (i) => 'https://picsum.photos/id/$i/800/800');

  ScrollController _scrollController = ScrollController();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: NotificationListener(
        onNotification: (Notification notification) {
          if (notification is ScrollStartNotification) {
            print('滚动开始');
          }
          if(notification is ScrollUpdateNotification){
            print('滚动中');
          }
          if (notification is ScrollEndNotification) {
            print('停止滚动');
            if (_scrollController.position.extentAfter == 0) {
              print('滚动到底部');
            }
            if (_scrollController.position.extentBefore == 0) {
              print('滚动到头部');
            }
          }
        },
        child: ListView.builder(
          controller: _scrollController,
          itemCount: images.length,
          itemBuilder: (context, index) {
            return Padding(
              padding: const EdgeInsets.all(8.0),
              child: AjanuwImage(
                image: AjanuwNetworkImage(images[index]),
                loadingBuilder: AjanuwImage.defaultLoadingBuilder,
                errorBuilder: AjanuwImage.defaultErrorBuilder,
                gaplessPlayback: true,
              ),
            );
          },
        ),
      ),
    );
  }
}
posted @ 2019-08-19 18:57  Ajanuw  阅读(876)  评论(0编辑  收藏  举报