Last active
July 15, 2019 12:06
-
-
Save ShivamGoyal1899/49de5b5f62da49ca15fb0836edb094ef to your computer and use it in GitHub Desktop.
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 'dart:async'; | |
import 'dart:math'; | |
import 'package:flutter/material.dart'; | |
void main() => runApp(PullToRefresh()); | |
class PullToRefresh extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
debugShowCheckedModeBanner: false, | |
title: 'Pull To Refresh', | |
home: HomePage(), | |
); | |
} | |
} | |
class HomePage extends StatefulWidget { | |
@override | |
_HomePageState createState() => _HomePageState(); | |
} | |
class _HomePageState extends State<HomePage> { | |
var list; | |
var random; | |
var refreshKey = GlobalKey<RefreshIndicatorState>(); | |
@override | |
void initState() { | |
list = List.generate(3, (i) => "Item $i"); | |
super.initState(); | |
random = Random(); | |
refreshList(); | |
} | |
Future<Null> refreshList() async { | |
refreshKey.currentState?.show(atTop: false); | |
await Future.delayed(Duration(seconds: 2)); | |
setState(() { | |
list = List.generate(random.nextInt(10), (i) => "Item $i"); | |
}); | |
return null; | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text("Pull to refresh"), | |
), | |
body: RefreshIndicator( | |
key: refreshKey, | |
child: ListView.builder( | |
itemCount: list?.length, | |
itemBuilder: (context, i) => ListTile( | |
title: Text(list[i]), | |
), | |
), | |
onRefresh: refreshList, | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment