Advertisement
wildanfuady

Untitled

Oct 18th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'package:http/http.dart' as http;
  4. import 'package:flutter/material.dart';
  5. import 'package:sekolahku/project/viewSiswa.dart';
  6. import 'package:sekolahku/project/addSiswa.dart';
  7. import 'package:shared_preferences/shared_preferences.dart';
  8. import 'package:sekolahku/project/login2.dart';
  9.  
  10. class ListSiswa extends StatefulWidget{
  11. @override
  12. _ListSiswa createState() => _ListSiswa();
  13. }
  14.  
  15. class _ListSiswa extends State<ListSiswa>{
  16.  
  17. // refresh
  18. var refreshKey = GlobalKey<RefreshIndicatorState>();
  19.  
  20. @override
  21. void initState() {
  22. super.initState();
  23. getData();
  24. }
  25. // mengambil data dari database
  26. Future<List> getData() async {
  27. // refresh data
  28. refreshKey.currentState?.show(atTop: false);
  29. await Future.delayed(Duration(seconds: 1));
  30. // end refresh data
  31. final response = await http.get("https://flutterapp.ilmucoding.com/siswa/getsiswa.php");
  32. return json.decode(response.body);
  33. }
  34.  
  35. @override
  36. Widget build(BuildContext context){
  37. return new Scaffold(
  38. appBar: new AppBar(
  39. title: Text("Sekolahku"),
  40. leading: Icon(Icons.school),
  41. actions: <Widget>[
  42. IconButton(
  43. icon: Icon(Icons.lock),
  44. onPressed: () async {
  45. // hapus shared prefs login
  46. final prefs = await SharedPreferences.getInstance();
  47. prefs.remove('login');
  48. // redirect page/route ke login
  49. Navigator.push(
  50. context,
  51. MaterialPageRoute(builder: (context) => Login()),
  52. );
  53. },
  54. ),
  55. ],
  56. ),
  57. body: new RefreshIndicator(
  58. key: refreshKey,
  59. child: FutureBuilder<List>(
  60. future: getData(),
  61. builder: (context, snapshot){
  62. if(snapshot.hasError) print(snapshot.error);
  63. return snapshot.hasData
  64. ? new ItemList(
  65. list: snapshot.data,
  66. )
  67. : new Center(
  68. child: new CircularProgressIndicator(),
  69. );
  70. },
  71. ),
  72. onRefresh: getData,
  73. ),
  74. floatingActionButton: FloatingActionButton(
  75. child: Icon(Icons.add),
  76. onPressed: ()=> Navigator.of(context).push(
  77. MaterialPageRoute(
  78. builder: (BuildContext context) => AddSiswa(),
  79. ),
  80. ),
  81. ),
  82. );
  83. }
  84. }
  85.  
  86. class ItemList extends StatelessWidget{
  87. final List list;
  88. ItemList({this.list});
  89. @override
  90. Widget build(BuildContext context){
  91. return new ListView.builder(
  92. itemCount: list == null ? 0 : list.length,
  93. itemBuilder: (context, i){
  94. return new Container (
  95. padding: const EdgeInsets.all(5.0),
  96. child: new GestureDetector(
  97. // menuju halaman detail siswa menggunakan page route
  98. onTap: () => Navigator.of(context).pushReplacement(
  99. new MaterialPageRoute(
  100. builder: (BuildContext context) => ViewSiswa(list:list, index: i,)
  101. ),
  102. ),
  103. // onTap:
  104. child: Card(
  105. child: new ListTile(
  106. title: Text(list[i]['nama']),
  107. leading: Icon(Icons.person),
  108. subtitle: Text("Usia : ${list[i]['usia']} tahun"),
  109. )
  110. ),
  111. ),
  112. );
  113. },
  114. );
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement