Skip to content

Instantly share code, notes, and snippets.

@RyouMon
Last active December 24, 2022 15:36
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save RyouMon/8100c57057ce12b710a3777f511a5be3 to your computer and use it in GitHub Desktop.
Flutter Demo: ScrollNotification Example
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class ScrollNotificationTest extends StatefulWidget {
const ScrollNotificationTest({super.key});
@override
State<ScrollNotificationTest> createState() => _ScrollNotificationTestState();
}
class _ScrollNotificationTestState extends State<ScrollNotificationTest> {
String progress = "0%";
ScrollController controller = ScrollController();
@override
Widget build(BuildContext context) {
return Scrollbar(
controller: controller,
child: NotificationListener<ScrollNotification>(
onNotification: (notification) {
setState(() {
progress =
"${(notification.metrics.pixels / notification.metrics.maxScrollExtent * 100).toInt()}%";
});
return false;
},
child: Stack(
alignment: Alignment.center,
children: [
ListView.builder(
controller: controller,
itemCount: 100,
itemExtent: 50.0,
itemBuilder: ((context, index) => ListTile(
title: Text("$index"),
))),
CircleAvatar(
radius: 30.0,
backgroundColor: Colors.black54,
child: Text(progress),
)
],
),
));
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key, required this.title});
final String title;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(title)),
body: const ScrollNotificationTest(),
);
}
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const HomePage(title: 'ScrollNotification Example'),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment