This commit is contained in:
Mohamed Nouffer
2022-03-30 16:19:18 +05:30
parent ea874c094c
commit ee3845681d
5 changed files with 193 additions and 31 deletions

View File

@ -1,7 +1,10 @@
import 'package:flutter_map/plugin_api.dart';
import 'package:geojson/geojson.dart';
import 'package:get/get.dart';
import 'package:latlong2/latlong.dart';
import 'package:rogapp/pages/map/map_page.dart';
import 'package:rogapp/services/location_service.dart';
import 'package:rogapp/services/perfecture_service.dart';
@ -10,20 +13,86 @@ class HomeController extends GetxController {
List<GeoJsonFeatureCollection> locations = <GeoJsonFeatureCollection>[].obs;
List<GeoJsonFeature> currentFeature = <GeoJsonFeature>[].obs;
List<dynamic> perfectures = <dynamic>[].obs;
List<LatLngBounds> currentBound = <LatLngBounds>[].obs;
List<dynamic> subPerfs = <dynamic>[].obs;
@override
void onInit() {
super.onInit();
LocationService.loadLocations().then((value){
locations.add(value!);
});
PerfectureService.loadPerfectures().then((value){
perfectures.add(value);
print(perfectures);
});
super.onInit();
}
void getBoundFromLatLng(List<LatLng> list) {
double? x0, x1, y0, y1;
for (LatLng latLng in list) {
if (x0 == null) {
x0 = x1 = latLng.latitude;
y0 = y1 = latLng.longitude;
} else {
if (latLng.latitude > x1!) x1 = latLng.latitude;
if (latLng.latitude < x0) x0 = latLng.latitude;
if (latLng.longitude > y1!) y1 = latLng.longitude;
if (latLng.longitude < y0!) y0 = latLng.longitude;
}
}
currentBound.clear();
currentBound.add(LatLngBounds(LatLng(x1!, y1!), LatLng(x0!, y0!)));
}
void setBounds(){
List<LatLng> lts = [];
if(locations.length > 0){
for(GeoJsonFeature i in locations[0].collection){
GeoJsonMultiPoint p = i.geometry as GeoJsonMultiPoint;
LatLng lt = LatLng(p.geoSerie!.geoPoints[0].latitude , p.geoSerie!.geoPoints[0].longitude) ;
lts.add(lt);
}
}
else{
LatLng lt = LatLng(37.15319600454702, 139.58765950528198);
lts.add(lt);
}
getBoundFromLatLng(lts);
}
void loadLocationforPerf(String perf){
locations.clear();
LocationService.loadLocationsFor(perf).then((value){
locations.add(value!);
setBounds();
MapPage.mapController.fitBounds(currentBound[0]);
});
}
String getSubInitialVal(){
int min = 0;
if(subPerfs.length > 0){
min = subPerfs[0][0]['id'] as int;
for(var sub in subPerfs[0]){
int x = sub['id'] as int;
if(x < min){
min = x;
}
}
}
return min.toString();
}
void loadSubPerfFor(String perf){
subPerfs.clear();
PerfectureService.loadSubPerfectures(perf).then((value){
subPerfs.add(value);
print(subPerfs);
});
}
GeoJsonFeature? getFeatureForLatLong(double lat, double long){

View File

@ -21,7 +21,26 @@ class MapPage extends StatelessWidget {
final HomeController homeController = Get.find<HomeController>();
final MapController mapController = MapController();
static final MapController mapController = MapController();
Widget getBreadCurms(){
print("---------map-------------");
return Obx(() =>
homeController.perfectures.length > 0 ?
BreadCrumb.builder(
itemCount: homeController.perfectures.length,
builder: (index) {
return BreadCrumbItem(
content: PerfectureWidget(homeController: homeController,) //Text('Item$index')
);
},
divider: Icon(Icons.chevron_right),
) :
Container(width: 0, height: 0,),
);
}
@override
Widget build(BuildContext context) {
@ -68,16 +87,8 @@ class MapPage extends StatelessWidget {
height: 50.0,
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: BreadCrumb(
items: <BreadCrumbItem>[
BreadCrumbItem(content:
//Text('Item1', style: TextStyle(fontSize: 16.0, fontWeight: FontWeight.bold),)
PerfectureWidget(),
),
BreadCrumbItem(content: Text('Item2', style: TextStyle(fontSize: 16.0, fontWeight: FontWeight.bold),)),
],
divider: Icon(Icons.chevron_right),
),
child:
getBreadCurms(),
),
),
Expanded(
@ -87,7 +98,9 @@ class MapPage extends StatelessWidget {
FlutterMap(
mapController: mapController,
options: MapOptions(
center: LatLng(37.15319600454702, 139.58765950528198),
//center: LatLng(37.15319600454702, 139.58765950528198),
bounds: homeController.currentBound.length > 0 ? homeController.currentBound[0]: LatLngBounds.fromPoints([LatLng(37.15319600454702, 139.58765950528198)]),
zoom: 6,
maxZoom: 20,
plugins: [
@ -121,13 +134,14 @@ class MapPage extends StatelessWidget {
builder:((context) => BottomSheetWidget())
);
}
},
size: Size(40, 40),
anchor: AnchorPos.align(AnchorAlign.center),
fitBoundsOptions: const FitBoundsOptions(
padding: EdgeInsets.all(50),
maxZoom: 15,
maxZoom: 265,
),
markers:homeController.locations[0].collection.map((i) {
GeoJsonMultiPoint p = i.geometry as GeoJsonMultiPoint;

View File

@ -8,7 +8,6 @@ class LocationService{
static Future<GeoJsonFeatureCollection?> loadLocations() async {
final geo = GeoJson();
GeoJsonFeatureCollection? fs;
print("#### feature collection ####");
String url = 'http://localhost:8100/api/location/';
final response = await http.get(Uri.parse(url),
headers: <String, String>{
@ -22,4 +21,22 @@ class LocationService{
}
}
static Future<GeoJsonFeatureCollection?> loadLocationsFor(String perfecture) async {
final geo = GeoJson();
GeoJsonFeatureCollection? fs;
String url = 'http://localhost:8100/api/inperf/?perf=' + perfecture;
final response = await http.get(Uri.parse(url),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
);
if (response.statusCode == 200) {
GeoJsonFeatureCollection cc = await featuresFromGeoJson(utf8.decode(response.bodyBytes));
return cc; //featuresFromGeoJson(utf8.decode(response.bodyBytes));
}
}
}

View File

@ -6,7 +6,7 @@ class PerfectureService{
static Future<List<dynamic>?> loadPerfectures() async {
List<dynamic> perfs = [];
String url = 'http://localhost:8100/api/perf/';
String url = 'http://localhost:8100/api/perf_main/';
final response = await http.get(Uri.parse(url),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
@ -15,10 +15,27 @@ class PerfectureService{
if (response.statusCode == 200) {
perfs = json.decode(response.body);
perfs = json.decode(utf8.decode(response.bodyBytes));
}
return perfs;
}
static Future<List<dynamic>?> loadSubPerfectures(String sub) async {
List<dynamic> perfs = [];
String url = 'http://localhost:8100/api/insubperf/?perf=' + sub;
final response = await http.get(Uri.parse(url),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
);
if (response.statusCode == 200) {
perfs = json.decode(utf8.decode(response.bodyBytes));
}
return perfs;
}
}

View File

@ -1,33 +1,31 @@
import 'package:flutter/material.dart';
import 'package:geojson/geojson.dart';
import 'package:get/get.dart';
import 'package:rogapp/pages/home/home_controller.dart';
class PerfectureWidget extends StatefulWidget {
PerfectureWidget({Key? key}) : super(key: key);
final HomeController homeController = Get.find<HomeController>();
HomeController homeController;
PerfectureWidget({required this.homeController});
@override
State<PerfectureWidget> createState() => _PerfectureWidgetState();
}
class _PerfectureWidgetState extends State<PerfectureWidget> {
String dropdownValue = 'One';
String dropdownValue = "1";
List<DropdownMenuItem<String>> getDropdownItems() {
List<DropdownMenuItem<String>> dropDownItems = [];
print("---------");
print(widget.homeController.perfectures);
for (List<String> currency in widget.homeController.perfectures[0]) {
for (Map<String, dynamic> currency in widget.homeController.perfectures[0]) {
//print(currency["id"].toString());
var newDropdown = DropdownMenuItem(
child: Text("dddd"),
value: "qqqq",
child: Text(currency["adm1_ja"].toString()),
value: currency["id"].toString(),
);
dropDownItems.add(newDropdown);
@ -35,11 +33,30 @@ class _PerfectureWidgetState extends State<PerfectureWidget> {
return dropDownItems;
}
List<DropdownMenuItem<String>> getSubDropdownItems() {
List<DropdownMenuItem<String>> dropDownItems = [];
if(widget.homeController.subPerfs.length > 0){
for (Map<String, dynamic> currency in widget.homeController.subPerfs[0]) {
//print(currency["id"].toString());
var newDropdown = DropdownMenuItem(
child: Text(currency["adm2_l"].toString()),
value: currency["id"].toString(),
);
dropDownItems.add(newDropdown);
}
}
return dropDownItems;
}
@override
Widget build(BuildContext context) {
return DropdownButton<String>(
return Obx(() =>
Row(
children: [
DropdownButton<String>(
value: dropdownValue,
icon: const Icon(Icons.arrow_downward),
elevation: 16,
@ -51,9 +68,37 @@ class _PerfectureWidgetState extends State<PerfectureWidget> {
onChanged: (String? newValue) {
setState(() {
dropdownValue = newValue!;
print(newValue);
widget.homeController.loadLocationforPerf(newValue);
widget.homeController.loadSubPerfFor(newValue);
});
},
items:getDropdownItems());
items:getDropdownItems()),
widget.homeController.subPerfs.length > 0 ?
DropdownButton<String>(
value: widget.homeController.getSubInitialVal(),
icon: const Icon(Icons.arrow_downward),
elevation: 16,
style: const TextStyle(color: Colors.deepPurple),
underline: Container(
height: 2,
color: Colors.deepPurpleAccent,
),
onChanged: (String? newValue) {
setState(() {
dropdownValue = newValue!;
print(newValue);
//widget.homeController.loadLocationforPerf(newValue);
});
},
items:getSubDropdownItems()) :
Text("--")
],
)
);
}
}