Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'dart:async';
  3. import 'dart:convert';
  4. import 'package:http/http.dart' as http;
  5.  
  6.  
  7. // class response json
  8. class Response {
  9. final String status;
  10. final String message;
  11.  
  12. Response({this.status, this.message});
  13.  
  14. factory Response.fromJson(Map<String, dynamic> json) {
  15. return Response(
  16. status: json['status'],
  17. message: json['message'],
  18. );
  19. }
  20. }
  21.  
  22. // fungsi untuk kirim http post
  23. Future<Response> post(String url,var body)async{
  24. return await http
  25. .post(Uri.encodeFull(url), body: body, headers: {"Accept":"application/json"})
  26. .then((http.Response response) {
  27.  
  28. final int statusCode = response.statusCode;
  29.  
  30. if (statusCode < 200 || statusCode > 400 || json == null) {
  31. throw new Exception("Error while fetching data");
  32. }
  33. return Response.fromJson(json.decode(response.body));
  34. });
  35. }
  36.  
  37. class Login extends StatefulWidget {
  38. @override
  39. LoginState createState() => new LoginState();
  40. }
  41.  
  42. class LoginState extends State<Login> {
  43.  
  44. // variabel member class
  45. final _username = TextEditingController();
  46. final _password = TextEditingController();
  47.  
  48. // member response
  49. String _response = '';
  50. bool _apiCall = false;
  51.  
  52. // fungsi panggil API
  53. void _callPostAPI() {
  54. post(
  55. 'http://192.3.168.178/restapi/login.php',
  56. {
  57. 'username': _username.text,
  58. 'password': _password.text
  59. }).then((response) {
  60. // jika respon normal
  61. setState(() {
  62. _apiCall = false;
  63. _response = response.message;
  64. });
  65.  
  66. },
  67. // jika respon error
  68. onError: (error) {
  69. _apiCall = false;
  70. _response = error.toString();
  71. }
  72. );
  73. }
  74.  
  75. Widget progressWidget() {
  76. if (_apiCall)
  77. // jika masih proses kirim API
  78. return AlertDialog(
  79. content: new Column(
  80. children: <Widget>[
  81. CircularProgressIndicator(),
  82. Text("Please wait")
  83. ],
  84. ),
  85. );
  86. else
  87. // jika sudah selesai kirim API
  88. return Center(
  89. child: Text(
  90. _response,
  91. style: new TextStyle(fontSize: 15.0),
  92. ),
  93. );
  94. }
  95.  
  96. @override
  97. Widget build(BuildContext context) {
  98. return new Scaffold(
  99. appBar: new AppBar(
  100. title: const Text("Welcome to Flutter"),
  101. ),
  102. body: SafeArea(
  103. child: ListView(
  104. padding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
  105. children: <Widget>[
  106. // nama
  107. TextField(
  108. controller: _username,
  109. decoration: InputDecoration(
  110. filled: true,
  111. labelText: 'Username'
  112. ),
  113. ),
  114. // spasi
  115. SizedBox(height: 5.0),
  116. // alamat
  117. TextField(
  118. controller: _password,
  119. decoration: InputDecoration(
  120. filled: true,
  121. labelText: 'Password'
  122. ),
  123. obscureText: true,
  124. ),
  125. // spasi
  126. SizedBox(height: 10.0),
  127. // tombol
  128. RaisedButton(
  129. child: Text('LOGIN'),
  130. onPressed: () {
  131.  
  132. setState(() {
  133. _apiCall = true;
  134. });
  135. _callPostAPI();
  136. /*
  137. return showDialog(
  138. context: context,
  139. builder: (context) {
  140. return AlertDialog(
  141. content: Text('Username: ${_username.text}\nPassword: ${_password.text}'),
  142. );
  143. },
  144. );
  145. */
  146. },
  147. ),
  148.  
  149. // panggil loading widget
  150. progressWidget()
  151. ],
  152. )
  153. ),
  154. );
  155. }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement