Skip to content

Instantly share code, notes, and snippets.

@zmtzawqlp
Created December 27, 2018 09:57
Show Gist options
  • Save zmtzawqlp/2c055e734c8cb61eaf7e734f34c5c83b to your computer and use it in GitHub Desktop.
Save zmtzawqlp/2c055e734c8cb61eaf7e734f34c5c83b to your computer and use it in GitHub Desktop.
PullToRefreshHeader
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:pull_to_refresh_notification/pull_to_refresh_notification.dart';
class PullToRefreshHeader extends StatefulWidget {
@override
_PullToRefreshHeaderState createState() => _PullToRefreshHeaderState();
}
class _PullToRefreshHeaderState extends State<PullToRefreshHeader> {
int listlength = 5;
@override
Widget build(BuildContext context) {
return PullToRefreshNotification(
color: Colors.blue,
onRefresh: onRefresh,
maxDragOffset: 80.0,
child: CustomScrollView(
slivers: <Widget>[
SliverAppBar(
pinned: true,
title: Text("PullToRefreshHeader"),
),
PullToRefreshContainer(buildPulltoRefreshHeader),
SliverList(
delegate:
SliverChildBuilderDelegate((BuildContext context, int index) {
return Container(
padding: EdgeInsets.only(bottom: 4.0),
child: Column(
children: <Widget>[
Text(
"List item : ${listlength - index}",
style: TextStyle(fontSize: 15.0, inherit: false),
),
Divider(
color: Colors.grey,
height: 2.0,
)
],
));
}, childCount: listlength)),
SliverList(
delegate:
SliverChildBuilderDelegate((BuildContext context, int index) {
return Container(
padding: EdgeInsets.only(bottom: 4.0),
child: Column(
children: <Widget>[
Text(
"List item : ${listlength - index}",
style: TextStyle(fontSize: 15.0, inherit: false),
),
Divider(
color: Colors.grey,
height: 2.0,
)
],
));
}, childCount: listlength)),
SliverGrid(
gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
delegate:
SliverChildBuilderDelegate((BuildContext context, int index) {
return Container(
padding: EdgeInsets.only(bottom: 4.0),
child: Column(
children: <Widget>[
Text(
"List item : ${listlength - index}",
style: TextStyle(fontSize: 15.0, inherit: false),
),
Divider(
color: Colors.grey,
height: 2.0,
)
],
));
}, childCount: listlength)),
],
),
);
}
Widget buildPulltoRefreshHeader(PullToRefreshScrollNotificationInfo info) {
//print(info?.mode);
//print(info?.dragOffset);
// print("------------");
var offset = info?.dragOffset ?? 0.0;
var mode = info?.mode;
Widget refreshWiget = Container();
//it should more than 18, so that RefreshProgressIndicator can be shown fully
if (info?.refreshWiget != null &&
offset > 18.0 &&
mode != RefreshIndicatorMode.error) {
refreshWiget = info.refreshWiget;
}
Widget child = null;
if (mode == RefreshIndicatorMode.error) {
child = GestureDetector(
onTap: () {
// refreshNotification;
info?.pullToRefreshNotificationState?.show();
},
child: Container(
color: Colors.grey,
alignment: Alignment.bottomCenter,
height: offset,
width: double.infinity,
//padding: EdgeInsets.only(top: offset),
child: Container(
padding: EdgeInsets.only(left: 5.0),
alignment: Alignment.center,
child: Text(
mode?.toString() + " click to retry" ?? "",
style: TextStyle(fontSize: 12.0, inherit: false),
),
),
));
} else {
child = Container(
color: Colors.grey,
alignment: Alignment.bottomCenter,
height: offset,
width: double.infinity,
//padding: EdgeInsets.only(top: offset),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
refreshWiget,
Container(
padding: EdgeInsets.only(left: 5.0),
alignment: Alignment.center,
child: Text(
mode?.toString() ?? "",
style: TextStyle(fontSize: 12.0, inherit: false),
),
)
],
),
);
}
return SliverToBoxAdapter(
child: child,
);
}
bool success = false;
Future<bool> onRefresh() {
final Completer<bool> completer = new Completer<bool>();
new Timer(const Duration(seconds: 2), () {
completer.complete(success);
success = true;
});
return completer.future.then((bool success) {
if (success) {
setState(() {
listlength += 10;
});
}
return success;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment