Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:async';
- import 'dart:convert';
- import 'package:http/http.dart' as http;
- import 'package:flutter/material.dart';
- import 'package:sekolahku/project/viewSiswa.dart';
- import 'package:sekolahku/project/addSiswa.dart';
- import 'package:shared_preferences/shared_preferences.dart';
- import 'package:sekolahku/project/login2.dart';
- class ListSiswa extends StatefulWidget{
- @override
- _ListSiswa createState() => _ListSiswa();
- }
- class _ListSiswa extends State<ListSiswa>{
- // refresh
- var refreshKey = GlobalKey<RefreshIndicatorState>();
- @override
- void initState() {
- super.initState();
- getData();
- }
- // mengambil data dari database
- Future<List> getData() async {
- // refresh data
- refreshKey.currentState?.show(atTop: false);
- await Future.delayed(Duration(seconds: 1));
- // end refresh data
- final response = await http.get("https://flutterapp.ilmucoding.com/siswa/getsiswa.php");
- return json.decode(response.body);
- }
- @override
- Widget build(BuildContext context){
- return new Scaffold(
- appBar: new AppBar(
- title: Text("Sekolahku"),
- leading: Icon(Icons.school),
- actions: <Widget>[
- IconButton(
- icon: Icon(Icons.lock),
- onPressed: () async {
- // hapus shared prefs login
- final prefs = await SharedPreferences.getInstance();
- prefs.remove('login');
- // redirect page/route ke login
- Navigator.push(
- context,
- MaterialPageRoute(builder: (context) => Login()),
- );
- },
- ),
- ],
- ),
- body: new RefreshIndicator(
- key: refreshKey,
- child: FutureBuilder<List>(
- future: getData(),
- builder: (context, snapshot){
- if(snapshot.hasError) print(snapshot.error);
- return snapshot.hasData
- ? new ItemList(
- list: snapshot.data,
- )
- : new Center(
- child: new CircularProgressIndicator(),
- );
- },
- ),
- onRefresh: getData,
- ),
- floatingActionButton: FloatingActionButton(
- child: Icon(Icons.add),
- onPressed: ()=> Navigator.of(context).push(
- MaterialPageRoute(
- builder: (BuildContext context) => AddSiswa(),
- ),
- ),
- ),
- );
- }
- }
- class ItemList extends StatelessWidget{
- final List list;
- ItemList({this.list});
- @override
- Widget build(BuildContext context){
- return new ListView.builder(
- itemCount: list == null ? 0 : list.length,
- itemBuilder: (context, i){
- return new Container (
- padding: const EdgeInsets.all(5.0),
- child: new GestureDetector(
- // menuju halaman detail siswa menggunakan page route
- onTap: () => Navigator.of(context).pushReplacement(
- new MaterialPageRoute(
- builder: (BuildContext context) => ViewSiswa(list:list, index: i,)
- ),
- ),
- // onTap:
- child: Card(
- child: new ListTile(
- title: Text(list[i]['nama']),
- leading: Icon(Icons.person),
- subtitle: Text("Usia : ${list[i]['usia']} tahun"),
- )
- ),
- ),
- );
- },
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement