Created
January 23, 2022 19:37
-
-
Save EArminjon/31bcc1704903e876de89d74a3c845e1e to your computer and use it in GitHub Desktop.
NotificationListener
This file contains 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
import 'dart:math'; | |
import 'package:flutter/material.dart'; | |
class Home extends StatefulWidget { | |
const Home({Key? key}) : super(key: key); | |
@override | |
_HomeState createState() => _HomeState(); | |
} | |
class SimpleBloc extends StatelessWidget { | |
const SimpleBloc({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
alignment: Alignment.center, | |
color: Colors.primaries[Random().nextInt(Colors.primaries.length)], | |
width: double.maxFinite, | |
height: 100, | |
); | |
} | |
} | |
class _HomeState extends State<Home> { | |
ScrollController controller = ScrollController(); | |
@override | |
void dispose() { | |
controller.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: ListView( | |
controller: controller, | |
children: [ | |
const SimpleBloc(), | |
const SimpleBloc(), | |
const SimpleBloc(), | |
Container( | |
height: 350, | |
padding: const EdgeInsets.all(16), | |
child: NotificationListener<OverscrollNotification>( | |
onNotification: (OverscrollNotification value) { | |
if (value.overscroll < 0 && controller.offset + value.overscroll <= 0) { | |
if (controller.offset != 0) { | |
controller.jumpTo(0); | |
} | |
} else if (controller.offset + value.overscroll >= controller.position.maxScrollExtent) { | |
if (controller.offset != controller.position.maxScrollExtent) { | |
controller.jumpTo(controller.position.maxScrollExtent); | |
} | |
} else { | |
controller.jumpTo(controller.offset + value.overscroll); | |
} | |
return true; | |
}, | |
child: ListView( | |
children: const [ | |
SimpleBloc(), | |
SimpleBloc(), | |
SimpleBloc(), | |
SimpleBloc(), | |
SimpleBloc(), | |
SimpleBloc(), | |
], | |
), | |
), | |
), | |
const SimpleBloc(), | |
const SimpleBloc(), | |
const SimpleBloc(), | |
], | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment