Skip to content

Instantly share code, notes, and snippets.

@ShivamGoyal1899

ShivamGoyal1899/main.dart

Last active Jul 15, 2019
Embed
What would you like to do?
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
You can’t perform that action at this time.