Created
January 6, 2021 06:41
-
-
Save mutant0113/1e7903d56bcd6955e4635d4e38e437a2 to your computer and use it in GitHub Desktop.
FlutterRefreshIndicatorCupertinoStyle
This file contains hidden or 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:math'; | |
import 'package:flutter/cupertino.dart'; | |
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 = 20; | |
List<int> randomNumbers; | |
@override | |
void initState() { | |
super.initState(); | |
randomNumbers = _randomNumbers; | |
} | |
@override | |
Widget build(BuildContext context) => CustomScrollView( | |
slivers: [ | |
CupertinoSliverRefreshControl( | |
onRefresh: _refreshRandomNumbers, | |
), | |
_sliverListView, | |
_sliverGridView, | |
], | |
); | |
SliverList get _sliverListView => SliverList(delegate: _sliverChildBuilderDelegate); | |
SliverGrid get _sliverGridView => SliverGrid( | |
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2, childAspectRatio: 4), | |
delegate: _sliverChildBuilderDelegate, | |
); | |
SliverChildBuilderDelegate get _sliverChildBuilderDelegate => SliverChildBuilderDelegate( | |
(context, index) => Text( | |
'number: ${randomNumbers[index]}', | |
style: TextStyle(fontSize: 20, height: 1.5), | |
), | |
childCount: _randomNumbersCount, | |
); | |
// 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