Created
April 1, 2020 17:23
-
-
Save teerasej/6c4f0d8eb763b201efdc7b1f62b88121 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 'package:flutter/material.dart'; | |
import 'detail_page.dart'; | |
import 'contact_model.dart'; | |
import 'package:http/http.dart' as http; | |
import 'dart:convert'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: MyHomePage(), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
List numberList = ['a', 'b', 'c', 'd', 'e', 'f']; | |
RandomUserResult contactResult = new RandomUserResult(); | |
void getData() async { | |
final response = await http.get('https://randomuser.me/api/?results=50'); | |
print(response.body); | |
var randomUserResult = | |
RandomUserResult.fromJson(json.decode(response.body)); | |
setState(() { | |
contactResult = randomUserResult; | |
}); | |
} | |
@override | |
void initState() { | |
super.initState(); | |
getData(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Contact'), | |
), | |
body: ListView.builder( | |
itemCount: contactResult.results.length, | |
itemBuilder: (BuildContext context, int index) { | |
var contact = contactResult.results[index]; | |
return ListTile( | |
leading: Image.network( | |
contact.picture.large, | |
width: 50.0, | |
height: 50.0, | |
fit: BoxFit.scaleDown, | |
), | |
title: Text( | |
"${contact.name.first} ${contact.name.last}", | |
style: TextStyle(fontWeight: FontWeight.w700), | |
), | |
subtitle: Text( | |
contact.phone, | |
style: TextStyle(color: Colors.grey), | |
), | |
onTap: () { | |
Navigator.push(context, | |
MaterialPageRoute(builder: (context) => DetailPage(contact))); | |
}, | |
); | |
}, | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment