Created
January 18, 2020 19:51
-
-
Save maffan91/17c6061feb22f81d049858940f4a36c6 to your computer and use it in GitHub Desktop.
Pull to refresh in Flutter
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 'package:flutter/material.dart'; | |
import 'package:http/http.dart' as http; | |
import 'dart:convert'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Pull to Refresh Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: UserList(), | |
); | |
} | |
} | |
class UserList extends StatefulWidget { | |
@override | |
State<StatefulWidget> createState() { | |
return _UserListState(); | |
} | |
} | |
class _UserListState extends State<UserList> { | |
final String apiUrl = "https://randomuser.me/api/?results=10"; | |
List<dynamic> _users = []; | |
void fetchUsers() async { | |
var result = await http.get(apiUrl); | |
setState(() { | |
_users = json.decode(result.body)['results']; | |
}); | |
} | |
String _name(dynamic user) { | |
return user['name']['title'] + | |
" " + | |
user['name']['first'] + | |
" " + | |
user['name']['last']; | |
} | |
String _location(dynamic user) { | |
return user['location']['country']; | |
} | |
String _age(Map<dynamic, dynamic> user) { | |
return "Age: " + user['dob']['age'].toString(); | |
} | |
Widget _buildList() { | |
return _users.length != 0 | |
? RefreshIndicator( | |
child: ListView.builder( | |
padding: EdgeInsets.all(8), | |
itemCount: _users.length, | |
itemBuilder: (BuildContext context, int index) { | |
return Card( | |
child: Column( | |
children: <Widget>[ | |
ListTile( | |
leading: CircleAvatar( | |
radius: 30, | |
backgroundImage: NetworkImage( | |
_users[index]['picture']['large'])), | |
title: Text(_name(_users[index])), | |
subtitle: Text(_location(_users[index])), | |
trailing: Text(_age(_users[index])), | |
) | |
], | |
), | |
); | |
}), | |
onRefresh: _getData, | |
) | |
: Center(child: CircularProgressIndicator()); | |
} | |
Future<void> _getData() async { | |
setState(() { | |
fetchUsers(); | |
}); | |
} | |
@override | |
void initState() { | |
super.initState(); | |
fetchUsers(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('User List'), | |
), | |
body: Container( | |
child: _buildList(), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment