import 'package:amap_flutter_location/amap_flutter_location.dart'; import 'package:amap_flutter_location/amap_location_option.dart'; import 'package:dio/dio.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:huixiang/generated/l10n.dart'; import 'package:huixiang/retrofit/data/base_data.dart'; import 'package:huixiang/retrofit/data/store.dart'; import 'package:huixiang/retrofit/retrofit_api.dart'; import 'package:huixiang/view_widget/icon_text.dart'; import 'package:huixiang/view_widget/loading_view.dart'; import 'package:huixiang/view_widget/my_appbar.dart'; import 'package:permission_handler/permission_handler.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:amap_flutter_base/amap_flutter_base.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; class StoreSelectorPage extends StatefulWidget { @override State createState() { return _StoreSelectorPage(); } } class _StoreSelectorPage extends State { ApiService apiService; AMapFlutterLocation aMapFlutterLocation; @override void dispose() { super.dispose(); aMapFlutterLocation.stopLocation(); aMapFlutterLocation.destroy(); } @override void initState() { super.initState(); SharedPreferences.getInstance().then((value) { apiService = ApiService(Dio(), context: context, token: value.getString('token')); }); if (aMapFlutterLocation == null) { AMapFlutterLocation.setApiKey("f39d1daa020a56f208eb2519f63e9534", "feaae7986201b571cace1b83728be5bb"); aMapFlutterLocation = AMapFlutterLocation(); aMapFlutterLocation.onLocationChanged().listen((event) { if (event != null && event["latitude"] != null && event["longitude"] != null) { print("location: $event"); aMapFlutterLocation.stopLocation(); if (event["latitude"] is String && event["longitude"] is String) { latLng = LatLng(double.tryParse(event["latitude"]), double.tryParse(event["longitude"])); } else { latLng = LatLng(event["latitude"], event["longitude"]); } if (Navigator.of(context).canPop()) { Navigator.of(context).pop(); } saveLatLng( latLng, event["province"], event["city"], event["district"]); queryStore("${event["latitude"]}", "${event["longitude"]}", event["province"], event["city"], event["district"]); } }); } aMapFlutterLocation.setLocationOption(AMapLocationOption( needAddress: true, onceLocation: true, locationInterval: 100000, locationMode: AMapLocationMode.Hight_Accuracy, desiredAccuracy: DesiredAccuracy.ThreeKilometers, pausesLocationUpdatesAutomatically: true, )); startLocation(); } saveLatLng(LatLng latLng, province, city, district) async { SharedPreferences prefs = await SharedPreferences.getInstance(); await prefs.setString("latitude", "${latLng.latitude}"); await prefs.setString("longitude", "${latLng.longitude}"); await prefs.setString("province", province); await prefs.setString("city", city); await prefs.setString("district", district); } List storeList; LatLng latLng; startLocation() async { if (await Permission.locationWhenInUse.serviceStatus.isEnabled) { bool isShown = await Permission.contacts.shouldShowRequestRationale; // if (isShown) { // SmartDialog.showToast("shouldShowRequestRationale"); // } if (await Permission.location.isPermanentlyDenied) { openAppSettings(); getLatLng(); } else if (await Permission.location.isGranted) { showCupertinoDialog( context: context, barrierDismissible: true, builder: (context) { return LoadingView(); }); aMapFlutterLocation.startLocation(); } else { PermissionStatus permissionStatus = await Permission.location.request(); if (permissionStatus.isGranted) { startLocation(); } else { openAppSettings(); getLatLng(); } } } else { //enabledLocation getLatLng(); } } getLatLng() async { SharedPreferences.getInstance().then( (value) => { if (value.containsKey("latitude") && value.containsKey("longitude") && value.containsKey("province") && value.containsKey("city") && value.containsKey("district")) { latLng = LatLng(double.tryParse(value.getString("latitude")), double.tryParse(value.getString("longitude"))), queryStore( value.getString("latitude"), value.getString("longitude"), value.getString("province"), value.getString("city"), value.getString("district"), ), } else { queryStore("", "", "", "", ""), } }, ); } queryStore(latitude, longitude, province, city, district) async { BaseData baseData = await apiService.queryStore({ "city": "", "district": "", "latitude": latitude, "longitude": longitude, "province": "", "searchKey": "" }); if (baseData != null && baseData.isSuccess) { storeList = (baseData.data as List) .map((e) => Store.fromJson(e)) .toList(); if (mounted) setState(() {}); } } @override Widget build(BuildContext context) { return Scaffold( appBar: MyAppBar( title: S.of(context).mendianxuanzhe, titleColor: Colors.black, titleSize: 18.sp, background: Color(0xFFFAFAFA), leadingColor: Colors.black, ), body: Container( margin: EdgeInsets.only(top: 18.h), child: Column( children: [ Expanded( child: ListView.builder( itemCount: storeList != null ? storeList.length : 0, physics: BouncingScrollPhysics(), itemBuilder: (context, position) { return GestureDetector( onTap: () { setState(() { groupValue = position; }); }, child: buildStoreItem(storeList[position], position), ); }), ), InkWell( onTap: () { if (groupValue == null) { SmartDialog.showToast(S.of(context).qingxuanzeyigemendian, alignment: Alignment.center); return; } Store store = storeList[groupValue]; Navigator.of(context).pop({ "id": store.id, "address": store.address, }); }, child: Container( padding: EdgeInsets.only(top: 16.h, bottom: 16.h), decoration: BoxDecoration( color: Color(0xFF32A060), borderRadius: BorderRadius.only( topLeft: Radius.circular(4), topRight: Radius.circular(4), ), ), alignment: Alignment.center, child: Text( S.of(context).queren, style: TextStyle( fontSize: 16.sp, color: Color(0xFFFFFFFF), fontWeight: FontWeight.bold, ), ), ), ), ], ), ), ); } Widget buildStoreItem(Store store, position) { return Container( margin: EdgeInsets.only(left: 16.w, right: 16.w, top: 8.h, bottom: 8.h), padding: EdgeInsets.all(16), decoration: BoxDecoration( color: Colors.white, boxShadow: [ BoxShadow( color: Colors.black.withAlpha(12), offset: Offset(0, 3), blurRadius: 14, spreadRadius: 0, ) ], borderRadius: BorderRadius.circular(8), ), child: Row( children: [ Expanded( child: Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( store.storeName, style: TextStyle( fontSize: 16.sp, fontWeight: FontWeight.bold, color: Color(0xFF353535), ), ), SizedBox( height: 12.h, ), IconText( store.address, isMax: true, textAxisAlignment: CrossAxisAlignment.start, textStyle: TextStyle( color: Color(0xFF353535), fontSize: 14.sp, ), leftIcon: Icons.location_on, iconColor: Color(0xFF32A060), iconSize: 16, ), SizedBox( height: 4.h, ), IconText( (store.openStartTime == null && store.openEndTime == null) ? S.of(context).quantian : "${store.openStartTime}-${store.openEndTime}", textStyle: TextStyle( color: Color(0xFF353535), fontSize: 14.sp, ), leftIcon: Icons.access_time, iconColor: Color(0xFF32A060), iconSize: 16, ), ], ), ), Radio( value: position, groupValue: groupValue, activeColor: Colors.green, onChanged: (value) { setState(() { groupValue = value; }); }, ) ], ), ); } int groupValue = 0; }