Skip to content

Instantly share code, notes, and snippets.

@zmtzawqlp
Created May 11, 2020 10:14
Show Gist options
  • Save zmtzawqlp/e0290638f5d5a697a51c3410fda948c9 to your computer and use it in GitHub Desktop.
Save zmtzawqlp/e0290638f5d5a697a51c3410fda948c9 to your computer and use it in GitHub Desktop.
pull down refresh before
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: PullDownRefresh(),
);
}
}
class PullDownRefresh extends StatefulWidget {
@override
_PullDownRefreshState createState() => _PullDownRefreshState();
}
class _PullDownRefreshState extends State<PullDownRefresh> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: createBody(),
);
}
int itemCount = 30;
int itemBefore = 0;
double beforeHeight = 0.0;
Key key = GlobalKey();
Widget createBody() {
return NotificationListener<ScrollNotification>(
onNotification: (value) {
if (value is OverscrollNotification &&
value.metrics.axisDirection == AxisDirection.down) {
if (beforeHeight == 30.0) return true;
setState(() {
beforeHeight = 30.0;
Future.delayed(Duration(seconds: 3), () {
setState(() {
beforeHeight = 0.0;
itemBefore += 10;
});
});
});
}
return true;
},
child: CustomScrollView(
slivers: <Widget>[
SliverToBoxAdapter(
child: Container(
alignment: Alignment.center,
child: Text('加载前天...'),
height: beforeHeight,
),
),
SliverList(
delegate: SliverChildBuilderDelegate(
(c, index) {
return Text('我是前天的数据$index');
},
childCount: itemBefore,
),
),
SliverList(
key: key,
delegate: SliverChildBuilderDelegate((c, index) {
return Text('center$index');
}, childCount: itemCount),
),
],
center: key,
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment