Advertisement
andresual

KursPage2

Mar 19th, 2020
691
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 9.13 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:shared_preferences/shared_preferences.dart';
  3. import 'package:loading/loading.dart';
  4. import 'package:loading/indicator/ball_pulse_indicator.dart';
  5. import 'package:pull_to_refresh/pull_to_refresh.dart';
  6.  
  7. import 'package:http/http.dart' as http;
  8. import 'dart:async';
  9. import 'dart:convert';
  10.  
  11. class KursPage extends StatefulWidget {
  12.   @override
  13.   _KursPageState createState() => _KursPageState();
  14. }
  15.  
  16. class _KursPageState extends State<KursPage> {
  17.  
  18.   //UIs
  19.   bool loading = true;
  20.   bool updateCounter = true;
  21.  
  22.   //Variables
  23.   String baseUrl, serverCode;
  24.   Map data;
  25.   String kursSekarang, kursPembayaran, kursSekarangTanggal, kursPembayaranTanggal;
  26.  
  27.   //Datasets
  28.   List kursList;
  29.  
  30.   RefreshController _refreshController = RefreshController(initialRefresh: false);
  31.  
  32.   @override
  33.   void initState() {
  34.     // TODO: implement initState
  35.     super.initState();
  36.     getServerUrl();
  37.   }
  38.  
  39.   @override
  40.   Widget build(BuildContext context) {
  41.  
  42.     final body = Container(
  43.         width: MediaQuery.of(context).size.width,
  44.         padding: EdgeInsets.all(8.0),
  45.         decoration: BoxDecoration(
  46.             color: Colors.grey[300]
  47.         ),
  48.         child: (loading) ?
  49.         Center(
  50.             child: Column(
  51.                 children: <Widget>[
  52.                   Padding(
  53.                     padding: EdgeInsets.only(top: MediaQuery.of(context).size.height/2.2),
  54.                   ),
  55.                   Loading(
  56.                     indicator: BallPulseIndicator(), size: 50.0, color: Colors.lightBlue,
  57.                   ),
  58.                   Text("Memuat Data")
  59.                 ]
  60.             )
  61.         ) : SmartRefresher(
  62.             enablePullDown: true,
  63.             enablePullUp: false,
  64.             header: WaterDropHeader(waterDropColor: Colors.lightBlue),
  65.             footer: CustomFooter(
  66.               builder: (BuildContext context, LoadStatus mode){
  67.                 Widget body ;
  68.                 if(mode==LoadStatus.noMore){
  69.                   body = Text("No more Data");
  70.                 }
  71.                 else if(mode==LoadStatus.loading){
  72.                   body = Loading(
  73.                     indicator: BallPulseIndicator(), size: 50.0, color: Colors.lightBlue,
  74.                   );
  75.                 }
  76.                 else if(mode == LoadStatus.failed){
  77.                   body = Text("Load Failed!Click retry!");
  78.                 }
  79.                 else if(mode == LoadStatus.canLoading){
  80.                   body = Text("release to load more");
  81.                 }
  82.                 else{
  83.                   body =  Text("pull up load");
  84.                 }
  85.                 return Container(
  86.                   height: 55.0,
  87.                   child: Center(child:body),
  88.                 );
  89.               },
  90.             ),
  91.             controller: _refreshController,
  92.             onRefresh: _onRefresh,
  93.             onLoading: _onLoading,
  94.             child: (kursList == null) ?
  95.             Center(
  96.                 child: Column(
  97.                     mainAxisAlignment: MainAxisAlignment.center,
  98.                     children: <Widget>[
  99.                       Text("Tidak ada rekam jejak kurs.", style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold))
  100.                     ]
  101.                 )
  102.             ) :
  103.             ListView.builder(
  104.                 itemCount: (kursList == null) ? 0 : kursList.length,
  105.                 itemBuilder: (BuildContext context, int index) {
  106.                   return InkWell(
  107.                       highlightColor: Colors.white.withAlpha(30),
  108.                       splashColor: Colors.white.withAlpha(20),
  109.                       child: Card(
  110.                           margin: EdgeInsets.all(3.0),
  111.                           elevation: 3,
  112.                           shape: RoundedRectangleBorder(
  113.                               borderRadius: BorderRadius.circular(7.0)
  114.                           ),
  115.                           child: Padding(
  116.                               padding: EdgeInsets.all(2.0),
  117.                               child: ListTile(
  118.                                   title: Row(
  119.                                       mainAxisAlignment: MainAxisAlignment.spaceBetween,
  120.                                       children: <Widget>[
  121.                                         Text("${kursList[index]["kurs_mata_uang"]}",
  122.                                             style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold)
  123.                                         ),
  124.                                         Row(
  125.                                             mainAxisAlignment: MainAxisAlignment.spaceBetween,
  126.                                             children: <Widget>[
  127.                                               Padding(
  128.                                                   padding: EdgeInsets.only(right: 5.0),
  129.                                               ),
  130.                                             ]
  131.                                         )
  132.                                       ]
  133.                                   ),
  134.                                   subtitle: Column(
  135.                                     mainAxisAlignment: MainAxisAlignment.spaceBetween,
  136.                                     children: <Widget>[
  137.                                       Row(
  138.                                           mainAxisAlignment: MainAxisAlignment.spaceBetween,
  139.                                           children: <Widget>[
  140.                                             Text("Kurs Sekarang \nRp.${kursList[index]["kurs_sekarang"]}"),
  141.                                             Text("Kurs Pembayaran \nRp.${kursList[index]["kurs_pembayaran"]}"),
  142.                                           ]
  143.                                       ),
  144.                                       SizedBox(height: 10.0),
  145.                                       Row(
  146.                                           mainAxisAlignment: MainAxisAlignment.spaceBetween,
  147.                                           children: <Widget>[
  148.                                             Text("${kursList[index]["kurs_sekarang_tgl"]}"),
  149.                                             Text("${kursList[index]["kurs_pembayaran_tgl"]}"),
  150.                                           ]
  151.                                       ),
  152.                                       Row(
  153.                                           mainAxisAlignment: MainAxisAlignment.spaceBetween,
  154.                                           children: <Widget>[
  155.                                           ]
  156.                                       )
  157.                                     ],
  158.                                   )
  159.                               )
  160.                           )
  161.                       )
  162.                   );
  163.                 }
  164.             )
  165.         )
  166.     );
  167.  
  168.     return Scaffold(
  169.       backgroundColor: Colors.grey[200],
  170.       appBar: new AppBar(
  171.         title: const Text('List Kurs'),
  172.         elevation: 3,
  173.       ),
  174.       body: body
  175.     );
  176.   }
  177.  
  178.   Future getServerUrl() async {
  179.     SharedPreferences prefs = await SharedPreferences.getInstance();
  180.     setState(() {
  181.       if (prefs.getString("login_jaringan") == "Lokal"){
  182.         baseUrl = "http://192.168.1.200/";
  183.       } else {
  184.         baseUrl = "http://koffiesoftmjw.ngrok.io/";
  185.       }
  186.  
  187.       if (prefs.getString("login_cabang") == 'MJWJKT'){
  188.         serverCode = "/koffie_mjw/index.php?c=c_kurs&m=get_action";
  189.       } else if(prefs.getString("login_cabang") == 'MJWSBY'){
  190.         serverCode = "/koffie_mjw_sby/index.php?c=c_kurs&m=get_action";
  191.       } else {
  192.         serverCode = "/koffie_mjw_testing/index.php?c=c_kurs&m=get_action";
  193.       }
  194.  
  195.       getData();
  196.     });
  197.   }
  198.  
  199.   Future getData() async {
  200.     http.Response response = await http.post("$baseUrl$serverCode", body: {
  201.       "task"    : "LIST",
  202.       "query"   : "",
  203.       "type"    : "app",
  204.       "start"   : "0",
  205.       "limit"   : "100"
  206.     });
  207.  
  208.     if (response.statusCode == 200) {
  209.       data = json.decode(response.body);
  210.       print(data.toString());
  211.  
  212.       if (data["total"] == "0") {
  213.         kursList = null;
  214.       } else {
  215.         kursList = data["results"];
  216.         for (var i = 0; i < json.decode(response.body)["results"].length; i++) {
  217.           kursSekarang = json.decode(response.body)["results"][i]["kurs_sekarang"];
  218.           kursPembayaran = json.decode(response.body)["results"][i]["kurs_pembayaran"];
  219.           kursSekarangTanggal = json.decode(response.body)["results"][i]["kurs_sekarang_tgl"];
  220.           kursPembayaranTanggal = json.decode(response.body)["results"][i]["kurs_pembayaran_tgl"];
  221.         }
  222.  
  223.         print(kursSekarangTanggal);
  224.  
  225.         setState(() {
  226.           loading = false;
  227.         });
  228.       }
  229.     } else {
  230.       return null;
  231.     }
  232.   }
  233.  
  234.   void _onRefresh() async{
  235.     await Future.delayed(Duration(milliseconds: 1000));
  236.     // if failed,use refreshFailed()
  237.     _refreshController.refreshCompleted();
  238.  
  239.     getData();
  240.   }
  241.  
  242.   void _onLoading() async{
  243.     // monitor network fetch
  244.     await Future.delayed(Duration(milliseconds: 1000));
  245.     // if failed,use loadFailed(),if no data return,use LoadNodata()
  246.     setState(() {});
  247.  
  248.     _refreshController.loadComplete();
  249.   }
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement