import 'package:flutter/material.dart';
main() => runApp(KSJMyApp());
class KSJMyAppextends StatelessWidget {
@override
Widgetbuild(BuildContext context) {
return MaterialApp(
home:MyApp(),
);
}
}
/*
* 两种方式可以监听
* controller:
* 1.可以设置默认滚动值
* 2.监听滚动
* 3.但是不能监听开始和结束滚动
* NotificationListener:
*
* */
class MyAppextends StatefulWidget {
@override
_MyAppStatecreateState() =>_MyAppState();
}
class _MyAppStateextends State {
ScrollControllercontroller =ScrollController(initialScrollOffset:100);
@override
void initState() {
// TODO: implement initState
super.initState();
controller.addListener(() {
print('控制器监听滚动距离${controller.offset}');
});
}
@override
Widgetbuild(BuildContext context) {
return Scaffold(
appBar:AppBar(
title:Text('KSJ'),
),
body:NotificationListener(
// 监听全程滚动到方法
onNotification: (ScrollNotification notification) {
if (notificationis ScrollStartNotification) {
print("开始滚动...1");
}else if (notificationis ScrollUpdateNotification) {
print(
"正在滚动...2:当前滚动比例值${notification.metrics.pixels / notification.metrics.maxScrollExtent}");
print("正在滚动...2:当前所在点${notification.metrics.pixels}");
print("正在滚动...2:总滚动距离${notification.metrics.maxScrollExtent}");
}else if (notificationis ScrollEndNotification) {
print("结束滚动...3");
}
// 阻止冒泡
return false;
},
child:ListView.builder(
controller:controller,
itemCount:100,
itemBuilder: (BuildContext context, int index) {
return ListTile(
leading:Icon(Icons.people),
title:Text("联系人$index"),
);
}),
),
floatingActionButton:FloatingActionButton(
child:Icon(Icons.add),
onPressed: () {
// controller.jumpTo(0);
controller.animateTo(0,
duration:Duration(seconds:1), curve: Curves.easeIn);
}),
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
);
}
@override
void dispose() {
// TODO: implement dispose
super.dispose();
controller.dispose();
}
}
网友评论