maj*_*i69 8 android dart flutter
我正在使用ListView
小部件将项目显示为列表。在三个窗口中,查看项目必须将中间项目放置在中间。
那么,如何ListView
在滚动停止时检测位置?
如何检测ListView滚动停止?
Peg*_*sis 22
majidfathi69 的回答很好,但您不需要在列表中添加控制器:(更改ScrollUpdateNotification
为ScrollEndNotification
仅在滚动结束时收到通知时。)
NotificationListener<ScrollUpdateNotification>(
child: ListView(
children: ...
),
onNotification: (notification) {
//How many pixels scrolled from pervious frame
print(notification.scrollDelta);
//List scroll position
print(notification.metrics.pixels);
},
),
Run Code Online (Sandbox Code Playgroud)
maj*_*i69 14
我使用的NotificationListener
是一个小部件,用于监听气泡冒泡的通知。然后使用ScrollEndNotification
,它指示滚动已停止。
对于滚动位置,我使用的_scrollController
类型是ScrollController
。
new NotificationListener(
child: new ListView(
controller: _scrollController,
children: ...
),
onNotification: (t) {
if (t is ScrollEndNotification) {
print(_scrollController.position.pixels);
}
},
),
Run Code Online (Sandbox Code Playgroud)
Sed*_*ush 11
您还可以通过以下步骤实现此功能
import 'package:flutter/material.dart';
class YourPage extends StatefulWidget {
YourPage({Key key}) : super(key: key);
@override
_YourPageState createState() => _YourPageState();
}
class _YourPageState extends State<YourPage> {
ScrollController _scrollController;
double _scrollPosition;
_scrollListener() {
setState(() {
_scrollPosition = _scrollController.position.pixels;
});
}
@override
void initState() {
_scrollController = ScrollController();
_scrollController.addListener(_scrollListener);
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false,
title: Text('Position $_scrollPosition pixels'),
),
body: Container(
child: ListView.builder(
controller: _scrollController,
itemCount: 200,
itemBuilder: (context, index) {
return ListTile(
leading: Icon(Icons.mood),
title: Text('Item: $index'),
);
},
)),
);
}
}
Run Code Online (Sandbox Code Playgroud)
的NotificationListener
现在接受型参数,它使得代码短:)
NotificationListener<ScrollEndNotification>(
child: ListView(
controller: _scrollController,
children: ...
),
onNotification: (notification) {
print(_scrollController.position.pixels);
// Return true to cancel the notification bubbling. Return false (or null) to
// allow the notification to continue to be dispatched to further ancestors.
return true;
},
),
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6018 次 |
最近记录: |