Guest User

fullsourcecode.dart

a guest
Aug 19th, 2019
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.29 KB | None | 0 0
  1. import 'dart:io';
  2.  
  3. import 'package:flutter/material.dart';
  4. import 'package:page_transition/page_transition.dart';
  5. import 'package:mosque/api/api_mosque.dart';
  6. import 'package:mosque/main.dart';
  7.  
  8. class UpdateScreen extends StatefulWidget {
  9. final String idUpdate;
  10. final String nrpUpdate;
  11. final String namaUpdate;
  12. final String emailUpdate;
  13. final String jurusanUpdate;
  14. final String imageUpdate;
  15.  
  16. UpdateScreen({
  17. this.idUpdate,
  18. this.nrpUpdate,
  19. this.namaUpdate,
  20. this.emailUpdate,
  21. this.jurusanUpdate,
  22. this.imageUpdate,
  23. });
  24. @override
  25. _UpdateScreenState createState() => _UpdateScreenState();
  26. }
  27.  
  28. class _UpdateScreenState extends State<UpdateScreen> {
  29. final _scaffoldKey = GlobalKey<ScaffoldState>();
  30. bool _isLoadingButton = false;
  31. ApiHelper api = ApiHelper();
  32. File _image;
  33. TextEditingController _txtId;
  34. TextEditingController _txtNRP;
  35. TextEditingController _txtNama;
  36. TextEditingController _txtEmail;
  37. TextEditingController _txtJurusan;
  38. @override
  39. void initState() {
  40. _txtId = TextEditingController(text: "${widget.idUpdate}");
  41. _txtNRP = TextEditingController(text: "${widget.nrpUpdate}");
  42. _txtNama = TextEditingController(text: "${widget.namaUpdate}");
  43. _txtEmail = TextEditingController(text: "${widget.emailUpdate}");
  44. _txtJurusan = TextEditingController(text: "${widget.jurusanUpdate}");
  45. super.initState();
  46. }
  47.  
  48. @override
  49. Widget build(BuildContext context) {
  50. return Scaffold(
  51. key: _scaffoldKey,
  52. appBar: AppBar(
  53. actions: <Widget>[
  54. _isLoadingButton
  55. ? _loadingIndicator()
  56. : IconButton(
  57. icon: Icon(
  58. Icons.check_box,
  59. color: Colors.white,
  60. size: 32,
  61. ),
  62. onPressed: () {
  63. _validateTextField();
  64. },
  65. )
  66. ],
  67. leading: IconButton(
  68. icon: Icon(Icons.arrow_left),
  69. onPressed: () => Navigator.pop(context, false),
  70. ),
  71. centerTitle: true,
  72. title: Text('Update ${widget.namaUpdate}'),
  73. ),
  74. body: Container(
  75. child: SingleChildScrollView(
  76. scrollDirection: Axis.vertical,
  77. padding: EdgeInsets.all(20),
  78. child: Column(
  79. mainAxisAlignment: MainAxisAlignment.center,
  80. children: <Widget>[
  81. Row(
  82. mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  83. children: <Widget>[
  84. Column(
  85. children: <Widget>[
  86. Text('Its Using Image Network'),
  87. CircleAvatar(
  88. radius: 70,
  89. backgroundImage: NetworkImage(
  90. '${Urls.BASE_API_IMAGE}/${widget.imageUpdate}'),
  91. ),
  92. ],
  93. ),
  94. Column(
  95. children: <Widget>[
  96. Text('im want this using image.file()'),
  97. CircleAvatar(
  98. radius: 70,
  99. backgroundImage: NetworkImage(
  100. '${Urls.BASE_API_IMAGE}/${widget.imageUpdate}'),
  101. ),
  102. ],
  103. ),
  104. ],
  105. ),
  106. TextFormField(
  107. readOnly: true,
  108. controller: _txtId,
  109. decoration: new InputDecoration(
  110. labelText: "Enter ID",
  111. fillColor: Colors.white,
  112. ),
  113. keyboardType: TextInputType.phone,
  114. ),
  115. TextFormField(
  116. autofocus: true,
  117. maxLength: 9,
  118. controller: _txtNRP,
  119. decoration: new InputDecoration(
  120. labelText: "Enter NRP",
  121. fillColor: Colors.white,
  122. ),
  123. keyboardType: TextInputType.phone,
  124. ),
  125. TextFormField(
  126. controller: _txtNama,
  127. decoration: new InputDecoration(
  128. labelText: "Enter Name",
  129. fillColor: Colors.white,
  130. ),
  131. keyboardType: TextInputType.text,
  132. ),
  133. TextFormField(
  134. controller: _txtEmail,
  135. decoration: new InputDecoration(
  136. labelText: "Enter Email",
  137. fillColor: Colors.white,
  138. ),
  139. keyboardType: TextInputType.emailAddress,
  140. ),
  141. TextFormField(
  142. controller: _txtJurusan,
  143. decoration: new InputDecoration(
  144. labelText: "Enter Jurusan",
  145. fillColor: Colors.white,
  146. ),
  147. keyboardType: TextInputType.text,
  148. ),
  149. ],
  150. ),
  151. ),
  152. ),
  153. );
  154. }
  155.  
  156. Widget _loadingIndicator() {
  157. return Align(
  158. alignment: Alignment.center,
  159. child: CircularProgressIndicator(),
  160. );
  161. }
  162.  
  163. void _validateTextField() async {
  164. if (_txtNRP.text == widget.nrpUpdate &&
  165. _txtNama.text == widget.namaUpdate &&
  166. _txtEmail.text == widget.emailUpdate &&
  167. _txtJurusan.text == widget.jurusanUpdate) {
  168. _showSnackBar(context, 'Data Tidak Boleh Sama', Colors.red);
  169. } else {
  170. _updateRevisi();
  171. }
  172. }
  173.  
  174. void _updateRevisi() async {
  175. final body = {
  176. "nrp": _txtNRP.text,
  177. "nama": _txtNama.text,
  178. "email": _txtEmail.text,
  179. "jurusan": _txtJurusan.text,
  180. "id": _txtId.text,
  181. };
  182. int update = await api.updateMahasiswaRevisi(CrudComponent.headers, body);
  183. if (update == 204) {
  184. Navigator.push(
  185. context,
  186. PageTransition(
  187. child: Welcome(), type: PageTransitionType.leftToRight));
  188. } else {
  189. _showSnackBar(context, "Failed Update ", Colors.red[800]);
  190. }
  191. }
  192.  
  193. void _showSnackBar(BuildContext context, String message, Color color) {
  194. final snackBar = SnackBar(
  195. duration: Duration(seconds: 1),
  196. backgroundColor: color,
  197. content: Text(message),
  198. );
  199. _scaffoldKey.currentState.showSnackBar(snackBar);
  200. }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment