Skip to content

Instantly share code, notes, and snippets.

@mutant0113
Last active December 12, 2020 08:49
Show Gist options
  • Save mutant0113/6843e9989b1149ca5bda42b815173351 to your computer and use it in GitHub Desktop.
Save mutant0113/6843e9989b1149ca5bda42b815173351 to your computer and use it in GitHub Desktop.
FlutterRefreshIndicator
import 'dart:math';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) => MaterialApp(
color: Colors.white,
home: Scaffold(
appBar: AppBar(
title: Text('Pull to refresh sample'),
),
body: MyHomePage()),
);
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
static int _randomNumbersCount = 100;
List<int> randomNumbers;
@override
void initState() {
super.initState();
randomNumbers = _randomNumbers;
}
@override
Widget build(BuildContext context) => RefreshIndicator(
onRefresh: _refreshRandomNumbers,
child: ListView.builder(
itemCount: _randomNumbersCount,
itemBuilder: (context, index) => Text(
'number: ${randomNumbers[index]}',
style: TextStyle(fontSize: 20, height: 1.5),
),
),
);
// Pretends we fetch the data from server and it takes time.
Future<void> _refreshRandomNumbers() => Future.delayed(Duration(seconds: 2), () {
setState(() => randomNumbers = _randomNumbers);
});
List<int> get _randomNumbers => List.generate(
_randomNumbersCount,
(index) => Random().nextInt(_randomNumbersCount),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment