Advertisement
rifki_cs29

homepage

Oct 12th, 2021
1,224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.98 KB | None | 0 0
  1. import 'dart:convert';
  2.  
  3. import 'package:flutter/material.dart';
  4. import 'package:get/get.dart';
  5. import 'package:http/http.dart' as http;
  6. import 'package:weather_app/views/pages/weather_page.dart';
  7.  
  8. class HomePage extends StatefulWidget {
  9.   const HomePage({ Key? key }) : super(key: key);
  10.  
  11.   @override
  12.   _HomePageState createState() => _HomePageState();
  13. }
  14.  
  15. class _HomePageState extends State<HomePage> {
  16.   static const String _urlRegion = 'https://emsifa.github.io/api-wilayah-indonesia/api/';
  17.   String? _valProvince;
  18.   String? _valCity;
  19.   List<dynamic> _dataProvince = [];
  20.   List<dynamic> _dataCity = [];
  21.  
  22.   Future<void> getProvince() async {
  23.     final response = await http.get(Uri.parse('${_urlRegion}provinces.json')); //untuk melakukan request ke webservice
  24.     var listData = jsonDecode(response.body);
  25.     setState(() {
  26.       _dataProvince = listData;
  27.     });
  28.   }
  29.  
  30.   Future<void> getCity(String idProvince)async{
  31.     final response = await http.get(Uri.parse('${_urlRegion}regencies/$idProvince.json'));
  32.  
  33.     var listData = jsonDecode(response.body);
  34.     setState(() {
  35.       _dataCity = listData;
  36.     });
  37.   }
  38.  
  39.  
  40.   @override
  41.   void initState() {
  42.     super.initState();
  43.     getProvince();
  44.   }
  45.  
  46.   @override
  47.   Widget build(BuildContext context) {
  48.     return Scaffold(
  49.       appBar: AppBar(
  50.         title: const Text("Dropdown Bercabang"),
  51.       ),
  52.       body: Padding(
  53.         padding: const EdgeInsets.all(16.0),
  54.         child: Column(
  55.           mainAxisAlignment: MainAxisAlignment.start,
  56.           children: <Widget>[
  57.             const Text("Silahkan pilih provinsi tujuan"),
  58.  
  59.             DropdownButton(
  60.               hint: const Text("Select Province"),
  61.               value: _valProvince,
  62.               items: _dataProvince.map((item){
  63.                 return DropdownMenuItem(
  64.                   child: Text(item['name']),
  65.                   value: item['id'],
  66.                 );
  67.               }).toList(),
  68.               onChanged: (value) {
  69.                 setState(() {
  70.                   _valProvince = value.toString() ;
  71.                   _valCity = null;
  72.                 });
  73.                 getCity(value.toString());
  74.               },
  75.             ),
  76.  
  77.  
  78.             const Text("Silahkan pilih kota tujuan"),
  79.             DropdownButton(
  80.               hint: const Text("Select City"),
  81.               value: _valCity,
  82.               items: _dataCity.map((item){
  83.                 return DropdownMenuItem(
  84.                   child: Text(item['name']),
  85.                   value: item['name'],
  86.                 );
  87.               }).toList(),
  88.               onChanged: (value) {
  89.                setState(() {
  90.                  _valCity = value.toString();
  91.                });
  92.               },
  93.             ),
  94.             ElevatedButton(
  95.               onPressed: () => Get.to(() => WeatherPage(city: _valCity?.split(" ").sublist(1, 2).join(" ").toLowerCase() ?? '')),
  96.               child: const Text('Halaman Utama'))
  97.           ],
  98.         ),
  99.       ),
  100.     );
  101.   }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement