Advertisement
wildanfuady

Untitled

Nov 13th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.36 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:grouped_buttons/grouped_buttons.dart';
  3. // Memanggil depedensi untuk memanipulasi data server
  4. import 'package:http/http.dart' as http;
  5. import 'package:flutter/services.dart';
  6. import 'package:sekolahku/project/ListSiswa.dart';
  7.  
  8. class AddSiswa extends StatelessWidget {
  9. @override
  10. Widget build(BuildContext context) {
  11. final appTitle = 'Tambah Siswa';
  12.  
  13. return Scaffold(
  14. appBar: AppBar(
  15. title: Text(appTitle),
  16. leading: IconButton(
  17. icon: Icon(Icons.arrow_back),
  18. onPressed: () => {
  19. // kembali ke list siswa
  20. Navigator.of(context).pushReplacement(
  21. new MaterialPageRoute(
  22. builder: (_) {
  23. return new ListSiswa();
  24. }
  25. ),
  26. ),
  27. },
  28. ),
  29. ),
  30. body: FormAddSiswa(),
  31. );
  32. }
  33. }
  34.  
  35. class FormAddSiswa extends StatefulWidget {
  36.  
  37. @override
  38. _AddSiswa createState() => _AddSiswa();
  39.  
  40. }
  41.  
  42. class _AddSiswa extends State<FormAddSiswa> {
  43.  
  44. // Buat list untuk radio
  45. String _jenjang = "TK";
  46. bool isChecked = true;
  47. final _formKey = GlobalKey<FormState>();
  48.  
  49. // Buat list untuk checkbox
  50. List<String> hobi = ["Membaca", "Menulis", "Menggambar"];
  51.  
  52. // Buat controller untuk menampung setiap data yang diinputkan dari text field
  53. TextEditingController controllerNama = new TextEditingController();
  54. TextEditingController controllerTelp = new TextEditingController();
  55. TextEditingController controllerKelas = new TextEditingController();
  56. TextEditingController controllerUsia = new TextEditingController();
  57. TextEditingController controllerAlamat = new TextEditingController();
  58.  
  59. void addData() {
  60. var url = "http://10.0.2.2/flutter-sekolahku-server/siswa/addsiswa.php";
  61. http.post(url, body: {
  62. "nama" : controllerNama.text,
  63. "telp" : controllerTelp.text,
  64. "kelas" : controllerKelas.text,
  65. "usia" : controllerUsia.text,
  66. "alamat" : controllerAlamat.text,
  67. "jenjang" : _jenjang,
  68. "hobi" : "${hobi.toString()}",
  69. });
  70. // clear data
  71. clear();
  72. }
  73.  
  74. clear() {
  75. // controllerTelp = null;
  76. _formKey.currentState?.reset();
  77. }
  78.  
  79. Widget build(BuildContext context){
  80.  
  81. final Size screenSize = MediaQuery.of(context).size;
  82.  
  83.  
  84. return Form(
  85. key: _formKey,
  86. child: Padding(
  87. padding: const EdgeInsets.all(10.0),
  88. child: ListView(
  89. children: <Widget>[
  90. TextFormField(
  91. controller: controllerNama,
  92. keyboardType: TextInputType.text,
  93. textInputAction: TextInputAction.next,
  94. decoration: InputDecoration(
  95. hintText: "Nama Siswa",
  96. icon: Icon(Icons.person),
  97. labelText: "Nama Siswa *",
  98. ),
  99. validator: (val){
  100. if(val.isEmpty){
  101. return "Nama wajib diisi";
  102. }
  103. return null;
  104. },
  105. ),
  106. TextFormField(
  107. controller: controllerKelas,
  108. keyboardType: TextInputType.number,
  109. textInputAction: TextInputAction.next,
  110. decoration: InputDecoration(
  111. hintText: "Kelas",
  112. icon: Icon(Icons.school),
  113. labelText: "Kelas *",
  114. ),
  115. validator: (val){
  116. if(val.isEmpty){
  117. return "Kelas wajib diisi";
  118. }
  119. return null;
  120. },
  121. ),
  122. TextFormField(
  123. controller: controllerUsia,
  124. keyboardType: TextInputType.number,
  125. textInputAction: TextInputAction.next,
  126. decoration: InputDecoration(
  127. hintText: "Usia",
  128. icon: Icon(Icons.assessment),
  129. labelText: "Usia *",
  130. ),
  131. validator: (val){
  132. if(val.isEmpty){
  133. return "Usia wajib diisi";
  134. }
  135. return null;
  136. },
  137. ),
  138. TextFormField(
  139. controller: controllerTelp,
  140. keyboardType: TextInputType.number,
  141. textInputAction: TextInputAction.next,
  142. decoration: InputDecoration(
  143. hintText: "Phone",
  144. icon: Icon(Icons.phone),
  145. labelText: "Phone *",
  146. ),
  147. validator: (val){
  148. if(val.isEmpty){
  149. return "Phone wajib diisi";
  150. }
  151. return null;
  152. },
  153. ),
  154. TextFormField(
  155. controller: controllerAlamat,
  156. keyboardType: TextInputType.multiline,
  157. maxLines: 4,
  158. textInputAction: TextInputAction.next,
  159. decoration: InputDecoration(
  160. hintText: "Alamat",
  161. icon: Icon(Icons.room),
  162. labelText: "Alamat *",
  163. ),
  164. validator: (val){
  165. if(val.isEmpty){
  166. return "Alamat wajib diisi";
  167. }
  168. return null;
  169. },
  170. ),
  171. Padding(
  172. padding: EdgeInsets.only(top: 8.0),
  173. ),
  174. Text(
  175. "Jenjang",
  176. style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
  177. ),
  178. DropdownButton<String>(
  179. isExpanded: true,
  180. value: _jenjang,
  181. onChanged: (String jenjang){
  182. setState(() {
  183. _jenjang = jenjang;
  184. });
  185. },
  186. items: <String>[
  187. 'TK',
  188. 'SD',
  189. 'SMP',
  190. 'SMA'
  191. ].map<DropdownMenuItem<String>>(
  192. (String value){
  193. return DropdownMenuItem<String>(
  194. value: value,
  195. child: Text(value),
  196. );
  197. }
  198. ).toList(),
  199.  
  200. ),
  201. Padding(
  202. padding: EdgeInsets.only(top: 8.0),
  203. ),
  204. Text(
  205. "Hobi",
  206. style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
  207. ),
  208. CheckboxGroup(
  209. labels: <String>[
  210. "Membaca",
  211. "Menulis",
  212. "Menggambar",
  213. ],
  214. onSelected: (List<String> checked) => setState((){
  215. hobi = checked;
  216. print("checked: ${checked.toString()}");
  217. print(checked);
  218. }),
  219. ),
  220. Container(
  221. width: screenSize.width,
  222. child: new RaisedButton(
  223. child: new Text(
  224. "Simpan",
  225. style: TextStyle(color: Colors.white),
  226. ),
  227. onPressed: (){
  228. if (_formKey.currentState.validate()) {
  229. print("save");
  230. addData();
  231. }
  232. },
  233. color: Colors.blue,
  234. ),
  235. margin: EdgeInsets.only(top: 20),
  236. ),
  237. ],
  238. ),
  239. ),
  240. );
  241.  
  242. }
  243.  
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement