Skip to content

Instantly share code, notes, and snippets.

@mjohnsullivan
Created July 11, 2018 17:13
Show Gist options
  • Save mjohnsullivan/baa78b73e7fb6ca40224646bea6a41f3 to your computer and use it in GitHub Desktop.
Save mjohnsullivan/baa78b73e7fb6ca40224646bea6a41f3 to your computer and use it in GitHub Desktop.
Detect when a scrollable widget starts or stops scrolling
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Scroll Detection',
home: Scaffold(
appBar: AppBar(
title: Text('Scrolling Detection'),
),
body: Body(),
),
);
}
}
class Body extends StatefulWidget {
@override
createState() => _BodyState();
}
class _BodyState extends State<Body> {
String scrollingMessage = 'I am a scroll detector';
bool _scrollingStarted() {
setState(() => scrollingMessage = 'Wheeeeeeee!');
return false;
}
bool _scrollingEnded() {
setState(() => scrollingMessage = 'Snore ...');
return false;
}
@override
Widget build(BuildContext context) {
return Column(children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(scrollingMessage),
),
NotificationListener<ScrollStartNotification>(
onNotification: (_) => _scrollingStarted(),
child: NotificationListener<ScrollEndNotification>(
onNotification: (_) => _scrollingEnded(),
child: Expanded(
child: ListView(
children: List<ListTile>.generate(
100,
(i) => ListTile(title: Text('$i')),
)),
),
),
),
]);
}
}
@JulianBissekkou
Copy link

Really helpful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment