Skip to content

Instantly share code, notes, and snippets.

@goldalworming
Created July 13, 2019 11:38
Show Gist options
  • Save goldalworming/766813612f0a0391817aa3f5b252cb14 to your computer and use it in GitHub Desktop.
Save goldalworming/766813612f0a0391817aa3f5b252cb14 to your computer and use it in GitHub Desktop.
import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
class AccountPage extends StatefulWidget {
@override
_AccountPageState createState() => _AccountPageState();
}
class _AccountPageState extends State<AccountPage> {
var arrDaerah = [{"Nama":"Pilih Daerah","IdStr":""}];
var idDaerah = '';
var arrDesa = [{"Nama":"Pilih Desa","IdStr":""}];
var idDesa = '';
var arrKelompok = [{"Nama":"Pilih Kelompok","IdStr":""}];
var idKelompok = '';
Future<List> getDaerah() async {
var url = 'https://spll-rangers.im-engine.com/master_level?query=parent:0';
var resp = await http.get(url);
var respDaerah = json.decode(resp.body)['Rows'];
if (arrDaerah.length == 1) {
for (var i = 0; i < respDaerah.length; i++) {
var stringParams = {"Nama":"","IdStr":""};
stringParams["Nama"] = respDaerah[i]["Nama"];
stringParams["IdStr"] = respDaerah[i]["IdStr"];
arrDaerah.add(stringParams);
}
}
}
Future<List> getDesa(String value) async {
var url = 'https://spll-rangers.im-engine.com/master_level?query=parent:$value';
var resp = await http.get(url);
var respDesa = json.decode(resp.body)['Rows'];
if (arrDesa.length == 1) {
for (var i = 0; i < respDesa.length; i++) {
var stringParams = {"Nama":"","IdStr":""};
stringParams["Nama"] = respDesa[i]["Nama"];
stringParams["IdStr"] = respDesa[i]["IdStr"];
arrDesa.add(stringParams);
}
}
}
Future<List> getKelompok(String value) async {
var url = 'https://spll-rangers.im-engine.com/master_level?query=parent:$value';
var resp = await http.get(url);
var respKelompok = json.decode(resp.body)['Rows'];
if (arrKelompok.length == 1) {
for (var i = 0; i < respKelompok.length; i++) {
var stringParams = {"Nama":"","IdStr":""};
stringParams["Nama"] = respKelompok[i]["Nama"];
stringParams["IdStr"] = respKelompok[i]["IdStr"];
arrKelompok.add(stringParams);
}
}
}
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Halaman Akun"),
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.exit_to_app),
onPressed: () {
Navigator.pushReplacementNamed(context, "/login_page");
}),
body: new FutureBuilder<List>(
future: getDaerah(), // async work
builder: (BuildContext context, AsyncSnapshot<List> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.waiting:
// return new CircularProgressIndicator();
default:
if (snapshot.hasError)
return new Text('Error: ${snapshot.error}');
else
return Column(
children: <Widget>[
new DropdownButton<String>(
items: arrDaerah.map((var item) {
return new DropdownMenuItem<String>(
value: item["IdStr"].toString(),
child: Text(item["Nama"].toString()),
);
}).toList(),
onChanged: (newVal) {
setState(() {
idDaerah = newVal;
arrDesa = [{"Nama":"Pilih Desa","IdStr":""}];
idDesa = '';
getDesa(idDaerah);
});
},
value: idDaerah,
),
new DropdownButton<String>(
items: arrDesa.map((var item) {
return new DropdownMenuItem<String>(
value: item["IdStr"].toString(),
child: Text(item["Nama"].toString()),
);
}).toList(),
onChanged: (newVal) {
setState(() {
idDesa = newVal;
arrKelompok = [{"Nama":"Pilih Kelompok","IdStr":""}];
idKelompok = '';
getKelompok(idDesa);
});
},
value: idDesa,
),
new DropdownButton<String>(
items: arrKelompok.map((var item) {
return new DropdownMenuItem<String>(
value: item["IdStr"].toString(),
child: Text(item["Nama"].toString()),
);
}).toList(),
onChanged: (newVal) {
setState(() {
idKelompok = newVal;
});
},
value: idDesa,
),
],
);
}
},
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment