Created
August 2, 2021 00:05
-
-
Save monkeyswarm/ed127887e8e5844abfbf1817fb8ab9ae to your computer and use it in GitHub Desktop.
Track containers 5: use overscroll notification on child listview to drive parent listview
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Use overscroll notification from child listvieew to affect the parent listview. Requires use of ClampingScroll{hysics} | |
import 'package:flutter/material.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: MyHomePage(), | |
); | |
} | |
} | |
class MyHomePage extends StatelessWidget { | |
final _scrollController = ScrollController(); | |
Widget _buildContainer(Color color, Color borderColor) => | |
Container( | |
height: 300.0, | |
decoration: BoxDecoration( | |
color: color, border: Border.all(color: borderColor, width: 8)), | |
child: NotificationListener<OverscrollNotification>( | |
onNotification: (OverscrollNotification value) { | |
// Adapted from https://stackoverflow.com/questions/60579335/flutter-listview-scroll-parent-listview-when-child-listview-reach-bottom-c | |
if (value.overscroll < 0 && | |
_scrollController.offset + value.overscroll <= 0) { | |
if (_scrollController.offset != 0) | |
_scrollController.jumpTo(0); | |
return true; | |
} | |
if (_scrollController.offset + value.overscroll >= | |
_scrollController.position.maxScrollExtent) { | |
if (_scrollController.offset != | |
_scrollController.position.maxScrollExtent) | |
_scrollController | |
.jumpTo(_scrollController.position.maxScrollExtent); | |
return true; | |
} | |
_scrollController | |
.jumpTo(_scrollController.offset + value.overscroll); | |
return true; | |
}, | |
child: ListView( | |
physics: ClampingScrollPhysics(), | |
children: List.generate( | |
25, | |
(i) => Container( | |
height: 30.0, | |
child: Center(child: Text('foo $i'))))))); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Nested scrolling test'), | |
), | |
body: Column(children: [ | |
Container( | |
color: Color(0x88888888), | |
height: 600, | |
child: ListView(controller: _scrollController, children: [ | |
_buildContainer(Color(0x88FF0000), Color(0xFFFF0000)), | |
_buildContainer(Color(0x8800FF00), Color(0xFF00FF00)), | |
_buildContainer(Color(0x880000FF), Color(0xFF0000FF)), | |
]), | |
) | |
])); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment