Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 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. int _currentIndex = 0;
  18. final List<Widget> _children = [];
  19. // refresh
  20. var refreshKey = GlobalKey<RefreshIndicatorState>();
  21.  
  22. @override
  23. void initState() {
  24. super.initState();
  25. getData();
  26. }
  27.  
  28. void onTabTapped(int index) {
  29. setState(() {
  30. _currentIndex = index;
  31. });
  32.  
  33. }
  34.  
  35. // mengambil data dari database
  36. Future<List> getData() async {
  37. // refresh data
  38. refreshKey.currentState?.show(atTop: false);
  39. await Future.delayed(Duration(seconds: 1));
  40. // end refresh data
  41. final response = await http.get("https://f********.com/siswa/getsiswa.php");
  42. return json.decode(response.body);
  43. }
  44.  
  45. @override
  46. Widget build(BuildContext context){
  47. return new Scaffold(
  48. appBar: new AppBar(
  49. title: Text("Sekolahku"),
  50. leading: Icon(Icons.school),
  51. actions: <Widget>[
  52. IconButton(
  53. icon: Icon(Icons.lock_open, semanticLabel: "Logout",),
  54.  
  55. onPressed: () async {
  56. // hapus shared prefs login
  57. final prefs = await SharedPreferences.getInstance();
  58. prefs.remove('login');
  59. // redirect page/route ke login
  60. Navigator.push(
  61. context,
  62. MaterialPageRoute(builder: (context) => Login()),
  63. );
  64. },
  65. ),
  66. ],
  67. ),
  68. body: new RefreshIndicator(
  69. key: refreshKey,
  70. child: FutureBuilder<List>(
  71. future: getData(),
  72. builder: (context, snapshot){
  73. if(snapshot.hasError) print(snapshot.error);
  74. return snapshot.hasData
  75. ? new ItemList(
  76. list: snapshot.data,
  77. )
  78. : new Center(
  79. child: new CircularProgressIndicator(),
  80. );
  81. },
  82. ),
  83. onRefresh: getData,
  84. ),
  85. floatingActionButton: FloatingActionButton(
  86. child: Icon(Icons.add),
  87. onPressed: ()=> Navigator.of(context).push(
  88. MaterialPageRoute(
  89. builder: (BuildContext context) => AddSiswa(),
  90. ),
  91. ),
  92. ),
  93. bottomNavigationBar: BottomNavigationBar(
  94. onTap: onTabTapped, // new
  95. currentIndex: _currentIndex, // new
  96. items: [
  97. new BottomNavigationBarItem(
  98. icon: Icon(Icons.home),
  99. title: Text('Home'),
  100. ),
  101. new BottomNavigationBarItem(
  102. icon: Icon(Icons.person),
  103. title: Text('Data Siswa'),
  104. ),
  105. new BottomNavigationBarItem(
  106. icon: Icon(Icons.lock_open),
  107. title: Text('Logout')
  108. )
  109. ],
  110. ),
  111. );
  112. }
  113. }
  114.  
  115. class ItemList extends StatelessWidget{
  116. final List list;
  117. ItemList({this.list});
  118. @override
  119. Widget build(BuildContext context){
  120. return new ListView.builder(
  121. itemCount: list == null ? 0 : list.length,
  122. itemBuilder: (context, i){
  123. return new Container (
  124. padding: const EdgeInsets.all(5.0),
  125. child: new GestureDetector(
  126. // menuju halaman detail siswa menggunakan page route
  127. onTap: () => Navigator.of(context).pushReplacement(
  128. new MaterialPageRoute(
  129. builder: (BuildContext context) => ViewSiswa(list:list, index: i,)
  130. ),
  131. ),
  132. // onTap:
  133. child: Card(
  134. child: new ListTile(
  135. title: Text(list[i]['nama']),
  136. leading: Icon(Icons.person),
  137. subtitle: Text("Usia : ${list[i]['usia']} tahun"),
  138. )
  139. ),
  140. ),
  141. );
  142. },
  143. );
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement