Last active
April 5, 2018 09:18
-
-
Save vin-the-dev/626b1fe37daf6ac7121a024f7a366105 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 'dart:async'; | |
import 'package:flutter/material.dart'; | |
import 'dart:convert'; | |
import 'package:http/http.dart' as http; | |
import 'package:shared_preferences/shared_preferences.dart'; | |
import 'service_entry.dart'; | |
class HomePage extends StatefulWidget { | |
static String tag = 'home-page'; | |
@override | |
_HomePageState createState() => new _HomePageState(); | |
} | |
class _HomePageState extends State<HomePage> { | |
List data = []; | |
Future<List> getData() async { | |
var response = await http.get( | |
Uri.encodeFull('https://jsonplaceholder.typicode.com/users'), | |
headers: {'Accept': 'application/json'}, | |
// body: lData, | |
); | |
print("object"); | |
// setState(() { | |
// data = json.decode(response.body); | |
// }); | |
return data; | |
} | |
_logout() async { | |
SharedPreferences prefs = await SharedPreferences.getInstance(); | |
prefs.clear(); | |
setState(() {}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
// final Welcome = Text('Welcome', style: TextStyle(fontSize: 30.0)); | |
Widget listView(List data) { | |
return ListView.builder( | |
itemCount: data.length, | |
itemBuilder: (context, rowNumber) { | |
return ListTile( | |
title: new Text(data[rowNumber]['name']), | |
subtitle: new Text( | |
data[rowNumber]['email'], | |
style: TextStyle(fontStyle: FontStyle.normal, fontSize: 15.0), | |
), | |
); | |
}); | |
} | |
return new Scaffold( | |
appBar: AppBar( | |
title: Text("Service List"), | |
actions: <Widget>[ | |
new IconButton( | |
icon: new Icon(Icons.exit_to_app), | |
onPressed: () { | |
print("logOut"); | |
_logout(); | |
}, | |
) | |
], | |
), | |
backgroundColor: Colors.white, | |
body: Center( | |
child: new FutureBuilder( | |
future: getData(), | |
builder: (context, snapshot) { | |
if (snapshot.hasData) { | |
return listView(snapshot.data); | |
} else if (snapshot.hasError) { | |
return Text("Error: ${snapshot.error}"); | |
} | |
return CircularProgressIndicator(); | |
}, | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
tooltip: 'Add', | |
child: Icon(Icons.add), | |
onPressed: () { | |
Navigator.of(context).pushNamed(ServiceEntry.tag); | |
}, | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment