Last active
September 16, 2021 15:54
-
-
Save DoodleBears/f71b23acbd335205375b5dc07d369d0a to your computer and use it in GitHub Desktop.
ScrollMetrics (Notification)
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 'package:flutter/material.dart'; | |
void main() => runApp(const ScrollMetricsDemo()); | |
class ScrollMetricsDemo extends StatefulWidget { | |
const ScrollMetricsDemo({Key? key}) : super(key: key); | |
@override | |
State<ScrollMetricsDemo> createState() => ScrollMetricsDemoState(); | |
} | |
class ScrollMetricsDemoState extends State<ScrollMetricsDemo> { | |
int itemCount = 9; | |
final ScrollController scrollController = | |
ScrollController(keepScrollOffset: false); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
appBar: AppBar( | |
title: const Text('ScrollMetrics Demo'), | |
), | |
floatingActionButton: | |
Row(mainAxisAlignment: MainAxisAlignment.end, children: [ | |
FloatingActionButton( | |
child: const Icon(Icons.add), | |
onPressed: () => setState(() { | |
itemCount += 1; | |
}), | |
), | |
const SizedBox(width: 20.0), | |
FloatingActionButton( | |
child: const Icon(Icons.remove), | |
onPressed: () => setState(() { | |
itemCount -= 1; | |
}), | |
), | |
]), | |
body: NotificationListener<ScrollMetricsNotification>( | |
onNotification: (ScrollMetricsNotification notification) { | |
// ScaffoldMessenger.of(notification.context).showSnackBar( | |
// const SnackBar( | |
// content: Text('Scroll metrics changed!'), | |
// ), | |
// ); | |
print('Scroll metrics changed! itemCount = $itemCount '); | |
return false; | |
}, | |
child: Scrollbar( | |
isAlwaysShown: true, | |
child: ListView.builder( | |
shrinkWrap: true, | |
itemCount: itemCount, | |
// 由外层 SingleChildScrollView 来控制滚动 | |
physics: const NeverScrollableScrollPhysics(), | |
itemBuilder: (context, index) { | |
return ListTile( | |
title: Text('Tile ${index+1}'), | |
); | |
}, | |
), | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment